Switch selection with buttons

Hello guys,

So, i wanna make this 3axis robotic arm.
Unfortunately, i don’t have joysticks. So i came out with this solution: 3 button to select the axis you want to move and a potentiometer to move.

I am experiencing problems with the selection part. So ideally, you would press a button, it would light up one of the three LEDs and the potentiometer would control the selected motor/axis. In my first version (messy af code) i got a do while loop in a switch to get the program controlling only one motor without running through the whole loop (and i think this is the way to go). But, when i pressed a button, it didn’t switch to controlling the other motor.

I feel like i am getting really close to the final, but this switching part isn’t working :confused: >:(

Anyone here got a solution ?

Thanks a lot ^^

Here is ver1 of the code

#include <Servo.h>
#include <Stepper.h>
#define STEPS 4096

Servo servo1;
Servo servo2;
Stepper stepper(STEPS, 2, 3, 4, 5);

int btn1 = 8;
int btn2 = 9;
int btn3 = 10;

int led1 = 11;
int led2 = 12;
int led3 = 13;

int servo1Position = 0;
int servo2Position = 0;

int btn1State = 0;
int btn2State = 0;
int btn3State = 0;

int previous = 0;

int switchstate = 0;


void setup() {
  servo1.attach(6);
  servo2.attach(7);
  pinMode(btn1, INPUT_PULLUP);
  pinMode(btn2, INPUT_PULLUP);
  pinMode(btn3, INPUT_PULLUP);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  Serial.begin(115200);
}

void loop() {
  btn1State = digitalRead(btn1);
  btn2State = digitalRead(btn2);
  btn3State = digitalRead(btn3);
  if (btn1State == LOW && btn2State == HIGH && btn3State == HIGH) {
    switchstate = 1;
  } else if (btn1State == HIGH && btn2State == LOW && btn3State == HIGH) {
    switchstate = 2;
  } else if (btn1State == HIGH && btn2State == HIGH && btn3State == LOW) {
    switchstate = 3;
  } else {
    switchstate = switchstate;
  }
  switch (switchstate) {
    case 1:
      digitalWrite(led1, HIGH);
      digitalWrite(led2, LOW);
      digitalWrite(led3, LOW);
      Serial.println("1");
      int val = analogRead(A0);
      val = map(val, 0, 1023, 0, 180);
      servo1.write(val);
      delay(15);
      break;

    case 2:
      digitalWrite(led1, LOW);
      digitalWrite(led2, HIGH);
      digitalWrite(led3, LOW);
      Serial.println("2");
      int val = analogRead(A0);
      val = map(val, 0, 1023, 0, 180);
      servo2.write(val);
      delay(15);
      break;

    case 3:
      digitalWrite(led1, LOW);
      digitalWrite(led2, LOW);
      digitalWrite(led3, HIGH);
      Serial.println("3");
      int val = analogRead(A0);
      stepper.step(val - previous);
      previous = val;
      break;
  }
  delay(1000);
}

And here is ver2

#include <Servo.h>
#include <Stepper.h>
#define STEPS 4096

Servo servo1;
Servo servo2;
Stepper stepper(STEPS, 2, 3, 4, 5);

int btn1 = 8;
int btn2 = 9;
int btn3 = 10;

int led1 = 11;
int led2 = 12;
int led3 = 13;

int servo1Position = 0;
int servo2Position = 0;

int btn1State = 0;
int btn2State = 0;
int btn3State = 0;

int previous = 0;

int switchstate = 0;


void setup() {
  servo1.attach(6);
  servo2.attach(7);
  pinMode(btn1, INPUT_PULLUP);
  pinMode(btn2, INPUT_PULLUP);
  pinMode(btn3, INPUT_PULLUP);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  Serial.begin(115200);
}

void loop() {
  btn1State = digitalRead(btn1);
  btn2State = digitalRead(btn2);
  btn3State = digitalRead(btn3);
  if (btn1State == LOW && btn2State == HIGH && btn3State == HIGH) {
    switchstate = 1;
  } else if (btn1State == HIGH && btn2State == LOW && btn3State == HIGH) {
    switchstate = 2;
  } else if (btn1State == HIGH && btn2State == HIGH && btn3State == LOW) {
    switchstate = 3;
  } else {
    switchstate = switchstate;
  }
  switch (switchstate) {
    case 1:
      digitalWrite(led1, HIGH);
      digitalWrite(led2, LOW);
      digitalWrite(led3, LOW);
      Serial.println("1");
      int val = analogRead(A0);
      val = map(val, 0, 1023, 0, 180);
      servo1.write(val);
      delay(15);
      break;

    case 2:
      digitalWrite(led1, LOW);
      digitalWrite(led2, HIGH);
      digitalWrite(led3, LOW);
      Serial.println("2");
      int val = analogRead(A0);
      val = map(val, 0, 1023, 0, 180);
      servo2.write(val);
      delay(15);
      break;

    case 3:
      digitalWrite(led1, LOW);
      digitalWrite(led2, LOW);
      digitalWrite(led3, HIGH);
      Serial.println("3");
      int val = analogRead(A0);
      stepper.step(val - previous);
      previous = val;
      break;
  }
  delay(1000);
}

What’s not working in version 2?

Well, when i press a button, it doesn't switch to the other LED and motor :confused:

Does the Serial monitor print the correct values?

it doesn't print anything :confused:

Does the sketch compile without errors?

Yup

Things don’t compile here without errors on version 2

This compiles and works:

#include <Servo.h>
#include <Stepper.h>
#define STEPS 4096

Servo servo1;
Servo servo2;
Stepper stepper(STEPS, 2, 3, 4, 5);

int btn1 = 8;
int btn2 = 9;
int btn3 = 10;

int led1 = 11;
int led2 = 12;
int led3 = 13;

int servo1Position = 0;
int servo2Position = 0;

int btn1State = 0;
int btn2State = 0;
int btn3State = 0;

int previous = 0;

int switchstate = 0;

int val;

void setup()
{
  servo1.attach(6);
  servo2.attach(7);
  pinMode(btn1, INPUT_PULLUP);
  pinMode(btn2, INPUT_PULLUP);
  pinMode(btn3, INPUT_PULLUP);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  Serial.begin(115200);
}

void loop()
{
  btn1State = digitalRead(btn1);
  btn2State = digitalRead(btn2);
  btn3State = digitalRead(btn3);

  if (btn1State == LOW && btn2State == HIGH && btn3State == HIGH)
  {
    switchstate = 1;
  }

  else if (btn1State == HIGH && btn2State == LOW && btn3State == HIGH)
  {
    switchstate = 2;
  }

  else if (btn1State == HIGH && btn2State == HIGH && btn3State == LOW)
  {
    switchstate = 3;
  }

  else
  {
    switchstate = switchstate;
  }

  switch (switchstate)
  {
    case 1:
      digitalWrite(led1, HIGH);
      digitalWrite(led2, LOW);
      digitalWrite(led3, LOW);
      Serial.println("1");
      val = analogRead(A0);
      val = map(val, 0, 1023, 0, 180);
      servo1.write(val);
      delay(15);
      break;

    case 2:
      digitalWrite(led1, LOW);
      digitalWrite(led2, HIGH);
      digitalWrite(led3, LOW);
      Serial.println("2");
      val = analogRead(A0);
      val = map(val, 0, 1023, 0, 180);
      servo2.write(val);
      delay(15);
      break;

    case 3:
      digitalWrite(led1, LOW);
      digitalWrite(led2, LOW);
      digitalWrite(led3, HIGH);
      Serial.println("3");
      val = analogRead(A0);
      stepper.step(val - previous);
      previous = val;
      break;
  }

  delay(1000);

}

Ok, thanks for that ^^
but i still don't know how to do that "selection" part (with the buttons) :slight_smile:

The code offered in post #7 works here, i.e. the switches, serial monitor and the LED portion.

Don’t have the motors so you will have to check those.

Oh f*ck myself lmao :slight_smile: :slight_smile: :smiley:

Thank you very much :slight_smile: ^^