Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- Servo myservo; //create servo objet to control a servo
- const int potpin=A0; //0 assumes A0, analog pin used to ocnnect the potentiometer
- int potval; //variable to read the value from the analog pin
- const int WritePinYellow=13;
- const int WritePinGreen=12;
- const int WritePinBlue=11;
- void setup()
- {
- pinMode (potpin, INPUT);
- pinMode (WritePinYellow,OUTPUT);
- pinMode (WritePinGreen,OUTPUT);
- pinMode (WritePinBlue,OUTPUT);
- Serial.begin (9600);
- myservo.attach(9); // attaches the servo on pin 9 to the servo object- myservo is the name of the object
- //the . means i will use a function from another thing- which is attach, and 9 represents the pin for your servo
- }
- void loop()
- {
- //we already declared, we did all that, we did the set up, thats done- whats next in logic?
- //first thing you do is YOU NEED TO READ- (begin done in the set up, read in the loop)
- //then loop starts. it goes val=analogRead(potpin);
- //val has been declared to value read- you want to read the value to check if its working (and it is working)
- potval=analogRead(potpin); //reads the value of the potentiometer
- Serial.println(potval);
- potval=map(potval, 0, 1023, 0, 180); //scale it to use it within the servo
- myservo.write(potval); //this sets the servo position according to code
- delay(15); //waits for the servo to get there
- if (potval<=89)
- {
- digitalWrite (WritePinYellow,HIGH);
- digitalWrite (WritePinGreen,LOW);
- digitalWrite (WritePinBlue,LOW);
- }
- if (potval==90)
- {
- digitalWrite (WritePinGreen,HIGH);
- digitalWrite (WritePinYellow,LOW);
- digitalWrite (WritePinBlue,LOW);
- }
- if (potval>=180)
- {
- digitalWrite (WritePinBlue,HIGH);
- digitalWrite (WritePinYellow,LOW);
- digitalWrite (WritePinGreen,LOW);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement