Code to make motor go difrent speeds randomly

So curently i am trying to find how to code a motor that when you push a bumber switch it will start a motor at a random speed for a second could any one help me

1 Like

Is there a way to get a random number? I’m positive there must be, although I’ve never wanted to so I’ve never thought to try.

Have you tried srand() and rand()? I would set RAND_MAX to 200 and subtract 100 to get -100 to 100. I would then start with srand() and seed it using the timer so that you are highly unlikely to start with the same seed each time you run the program. You can quickly find documentation on these in lots of C/C++ references.

@Vexooooyea, assuming the functions that @callen had suggested will work for you, I would caution from suddenly sending motors high levels of power.

If there is no load or a very light load attached to the motor, that may be fine, but it would still put stress on the internal gears.

However, if there is load attached to the motor, then sudden power increases are know n to eventually shred internal gears:

You would need to apply some sort of slew control, where instead of one sudden jump, you increase motor power in several small steps. Let say 10 power unit every 20 msec.