Two servos whith two buttons.

Hello,
i found this Code but wen i fill it whith the Servo 2 and Switch2 Code an error is coming called:
'checkSwitches' was not declared in this scope
I want to control switch 1 for servo 1 and switch 2 for servo 2.

Any Ideas?

Thanks for help
Sven from Germany

#include <Servo.h>

// create servo object to control a servo
// twelve servo objects can be created on most boards
Servo myservo1;
Servo myservo2;

const byte switch1      = 2;    //switch press = LOW
const byte switch2      = 3;    //switch press = LOW
const byte heartBeatLED = 13;

byte cycleServo1Flag    = 0;    //= 0 stopped, = 1 going to 180, = 2 going to 0
byte cycleServo2Flag    = 0;    //= 0 stopped, = 1 going to 180, = 2 going to 0
byte lastSwitch1;               //current switch state
byte lastSwitch2;               //current switch state
byte servoDelay         = 15;   //15ms

int servo1Pos;
int servo2Pos;

//timing variables
unsigned long MillisHeartBeat;
unsigned long testSwitchMillis;
unsigned long servoMillis;

//**********************************************************************************************
void setup()
{
  myservo1.attach(8);
  myservo1.write(servo1Pos);

  myservo2.attach(9);
  myservo2.write(servo2Pos);

  pinMode(heartBeatLED, OUTPUT);

  pinMode(switch1, INPUT_PULLUP);         //push = LOW
  lastSwitch1 = digitalRead(switch1);

  pinMode(switch2, INPUT_PULLUP);         //push = LOW
  lastSwitch2 = digitalRead(switch2);

} //END of setup()

//**********************************************************************************************
void loop()
{
  //************************
  //code to see if the sketch is blocking, LED will toggle every 500ms
  if (millis() - MillisHeartBeat >= 500)
  {
    MillisHeartBeat = millis();

    //toggle LED state
    digitalWrite(heartBeatLED, !digitalRead(heartBeatLED));
  }

  //************************
  //time to check the switches?
  if (millis() - testSwitchMillis >= 50)
  {
    testSwitchMillis = millis();
    checkSwitches();
  }

  //************************
  //run at loop speed
  checkServos();


  //************************
  //none blocking code here
  //************************


}  //END of loop()

//                                c h e c k S e r v o s ( )
//**********************************************************************************************
void checkServos()
{
  //************************
  //servo #1
  //cycleServo1Flag = 0 stopped, = 1 going to 180, = 2 going to 0
  //if enabled, has servoDelay ms gone by?
  if (cycleServo1Flag == 1 && millis() - servoMillis >= servoDelay)
  {
    //reset timer
    servoMillis = millis();

    myservo1.write(servo1Pos);
    servo1Pos++;

    //only go to 180
    if (servo1Pos > 180)
    {
      //at 180 now get ready to go lower
      servo1Pos = 179;

      //change to other direction
      cycleServo1Flag = 2;
    } //finished going to 180

  } //if

  //if enabled, has servoDelay ms gone by?
  else if (cycleServo1Flag == 2 && millis() - servoMillis >= servoDelay)
  {
    //reset timer
    servoMillis = millis();

    myservo1.write(servo1Pos);
    servo1Pos--;
    if (servo1Pos < 0)
    {
      //stop servo movement
      cycleServo1Flag = 0;
    } //finished going to 0

  } //else if

  //************************
  //servo #2
  //cycleServo2Flag = 0 stopped, = 1 going to 180, = 2 going to 0
  //add your second servo code here

  

} //END of checkServos()


//                              c h e c k S w i t c h e s ( )
//**********************************************************************************************
void checkSwitches()
{
  //************************
  //switch #1
  byte currentState = digitalRead(switch1);

  //has this switch position changed?
  if (lastSwitch1 != currentState)
  {
    //update to the new state
    lastSwitch1 = currentState;

    //was the switch just pushed?
    if (lastSwitch1 == LOW)
    {
      //allow servo 1 movment
      cycleServo1Flag = 1;

      //start at 0 degrees
      servo1Pos = 0;
    }
    
  } //if

  //************************
  //switch #2
  currentState = digitalRead(switch2);

  //has this switch position changed?
  if (lastSwitch2 != currentState)
  {
    //update to the new state
    lastSwitch2 = currentState;

    //add your second switch code here

  } //if

} //END checkSwitches()

//**********************************************************************************************

It compiles fine for me for an Uno, IDE 1.6.12.

Yes the Sketch works but i have a Problem to Add Servo 2 and Switch 2 into the Sketch
this one here controll 1 servo with 1 button

Post the version that fails, so we can see what you've done wrong!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.