Advertisement
coltonspastebin

servo with potentiometer and 3 LED

Jan 23rd, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1.  
  2. #include <Servo.h>
  3.  
  4. Servo myservo; //create servo object to control a servo
  5.  
  6. const int potpin = A0; //analog pin used to connect the potentiometer
  7. int value; //variable to read the value from the analog pin
  8. const int whiteled = 5;
  9. const int yellowled = 4;
  10. const int blueled = 3;
  11.  
  12. void setup()
  13. {
  14. pinMode (potpin, INPUT);
  15. Serial.begin (9600);
  16. myservo.attach(12);//attaches the servo on pin 12 to the servo object
  17. pinMode (whiteled, OUTPUT);
  18. pinMode (yellowled, OUTPUT);
  19. pinMode (blueled, OUTPUT);
  20. }
  21.  
  22. void loop()
  23. {
  24. value = analogRead (potpin);
  25. value = map(value, 0, 1023, 0, 180);
  26. Serial.println (value);
  27. myservo.write (value); //write into servo the value of the potentiometer, makes servo move
  28. delay (15);
  29. if (value < 90)
  30. {
  31. digitalWrite (whiteled, HIGH);
  32. digitalWrite (yellowled, LOW);
  33. digitalWrite (blueled, LOW);
  34. }
  35.  
  36. else if (value == 90)
  37. {
  38. digitalWrite (whiteled, LOW);
  39. digitalWrite (yellowled, HIGH);
  40. digitalWrite (blueled, LOW);
  41. }
  42. else if (value > 90)
  43. {
  44. digitalWrite (whiteled, LOW);
  45. digitalWrite (yellowled, LOW);
  46. digitalWrite (blueled, HIGH);
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement