Go Down

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

#### bporter88

##### Mar 14, 2013, 10:33 am
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

#1
##### Mar 14, 2013, 04:21 pm
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: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

#### bporter88

#2
##### Mar 14, 2013, 04:48 pm
Aahhhhh, actually quite simple in the end! Thank you very much for your help.

Go Up