Advertisement
Guest User

arduino code

a guest
Aug 11th, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.12 KB | None | 0 0
  1. #include <Servo.h>
  2.  
  3. Servo myservo;  // create servo object to control a servo
  4.                 // a maximum of eight servo objects can be created
  5.  
  6. int pos = 0;    // variable to store the servo position
  7. int val;
  8. void setup()
  9. {
  10.   Serial.begin(9600);
  11.   myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  12.   myservo.write(0);
  13.   delay(1000);
  14. }
  15. void loop()
  16. {
  17.  delay(100);
  18. }
  19.  
  20. void serialEvent()
  21. {
  22.   val = Serial.parseInt();
  23.   if(val==1)  // goes from 0 degrees to 180 degrees
  24.   {
  25.     pos=pos+5;
  26.     pos=normal(pos);
  27.     myservo.write(pos);              // tell servo to go to position in variable 'pos'
  28.     delay(15);                       // waits 15ms for the servo to reach the position
  29.   }
  30.   else if(val==0)    // goes from 180 degrees to 0 degrees
  31.   {
  32.     pos=pos-5;
  33.     pos=normal(pos);                          
  34.     myservo.write(pos);              // tell servo to go to position in variable 'pos'
  35.     delay(15);                       // waits 15ms for the servo to reach the position
  36.   }
  37.  
  38. }
  39. int normal(int p)
  40. {
  41.  if(p>175)
  42.  p=180;
  43.  if(p<5)
  44.  p=0;
  45.  return p;
  46.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement