Advertisement
ChristineWu

Servo LED

Jan 12th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1.  
  2. #include <Servo.h>
  3.  
  4. Servo myservo; //create servo objet to control a servo
  5. const int potpin=A0; //0 assumes A0, analog pin used to ocnnect the potentiometer
  6. int potval; //variable to read the value from the analog pin
  7. const int WritePinYellow=13;
  8. const int WritePinGreen=12;
  9. const int WritePinBlue=11;
  10.  
  11. void setup()
  12. {
  13. pinMode (potpin, INPUT);
  14. pinMode (WritePinYellow,OUTPUT);
  15. pinMode (WritePinGreen,OUTPUT);
  16. pinMode (WritePinBlue,OUTPUT);
  17. Serial.begin (9600);
  18. myservo.attach(9); // attaches the servo on pin 9 to the servo object- myservo is the name of the object
  19. //the . means i will use a function from another thing- which is attach, and 9 represents the pin for your servo
  20.  
  21. }
  22.  
  23. void loop()
  24. {
  25. //we already declared, we did all that, we did the set up, thats done- whats next in logic?
  26. //first thing you do is YOU NEED TO READ- (begin done in the set up, read in the loop)
  27. //then loop starts. it goes val=analogRead(potpin);
  28. //val has been declared to value read- you want to read the value to check if its working (and it is working)
  29. potval=analogRead(potpin); //reads the value of the potentiometer
  30. Serial.println(potval);
  31. potval=map(potval, 0, 1023, 0, 180); //scale it to use it within the servo
  32. myservo.write(potval); //this sets the servo position according to code
  33. delay(15); //waits for the servo to get there
  34. if (potval<=89)
  35. {
  36. digitalWrite (WritePinYellow,HIGH);
  37. digitalWrite (WritePinGreen,LOW);
  38. digitalWrite (WritePinBlue,LOW);
  39. }
  40. if (potval==90)
  41. {
  42. digitalWrite (WritePinGreen,HIGH);
  43. digitalWrite (WritePinYellow,LOW);
  44. digitalWrite (WritePinBlue,LOW);
  45. }
  46. if (potval>=180)
  47. {
  48. digitalWrite (WritePinBlue,HIGH);
  49. digitalWrite (WritePinYellow,LOW);
  50. digitalWrite (WritePinGreen,LOW);
  51. }
  52.  
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement