Go Down

Topic: Incremental Servo Motor Rotation (Read 442 times) previous topic - next topic

bporter88

Hi all, I'm trying to get a servo motor spin in forward or reverse in random increments of 45degrees on the push of a button. That is to say with the servo motor at position 90 when the button is pushed it might go, say, 45 degrees forward but next time the button is pressed 90 degrees backwards. is it possible to add a random function without having a random choice within a range of numbers?

Hope this makes sense, and that it is possible.

Cheers

B

johnwasser

If I understand correctly you want a servo to go to a random choice of 0, 45, 90, 135, or 180 each time you press a button.

This would do the random motion:
Code: [Select]
servo.write(45*random(0,5));

If it is important for the servo to move on each button press you will have to remember where the servo is positioned and pick rando new positions until you get one that is different.  Without that there is a 1 in 5 chance that the 'new' random position will be the same as the old one.






Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

bporter88

Aahhhhh, actually quite simple in the end! Thank you very much for your help.

Go Up