[resolved]using button to select function

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);
 }
}

Ever heard of serial monitor :wink: Use it for debugging by using Serial.println statements in strategic places.

And poss() is supposed to return a bool; you don't return anything specifically so it will return garbage.

youceforo:
I made this code but it doesn't work correctely . );

What exactly does this mean?

dougp:
What exactly does this mean?

when I press the push button to select potentiometer ,the servo motor doesn't work.

the servo motor doesn't work

What exactly does that mean?

AWOL:
What exactly does that mean?

Probably that poss() 'returns' garbage :slight_smile:

AWOL:
What exactly does that mean?

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 .

+1 for adding Serial.print statements to see what's going on at runtime.

The next thing I'd do is separate the debouncing from the reading compares

if (reading == HIGH && previous == LOW && millis() - time > debounce)

if(millis() - time > debounce){
if (reading == HIGH && previous == LOW){
// do stuff
}
}

or something

Finaly I resolved my problem, thanks for your time.
this is final code :

#include<Servo.h>
int pot = 0;
int pos ;
int state = 1;
int inPin = 8;         
     
int reading;           
int previous = LOW;   


long time = 0;        
long debounce = 200;  
Servo servo;

void setup() {
  pinMode(inPin, INPUT);
 
  
 pinMode(6, INPUT);
 pinMode(7, INPUT);
 servo.attach(9);
}
void loop() {
reading = digitalRead(inPin);
 
 
  if (reading == HIGH && previous == LOW && millis() - time > debounce){
   if (state==1)
     state = 0;
 
     else
  state = 1;
  
    

    time = millis();    
  }


  previous = reading;
  
 
 switch (state) {
    case 0:
     poss ();
      break;
    case 1:
    pott ();
   pos = pot;
      
      break;
    default: 
     
    break;
  }
}
void pott (){
   
  pot = map(analogRead(A0),0,1023,0,180);
     
     servo.write(pot);
   delay(20);}

void 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);
  
   
 }
}