Code doesn’t work as intended

Hi, I can’t solve an issue with my code. The logic is as follows: I have a 4-position switch, that changes different modes of a gearbox model and 4 servo motors, when I change modes it goes trough every gear and then tries to select N or R even if they aren’t selected or when “R” is selected, it goes between N and R. Any help would be appreciated. The file is on Google Drive with a link: https://drive.google.com/file/d/12FCm_NrZklpRwqYwvjPDwYg9I56g8hTI/view?usp=drivesdk . Thank you.

Could you just paste the code as text?

Sure, one moment.

#include <SoftwareSerial.h>
#include <Servo.h>
Servo clutch1_2;
Servo clutch3;
Servo clutch4;
Servo clutch5;
const int GearR_Switch = 10;
const int GearN_Switch = 12;
const int GearD_Switch = 11;
const int GearM_Switch = 8;
const int GearPlus_Button = 4;
const int GearMinus_Button = 5;

int GearR_Switch_State = 0;
int GearN_Switch_State = 0;
int GearD_Switch_State = 0;
int GearM_Switch_State = 0;
int GearPlus_Button_State = 0;
int GearMinus_Button_State = 0;

void setup() {
clutch1_2.attach(9);
clutch3.attach(7);
clutch4.attach(6);
clutch5.attach(3);
Serial.begin(9600);
pinMode(GearR_Switch, INPUT);
pinMode(GearN_Switch, INPUT);
pinMode(GearD_Switch, INPUT);
pinMode(GearM_Switch, INPUT);
pinMode(GearPlus_Button, INPUT);
pinMode(GearMinus_Button, INPUT);

}

void loop() {
GearR_Switch_State = digitalRead(GearR_Switch);
GearN_Switch_State = digitalRead(GearN_Switch);
GearD_Switch_State = digitalRead(GearD_Switch);
GearM_Switch_State = digitalRead(GearM_Switch);
GearPlus_Button_State = digitalRead(GearPlus_Button);
GearMinus_Button_State = digitalRead(GearMinus_Button);

if(GearD_Switch_State == 1){
GearR_Switch_State = 0;
GearN_Switch_State = 0;
GearM_Switch_State = 0;
Gear1();
delay(500);
Gear2();
delay(500);
Gear3();
delay(500);
Gear4();
delay(500);
Gear5();
delay(500);
Gear6();
delay(2000);
};

if(GearR_Switch_State == 1){
GearN_Switch_State = 0;
GearD_Switch_State = 0;
GearM_Switch_State = 0;
GearR();
};

if(GearN_Switch_State == 1){
GearR_Switch_State = 0;
GearD_Switch_State = 0;
GearM_Switch_State = 0;
GearN();
};

// if(GearM_Switch_State == HIGH){
// };
}

void Gear1(){
clutch1_2.write(45);
clutch3.write(140);
clutch4.write(100);
clutch5.write(0);
Serial.println("Gear 1");
}

void Gear2(){
clutch1_2.write(45);
clutch3.write(140);
clutch4.write(0);
clutch5.write(115);
Serial.println("Gear 2");
}

void Gear3(){
clutch1_2.write(45);
clutch3.write(40);
clutch4.write(100);
clutch5.write(115);
Serial.println("Gear 3");
}

void Gear4(){
clutch1_2.write(135);
clutch3.write(140);
clutch4.write(100);
clutch5.write(115);
Serial.println("Gear 4");
}

void Gear5(){
clutch1_2.write(135);
clutch3.write(40);
clutch4.write(100);
clutch5.write(115);
Serial.println("Gear 5");
}

void Gear6(){
clutch1_2.write(135);
clutch3.write(140);
clutch4.write(0);
clutch5.write(115);
Serial.println("Gear 6");
}

void GearR(){
clutch1_2.write(90);
clutch3.write(40);
clutch4.write(100);
clutch5.write(0);
Serial.println("Gear R");
}

void GearN(){
clutch1_2.write(90);
clutch3.write(140);
clutch4.write(100);
clutch5.write(115);
Serial.println("Gear N");
}

Could you provide information on the 4-position switch? You may need pull up or down depending on the configuration of the switch.

It’s a simple 4-position slider switch with 5 contact points. I have tried to use “INPUT_PULLUP” but it didn’t change anything. I have it connected to 5V on Arduino board to the middle point and 4 wires are coming back to the digital pins.

This isn't an Arduino forum, it's for Particle devices. And all current Particles devices are not 5V tolerant, so connecting the switch to 5V will permanently damage the device.

However, if your switch looks like this, then you need INPUT_PULLDOWN because when the pole is unconnected it needs to be LOW, not high or floating.

Sorry, didn’t know about that, thanks for help, I will try right now.