2 Servos 2 Switches Independetly

Hi there

I'm still new to Arduino and cannot find relevant code to execute the following;

  • Operate two servos independently - lets call it servo A and servo B

  • The two servos must be connected to a non-latch switch A and B. Thus switch A controls servo A and switch B controls servo B

  • Once the switch (A or B) is depressed and released the servo (A or B) must turn from 0 to180 degrees then back to 0 degree (the 0 - 180 - 0 is completed with only a single switch toggle

  • once servo returns to 0 degree it waits for the next switch toggle

I trust someone will be able to assist. I tried hybridizing various code but to no avail. Thanking you in advance

Andre

Hi Magic

Here is some code i wrote that works with two servos but has a lot of extra’s… You use it as basis for your code. If you need further clarification please reply to my PM… :slight_smile:

#include <Servo.h>
// create servo objects to control servos
Servo servo1;
Servo servo2;
int servo1Pos = 0;
int servo2Pos = 0;
//create  button primitives for the push buttons
const int buttonPin1 = 2;     // the number of the pushbutton pin
const int buttonPin2 = 3;     // the number of the pushbutton pin
const int buttonPin3 = 4;     // the number of the pushbutton pin

int relayOn = 11;
//define pot pin
int potpin = 0;  // analog pin used to connect the potentiometer
//define relay pins
const int relayPin1 = 5;     // the number of the relay pin- solid state
const int relayPin2 = 6;     // the number of the relay pin
const int relayPin3 = 7;     // the number of the relay pin
const int relayPin4 = 8;     // the number of the relay pin

//delays for button , pot and relay1 in ms
int buttonDelay = 1000;
int relay1Delay = 500;
int potDelay = 1000; // easiest way to smooth pot is have read interval

// variables to store inputs
int val = 0;
int val2 = 0;
int buttonVal1;
int buttonVal2;
int buttonVal3;
int previousButton;
//For realtime response we can't use delay.. we use millis
//https://www.arduino.cc/reference/en/language/functions/time/millis/
unsigned long timeNow;
unsigned long potTime;
unsigned long relay1Time;

boolean executeLock = false;
void setup() {
  // attach servos
  servo2.attach(9);
  servo1.attach(10);

  //INPUT PINS
  // Keep in mind the pull-up means the pushbutton's logic is inverted. It goes
  // HIGH when it's open, and LOW when it's pressed.
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(buttonPin3, INPUT_PULLUP);

  //OUTPUT PINS
  pinMode(relayPin1, OUTPUT);
  pinMode(relayPin2, OUTPUT);
  pinMode(relayPin3, OUTPUT);
  pinMode(relayPin4, OUTPUT);
  digitalWrite(relayPin1, HIGH);
  digitalWrite(relayPin3, HIGH);
  digitalWrite(relayPin2, HIGH);
  digitalWrite(relayPin3, HIGH);

  timeNow = potTime = relay1Time = 0;
  Serial.begin(9600);
  //set to neutral gear
  previousButton = 2;
  neutralGear();
}

void loop() {
  if (potTime == 0 )
  {
    potTime = millis();
  }
  val2 = analogRead(potpin);// reads the value of the potentiometer (value between 0 and 1023)
  Serial.print("Raw pot value is ");
  Serial.println(val2);
  val2 = map(val2, 0, 1023, 0, 100); //map to percentage
  Serial.print("% pot value  ");
  Serial.print(val2);
  Serial.print("since potentionmeter value is ");

  /*
    pot value is higher than 5% s
    //for pot and servo 2
  */
  if (val2  > 5 ) {
    Serial.println("above 5% buttons are disabled");
    executeLock = true;
    val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
    Serial.println("Pot code exectuting  ");
    val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
    int sdelay=map(val,0,180,0,420);
    Serial.print("Servo to turn in degrees:  ");
    Serial.println(val);
    if (val != servo1Pos)
    {
      servo1.write(180);                  // sets the servo position according to the scaled value
      // waits for the servo to get there
      delay(sdelay);
      servo1Pos = val;
    }
    potTime = 0;
    executeLock = false;
    //return the servo home
    //servo.write(0);
    delay(100);
  }
  /*
    pot value is higher than 5% s
    //for pot and servo 2
  */
  else {
    Serial.println("Below 5% buttons are enabled");
    buttonVal1 = !digitalRead(buttonPin1);

    /**
       Button 1
       turn off all relays
       turns servo 1 180 degrees
       turns on relay one for delay specified
       turns on relay 2
    */
    if (buttonVal1 == HIGH )
    {
      //check if the previous state is acceptable.. either re
      if ( previousButton == 2)
      {
        forwardGear();
      }
    }

    /**
       Button 2
       turn off all relays
       turns servo 1 90 degrees
       turns on relay one for delay specified
       turns on relay 3
    */
    buttonVal2 = !digitalRead(buttonPin2);
    if (buttonVal2 == HIGH )
    {
      //check if the previous state is acceptable.. either re
        neutralGear();
    }
    /**
       Button 3
       turn off all relays
       turns on relay one for delay specified
       turns on relay 4
    */
    buttonVal3   = !digitalRead(buttonPin3);


    if (buttonVal3 == HIGH )
    {
      if (previousButton == 2)
      {
        reverseGear();
      }
    }

  }

}
int moveServo2ToPos(int position_target)
{
  int sdelay=map(position_target,0,180,0,420);
 
  if (position_target != servo2Pos)
  {
    servo2.write(180);
    delay(sdelay);
    servo2Pos = position_target;
    return 1;
  }
  else
  {
    return 0;
  }
  return -1;
}
void forwardGear()
{
  executeLock = true;
  moveServo2ToPos(180);
  digitalWrite(relayPin1, LOW);
  digitalWrite(relayPin3, HIGH);
  Serial.println(" Relay 2 ON ");
  digitalWrite(relayPin2, LOW);
  relayOn = relayPin2;
  previousButton = 1;
  delay(relay1Delay);
      digitalWrite(relayPin2, HIGH);
  //delay for relay 1
  Serial.println(" Relay 1 off ");
  digitalWrite(relayPin1, HIGH);
  relay1Time = 0;
  executeLock = false;
  delay(500);
  buttonVal1 = LOW;
}
void neutralGear()
{
  executeLock = true;
  moveServo2ToPos(90);
  digitalWrite(relayPin1, LOW);
  digitalWrite(relayPin4, HIGH);
  digitalWrite(relayPin2, HIGH);
  Serial.println(" Relay 2 ON ");
  digitalWrite(relayPin3, LOW);
  relayOn = relayPin3;
  previousButton = 2;
  delay(relay1Delay);
    digitalWrite(relayPin3, HIGH);

  //delay for relay 1
  Serial.println(" Relay 1 off ");
  digitalWrite(relayPin1, HIGH);
  relay1Time = 0;
  executeLock = false;
  delay(500);
  buttonVal2 = LOW;
}
void reverseGear()
{
  executeLock = true;
  moveServo2ToPos(0);
  digitalWrite(relayPin1, LOW);
  digitalWrite(relayPin3, HIGH);
  Serial.println(" Relay 3 ON ");
  digitalWrite(relayPin4, LOW);
  relayOn = relayPin4;
  previousButton = 3;
  delay(relay1Delay);
  digitalWrite(relayPin4, HIGH);
  //delay for relay 1
  Serial.println(" Relay 1 off ");
  digitalWrite(relayPin1, HIGH);
  relay1Time = 0;
  executeLock = false;
  delay(500);
  buttonVal3 = LOW;
}