Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- Servo myservo; // create servo object to control a servo
- // a maximum of eight servo objects can be created
- int pos = 0; // variable to store the servo position
- int val;
- void setup()
- {
- Serial.begin(9600);
- myservo.attach(9); // attaches the servo on pin 9 to the servo object
- myservo.write(0);
- delay(1000);
- }
- void loop()
- {
- delay(100);
- }
- void serialEvent()
- {
- val = Serial.parseInt();
- if(val==1) // goes from 0 degrees to 180 degrees
- {
- pos=pos+5;
- pos=normal(pos);
- myservo.write(pos); // tell servo to go to position in variable 'pos'
- delay(15); // waits 15ms for the servo to reach the position
- }
- else if(val==0) // goes from 180 degrees to 0 degrees
- {
- pos=pos-5;
- pos=normal(pos);
- myservo.write(pos); // tell servo to go to position in variable 'pos'
- delay(15); // waits 15ms for the servo to reach the position
- }
- }
- int normal(int p)
- {
- if(p>175)
- p=180;
- if(p<5)
- p=0;
- return p;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement