Code including both knob and button function for servos

Hi evry1,am currently trying to control three servo motors using knob functions and another by button function. When the code excludes the button function the servos function correctly. but when the code includes the button function the servos do not respond correctly to the inputs given.
The following code is what am using

#include <Servo.h>
Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;
int potpin1 = 0;  // analog pin used to connect the potentiometer
int potpin2 = 1;
int potpin3 = 2;
const int buttonPin = 4; //sets pin 4 as button
int buttonState = 0; //sets button 1 as off
int val1;    // variable to read the value from the analog pin 
int val2;
int val4;
void setup() 
{ 
  myservo1.attach(0);  // attaches the servo on pin 2 to the servo object 
  myservo2.attach(1);
  myservo3.attach(2);
  myservo4.attach(3);
  pinMode (buttonPin, INPUT); // sets button as input
myservo4.write(60); // starts servo at 60 degrees
  
}
void loop() 
{ 
  val1 = analogRead(potpin1);            // reads the value of the potentiometer (value between 0 and 1023) 
  val1 = map(val1, 0, 1023, 0, 60);     // scale it to use it with the servo (value between 0 and 180) 
  myservo1.write(val1);                  // sets the servo position according to the scaled value 
  delay(15);            

  val1 = analogRead(potpin1);            // reads the value of the potentiometer (value between 0 and 1023) 
  val1 = map(val1, 0, 1023, 60, 0);     // scale it to use it with the servo (value between 0 and 180) 
  myservo2.write(val1);
  delay(15);       

  val2 = analogRead(potpin2);             
  val2 = map(val2, 0, 1023, 0, 90);      
  myservo3.write(val2);                   
  delay(15); 

buttonState = digitalRead(buttonPin);  
if (buttonState == HIGH)
{
for(val4 = 0; val4 < 180; val4 += 1) // goes from 90 degrees to 110 degrees

{ // in steps of 1 degree

 myservo4.write(val4); // tell servo to go to position in variable ‘val4’

 delay(15); // waits 15ms for the servo to reach the position

}
} 

}

Moderator edit: CODE TAGS

It will also be helpful if myservo4 would rotate in the anti-clockwise direction.

Thank You

Hi,

No idea what you mean by not as expected, but if you mean that three servos do nothing while the forth is sweeping, the reason is that you enter a loop after the buttons is pressed. Your code stays in this loop for the full 2.7 seconds it takes your forth servo to sweep, only then do you exit the for loop and check the analog inputs.

Lots of ways to fix this, for a start, the loop function is already a loop, so you could get rid of the 'for' loop and just set var4 to 0 whenever the button is pressed. Then every time your code runs through the loop function, checking all your inputs and updating all your servos on the way, you can check to see if var4 is less than 180, if so add one and update your servo, if not, just carry on until the nex time the buttons is pressed, setting it back to 0 and so on and on ...

Duane B rcarduino.blogspot.com

[quote]
[color=#7E7E7E]/*  */[/color]
[color=#7E7E7E]// Constantes que no cambiarán[/color]
[color=#7E7E7E]//  configuran los números de los pines[/color]
const [color=#CC6600]int[/color] Detectordeinteriores = 44; 
const [color=#CC6600]int[/color] Detectordeexteriores = 45;
const [color=#CC6600]int[/color] Hacialaizquierda = 46;  
const [color=#CC6600]int[/color] Hacialaderecha = 47;
const [color=#CC6600]int[/color] Sentido=22;
const [color=#CC6600]int[/color] Marchaprimera=23;
const [color=#CC6600]int[/color] Marchasegunda=24;
const [color=#CC6600]int[/color] Marchatercera=25;
const [color=#CC6600]int[/color] Semaforo=26 ;
const [color=#CC6600]int[/color] Bocina=27;

[color=#7E7E7E]//variables que cambiarán se usa para indicar que el tren no está en la estación[/color]
[color=#7E7E7E]// utilizamos esta variable para diferenciar el centrado del tren de la llegada[/color]

[color=#CC6600]boolean[/color] Perdido=[color=#006699]LOW[/color];


[color=#CC6600]void[/color] [color=#CC6600][b]setup[/b][/color]() {
 
  [color=#7E7E7E]// configuramos los pines como salidas[/color]
 
  [color=#CC6600]pinMode[/color](Sentido, [color=#006699]OUTPUT[/color]);
  [color=#CC6600]pinMode[/color](Marchaprimera , [color=#006699]OUTPUT[/color]);
  [color=#CC6600]pinMode[/color](Marchasegunda, [color=#006699]OUTPUT[/color]);
  [color=#CC6600]pinMode[/color](Marchatercera, [color=#006699]OUTPUT[/color]);
  [color=#CC6600]pinMode[/color](Semaforo, [color=#006699]OUTPUT[/color]);
  [color=#CC6600]pinMode[/color](Bocina,[color=#006699]OUTPUT[/color]);

[color=#7E7E7E]//configuramos los pines como entradas[/color]
  
  [color=#CC6600]pinMode[/color](Detectordeinteriores, [color=#006699]INPUT[/color]);
  [color=#CC6600]pinMode[/color](Detectordeexteriores, [color=#006699]INPUT[/color]);
  [color=#CC6600]pinMode[/color](Hacialaizquierda, [color=#006699]INPUT[/color]);
  [color=#CC6600]pinMode[/color](Hacialaderecha, [color=#006699]INPUT[/color]);
  


  
}

[color=#CC6600]void[/color] [color=#CC6600][b]loop[/b][/color]()
{

  [color=#7E7E7E]//Si pulsamos el pulsador de la estación de la izquierda[/color]
  [color=#7E7E7E]//van entrando las marchas según el tiempo que pongamos en milisegundos[/color]
  
  [color=#CC6600]if[/color] ([color=#CC6600]digitalRead[/color](Hacialaderecha) == [color=#006699]HIGH[/color]) {  [color=#7E7E7E]//hacia la derecha   [/color]
    
    [color=#CC6600]digitalWrite[/color] (Sentido,[color=#006699]LOW[/color]); 
    Perdido= [color=#006699]HIGH[/color];
    [color=#CC6600]digitalWrite[/color](Marchaprimera, [color=#006699]HIGH[/color]);
    [color=#CC6600]digitalWrite[/color](Bocina,[color=#006699]HIGH[/color]);
    [color=#CC6600]digitalWrite[/color](Semaforo,[color=#006699]HIGH[/color]);
    [color=#CC6600]delay[/color](250);
    [color=#CC6600]digitalWrite[/color](Bocina,[color=#006699]LOW[/color]);
    [color=#CC6600]digitalWrite[/color](Marchasegunda,[color=#006699]HIGH[/color]);
    [color=#CC6600]delay[/color](250);
    [color=#CC6600]digitalWrite[/color](Marchatercera,[color=#006699]HIGH[/color]);
    [color=#CC6600]delay[/color](250);
    [color=#CC6600]digitalWrite[/color](Marchatercera,[color=#006699]LOW[/color]);
    


  } 
 [color=#7E7E7E]//Si en cualquiera de las dos estaciones llega el tren [/color]
 [color=#7E7E7E]//cuando se activa el detector de interiores se apaga la marcha segunda[/color]
 
  [color=#CC6600]if[/color] (([color=#CC6600]digitalRead[/color](Detectordeinteriores)==[color=#006699]HIGH[/color])&&([color=#CC6600]digitalRead[/color](Detectordeexteriores==[color=#006699]LOW[/color]))){[color=#7E7E7E]//llegada[/color]
      [color=#CC6600]if[/color] ((Perdido==[color=#006699]HIGH[/color])){
      [color=#CC6600]digitalWrite[/color](Marchasegunda, [color=#006699]LOW[/color]); 
      [color=#CC6600]digitalWrite[/color](Semaforo,[color=#006699]LOW[/color]);
      [color=#CC6600]digitalWrite[/color](Bocina, [color=#006699]HIGH[/color]);
     
     
     }
 }
[color=#7E7E7E]//cuando se activa el detector de exteriores se apaga la marcha primera y se deberá parar el tren[/color]

  [color=#CC6600]if[/color] (([color=#CC6600]digitalRead[/color](Detectordeexteriores)==[color=#006699]HIGH[/color])){[color=#7E7E7E]//llegada[/color]
      [color=#CC6600]if[/color](Perdido==[color=#006699]HIGH[/color]){
      [color=#CC6600]digitalWrite[/color](Marchaprimera,[color=#006699]LOW[/color]);
     Perdido=[color=#006699]LOW[/color];
      [color=#CC6600]digitalWrite[/color](Bocina, [color=#006699]LOW[/color]);
    }
  }
 [color=#7E7E7E]//centrado1: cuando el tren este en la estación se pueden dar 4 casos segun se encuentre activado [/color]
 [color=#7E7E7E]//los detectores y segun este el sentido. El programa deberá cambiar el sentido y activar la primera marcha[/color]
 [color=#7E7E7E]//para buscar estar en el punto que están activados los dos sensores[/color]
  [color=#CC6600]if[/color] (Perdido==[color=#006699]LOW[/color]){
    [color=#CC6600]if[/color](([color=#CC6600]digitalRead[/color](Detectordeinteriores)==[color=#006699]LOW[/color]) && ([color=#CC6600]digitalRead[/color](Detectordeexteriores)==[color=#006699]HIGH[/color])){
      
       
        [color=#CC6600]if[/color] ([color=#CC6600]digitalRead[/color](Sentido)==[color=#006699]LOW[/color]){
          
             [color=#CC6600]do[/color]{
             [color=#CC6600]digitalWrite[/color](Sentido,[color=#006699]HIGH[/color]);
             [color=#CC6600]delay[/color](20);
             [color=#CC6600]digitalWrite[/color](Marchaprimera,[color=#006699]HIGH[/color]);
             [color=#CC6600]delay[/color](100);
              }
             [color=#CC6600]while[/color]( (([color=#CC6600]digitalRead[/color](Detectordeinteriores)==[color=#006699]LOW[/color]) && ([color=#CC6600]digitalRead[/color](Detectordeexteriores)== [color=#006699]HIGH[/color])));
             
             
          }
        
    }
  }
  
 [color=#CC6600]if[/color] (Perdido==[color=#006699]LOW[/color]){[color=#7E7E7E]//centrado2[/color]
    [color=#CC6600]if[/color](([color=#CC6600]digitalRead[/color](Detectordeinteriores)==[color=#006699]HIGH[/color]) && ([color=#CC6600]digitalRead[/color](Detectordeexteriores)==[color=#006699]LOW[/color])){
      
   
         [color=#CC6600]if[/color]([color=#CC6600]digitalRead[/color](Sentido==[color=#006699]HIGH[/color]))
          {
          
          [color=#CC6600]do[/color]{
          [color=#CC6600]digitalWrite[/color](Sentido,[color=#006699]LOW[/color]);
          [color=#CC6600]delay[/color](20);
          [color=#CC6600]digitalWrite[/color](Marchaprimera,[color=#006699]HIGH[/color]);
          [color=#CC6600]delay[/color](100);
          }
          [color=#CC6600]while[/color]( (([color=#CC6600]digitalRead[/color](Detectordeinteriores)==[color=#006699]HIGH[/color]) && ([color=#CC6600]digitalRead[/color](Detectordeexteriores)== [color=#006699]LOW[/color])));
          
                  
        
          }
          
         
        
      }
    
  }
  
  [color=#7E7E7E]//parado cuando estan activados los dos sensores es donde el tren debe parar[/color]
    [color=#CC6600]if[/color]([color=#CC6600]digitalRead[/color](Detectordeinteriores)==[color=#006699]HIGH[/color]){
      [color=#CC6600]if[/color]([color=#CC6600]digitalRead[/color](Detectordeexteriores)==[color=#006699]HIGH[/color]){
        [color=#CC6600]digitalWrite[/color](Marchaprimera,[color=#006699]LOW[/color]);
         
     
        
       
        
      }
    }
  
  

  [color=#7E7E7E]// check if the pushbutton is pressed.[/color]
  [color=#7E7E7E]// if it is, the buttonState is HIGH:[/color]
  [color=#CC6600]if[/color] ([color=#CC6600]digitalRead[/color](Hacialaizquierda)==[color=#006699]HIGH[/color]) {     
    [color=#7E7E7E]// turn LED on: [/color]
    [color=#CC6600]digitalWrite[/color] (Sentido,[color=#006699]HIGH[/color]); 
     
    [color=#CC6600]digitalWrite[/color](Marchaprimera, [color=#006699]HIGH[/color]);  
    [color=#CC6600]digitalWrite[/color](Bocina,[color=#006699]HIGH[/color]);
    [color=#CC6600]digitalWrite[/color](Semaforo,[color=#006699]HIGH[/color]);   
    Perdido= [color=#006699]HIGH[/color];
    [color=#CC6600]delay[/color](250);
    [color=#CC6600]digitalWrite[/color](Bocina,[color=#006699]LOW[/color]);
    [color=#CC6600]digitalWrite[/color](Marchasegunda,[color=#006699]HIGH[/color]);
    [color=#CC6600]delay[/color](250);
    [color=#CC6600]digitalWrite[/color](Marchatercera,[color=#006699]HIGH[/color]);
    [color=#CC6600]delay[/color](250);
    [color=#CC6600]digitalWrite[/color](Marchatercera,[color=#006699]LOW[/color]);
  } 

 
  [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color](Perdido,[color=#006699]DEC[/color]);
  
}[color=#7E7E7E]//main[/color]



[/quote]