Here is the code:
#include <Servo.h>
Servo con;
const int buttonPin = 2;
const int potPin = A0;
int potValue = 0;
int buttonState = 0;
int buttonPressed = 0;
void setup() {
Serial.begin(9600);
con.attach(5);
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH);
}
void loop() {
buttonPressed = digitalRead(buttonPin);
buttonPressed = map(buttonPressed,0,1,1,0);//we want a 1 when it's pressed, so we used map to reverse the inputs
if(buttonPressed==1){
buttonState ++;//every time we push the button we increase the button state by 1
delay(500);//make sure we don't push the button to many times by keeping our finger there too long
}
if(buttonState>=2){
buttonState = 0;
}
if(buttonState==0){
con.detach();
}
else if(buttonState==1){
con.attach(5);
potValue = analogRead(potPin); //get value from potentiometer
Serial.println(potValue);
potValue = map(potValue,0,1023,0,179);
con.write(potValue);
potValue = map(potValue, 0,179,0,255);
Serial.println(potValue);
delay(20);
}
}
No comments:
Post a Comment