Pages: [1]   Go Down
Author Topic: Incremental Servo Motor Rotation  (Read 427 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8854
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.






Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: