I'm making a useless box with 5 switches. The switches need to be closed by the box in the same order they are switched on by the user.
The switches in the code shown below do not switch off in the same order they are switched on. For your understanding, I deleted anything that had to do with servo 2, since only servo 1 is responsible for moving to the switch position.
#include <Servo.h>
const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int buttonPin4 = 5;
const int buttonPin5 = 6;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
Servo myservo1;
int pos1=0;
void setup()
{
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);
myservo1.attach(9); //servo 1 on pin 9 (bottom servo)
myservo1.write(pos1); //set servo to 0 position
}
void loop()
{
buttonState1 = digitalRead(buttonPin1); //read all switches
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);
buttonState5 = digitalRead(buttonPin5);
if (buttonState1 == HIGH){ //move to switch position if switch is on
myservo1.write(36);
delay(1500);
}
else if (buttonState2 == HIGH){ //move to switch position if switch is on
myservo1.write(72);
delay(1500);
}
else if (buttonState3 == HIGH){ //move to switch position if switch is on
myservo1.write(108);
delay(1500);
}
else if (buttonState4 == HIGH){ //move to switch position if switch is on
myservo1.write(144);
delay(1500);
}
else if (buttonState5 == HIGH){ //move to switch position if switch is on
myservo1.write(180);
delay(1500);
}
}
To try and solve this, I made an array that saves whenever a switch is on, but this doesn't work at all and I don't know why. I feel like I'm close to the solution but I can't get this to work >.<
#include <Servo.h>
const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int buttonPin4 = 5;
const int buttonPin5 = 6;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
Servo myservo1;
int pos1=0;
void setup()
{
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);
myservo1.attach(9); //servo 1 on pin 9 (bottom servo)
myservo1.write(pos1); //set servos to 0 position
}
int order[100];
int i=0; //counter
void checker(){
void loop()
{
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);
buttonState5 = digitalRead(buttonPin5);
if (buttonState1){
order[i]=30;
i++;
}
else if (buttonState2){
order[i]=60;
i++;
}
else if (buttonState3){
order[i]=90;
i++;
}
else if (buttonState4){
order[i]=120;
i++;
}
else if (buttonState4){
order[i]=150;
i++;
}
myservo1.write(order[i-1]);
delay(500);
}