hello,I want to control servo motor by potentiometer or by 02 buttons (up and down),
so, I use one button to to select function
I made this code but it doesn't work correctely .
where is the problem?
.
#include<Servo.h>
int pot = 0;
int pos = 0 ;
int inPin = 8;
int outPin = 13;
int reading;
int previous = LOW;
long time = 0;
long debounce = 200;
Servo servo;
void setup() {
pinMode(inPin, INPUT);
pinMode(outPin, OUTPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
servo.attach(9);
}
void loop() {
reading = digitalRead(inPin);
if (reading == HIGH && previous == LOW && millis() - time > debounce){
if (poss ())
pott ();
else
poss ();
time = millis();
}
previous = reading;
}
void pott (){
pot =(analogRead(A0),0,1023,0,180);
servo.write(pot);
}
bool poss (){
while (digitalRead(6) == HIGH && pos < 180 ) {
pos++;
servo.write(pos);
delay (50);
}
while (digitalRead(7) == HIGH && pos > 0 ) {
pos--;
servo.write(pos);
delay (50);
}
}
when I press the select button and I change the value of potentiometer ,it doesn't move( it doen't make any angle),
but when I press the select button again to change from potentiometer to digital's push button ,the motor works .
I think ,if you have another code to change state from potentiometer to digital's pin ,post it ,
that will help me .