10 Switch Useless Machine Help with Code

Hi All
I have made a Useless Machine with a printer (DC motor with encoder) and a Servo.
I need some help with making the code
The way i want the useless machine to work is the order you turn the switches on it will turn them off.
So i need to track the position of 10 switches and turn them off in the right order.
I know of a ring buffer but i have no idea how to use it.
I dont no much so take it easy on me.

My useless machine video My Machine
This is my code for 5 switches (left to right).

int Switch1 = 22;
int Switch2 = 23;
int Switch3 = 24;
int Switch4 = 25;
int Switch5 = 26;

int LimitSwitch = 32;
int DcMotorOn = 8;        //Motor On - Off
int DcMotorDir1 = 9;      //Motor Direction (Dir1 LOW + Dir2 HIGH = Move Right)
int DcMotorDir2 = 10;     //Motor Direction (Dir1 HIGH + Dir2 LOW = Move Left)
#define encoderI 2
#define encoderQ 3
int Distance = 0;         //Distance on linear encoder
int ServoAngle = 170;     //ServoAngle
int DistanceAcc;          //Distance for acceleration / deceleration
int ValTwo = HIGH;

boolean LastSwitch1 = LOW;
boolean FakeSwitch = LOW;

#include <Servo.h> 
Servo Servo1; 
int pos = 0;

void setup() {

Serial.begin(9600);
Servo1.attach(11);
Servo1.write(180);   
pinMode(encoderI, INPUT);
pinMode(encoderQ, INPUT); attachInterrupt(0, handleEncoder, CHANGE);
pinMode(LimitSwitch, INPUT);
pinMode(Switch1, INPUT);
pinMode(DcMotorOn, OUTPUT);
pinMode(DcMotorDir1, OUTPUT);
pinMode(DcMotorDir2, OUTPUT);
digitalWrite(DcMotorOn, HIGH);
analogWrite(DcMotorDir1, 160); 
digitalWrite(DcMotorDir2, LOW); 
 
}


void loop() {

if (digitalRead(LimitSwitch) == HIGH) {  // Moves the Arm to the far rigth and sets the Distance to -150
    digitalWrite(DcMotorDir1, LOW); 
    Distance = -150;
    }

if (digitalRead(Switch1) == HIGH) {                     //Switch 1
    FlipSwitch (2870);
    }
if (digitalRead(Switch2) == HIGH) {                       //Switch 2
    FlipSwitch (2570);
    }
if (digitalRead(Switch3) == HIGH) {                        //Switch 3
    FlipSwitch (2250);
    }
if (digitalRead(Switch4) == HIGH) {                      //Switch 4
    FlipSwitch (1900);
    }
if (digitalRead(Switch5) == HIGH) {                     //Switch 5 
    FlipSwitch (1600);
    }

 
 
 
Serial.println(Distance);

}


void FlipSwitch (int ValOne) {                 // ValOne = distance to switch


    DistanceAcc = Distance;                                          
    
if (DistanceAcc < ValOne-600 && ValTwo == HIGH) {                               //Step 1  Set ValOne to teh distance with deceleration of 600 units (2870-2270=600)
    digitalWrite(DcMotorDir1, LOW); 
    analogWrite(DcMotorDir2, 255);
    }
if (DistanceAcc > ValOne-600 && ValTwo == HIGH) {                               //Step 2 Start deceleration from ValOne - 600 to ValOne Units
    digitalWrite(DcMotorDir1, LOW);
    int DcMotorAcc = map(DistanceAcc, ValOne-600, ValOne, 255, 120);
    analogWrite(DcMotorDir2, DcMotorAcc);
    }
       
if (Distance > ValOne && ValTwo == HIGH) { 
     digitalWrite(DcMotorDir1, LOW);                                           //Step 3 If > ValOne turn motor off
     digitalWrite(DcMotorDir2, LOW); 
     Servo1.write(85);                          
     delay(150);                                                               //Step 4 Move arm up to hit switch
     Servo1.write(170);                                                        //Step 5 Switch hs been hit. Move arm back down
     delay(130);                                           
     ValTwo = LOW;
     digitalWrite(DcMotorDir1, HIGH); 
     digitalWrite(DcMotorDir2, LOW);
   
 }
       
if (Distance < 1500 && ValTwo == LOW) {
                                                             //Step 8 Stop arm from moving right. (ready for step 1 to start over)
      digitalWrite(DcMotorDir1, LOW);
      digitalWrite(DcMotorDir2, LOW);
      ValTwo = HIGH;
    }
    

}


void handleEncoder() {
   if(digitalRead(encoderI) == digitalRead(encoderQ))                      //Reads the encoder = Distance
   { Distance ++;
   }
   else
   { Distance --;
   }
}

First, how will the machine know when you've finished flipping switches?
You will probably need to keep track of time.

Why are your switches at different distances from each other?
If they were equispaced, the coding would be much simpler.

They are all the same distances. It would still work if a changed the the code to 300 units form switch to switch. What ideas do you have?

jremington:
First, how will the machine know when you've finished flipping switches?
You will probably need to keep track of time.

I dont think the machine needs to know that. all it needs to know is what one to turn off and in what order.

Slowman223:
They are all the same distances. It would still work if a changed the the code to 300 units form switch to switch. What ideas do you have?

If they are all equidistant (300 units) then you can work out the distance to each switch by something like
Pseudocode:

move stepper to (base position  + (300 * switchNumber)); //base position is where (imaginary?) switch 0 would be/is

You'd have to use an array for the switch numbers.
NB. Do not use 'switch' as a variable name as it's a reserved word.

ok i have no idea what a array looks like.
int myArray[10] = {1,2,3,4,5,6,7,8,9,10};
like that?

i dont mind keeping it that way it is.
i just want to see if i can add a ring buffer to it.
got no idea how tho

I have been playing around with deceleration for when it goes back to home.
this is what i got, but no luck

int Switch1 = 22;
int Switch2 = 23;
int Switch3 = 24;
int Switch4 = 25;
int Switch5 = 26;

int LimitSwitch = 32;
int DcMotorOn = 8;        //Motor On - Off
int DcMotorDir1 = 9;      //Motor Direction (Dir1 LOW + Dir2 HIGH = Move Right)
int DcMotorDir2 = 10;     //Motor Direction (Dir1 HIGH + Dir2 LOW = Move Left)
#define encoderI 2
#define encoderQ 3
int Distance = 0;         //Distance on linear encoder
int ServoAngle = 170;     //ServoAngle
int DistanceAcc;          //Distance for acceleration / deceleration
int ValTwo = HIGH;

boolean LastSwitch1 = LOW;
boolean FakeSwitch = LOW;

#include <Servo.h> 
Servo Servo1; 
int pos = 0;

void setup() {

Serial.begin(9600);
Servo1.attach(11);
Servo1.write(180);   
pinMode(encoderI, INPUT);
pinMode(encoderQ, INPUT); attachInterrupt(0, handleEncoder, CHANGE);
pinMode(LimitSwitch, INPUT);
pinMode(Switch1, INPUT);
pinMode(DcMotorOn, OUTPUT);
pinMode(DcMotorDir1, OUTPUT);
pinMode(DcMotorDir2, OUTPUT);
digitalWrite(DcMotorOn, HIGH);
analogWrite(DcMotorDir1, 160); 
digitalWrite(DcMotorDir2, LOW); 

}


void loop() {

if (digitalRead(LimitSwitch) == HIGH) {  // Moves the Arm to the far rigth and sets the Distance to -150
    digitalWrite(DcMotorDir1, LOW); 
    Distance = -150;
    }

if (digitalRead(Switch1) == HIGH) {                     //Switch 1
    FlipSwitch (2870);
    }
if (digitalRead(Switch2) == HIGH) {                       //Switch 2
    FlipSwitch (2570);
    }
if (digitalRead(Switch3) == HIGH) {                        //Switch 3
    FlipSwitch (2250);
    }
if (digitalRead(Switch4) == HIGH) {                      //Switch 4
    FlipSwitch (1900);
    }
if (digitalRead(Switch5) == HIGH) {                     //Switch 5 
    FlipSwitch (1600);
    }




//Serial.println(Distance);

}


void FlipSwitch (int ValOne) {                 // ValOne = distance to switch


    DistanceAcc = Distance;                                          
    
if (DistanceAcc < ValOne-600 && ValTwo == HIGH) {                               //Step 1  Set ValOne to teh distance with deceleration of 600 units (2870-2270=600)
    digitalWrite(DcMotorDir1, LOW); 
    analogWrite(DcMotorDir2, 255);
    }
if (DistanceAcc > ValOne-600 && ValTwo == HIGH) {                               //Step 2 Start deceleration from ValOne - 600 to ValOne Units
    digitalWrite(DcMotorDir1, LOW);
    int DcMotorAcc = map(DistanceAcc, ValOne-600, ValOne, 255, 120);
    analogWrite(DcMotorDir2, DcMotorAcc);
    }
       
if (Distance > ValOne && ValTwo == HIGH) { 
     digitalWrite(DcMotorDir1, LOW);                                           //Step 3 If > ValOne turn motor off
     digitalWrite(DcMotorDir2, LOW); 
     Servo1.write(85);                          
     delay(150);                                                               //Step 4 Move arm up to hit switch
     Servo1.write(170);                                                        //Step 5 Switch hs been hit. Move arm back down
     delay(130);                                           
     ValTwo = LOW;
     }
   
if (DistanceAcc < 600 && ValTwo == LOW) {                                      //Step 6 Move the arm right. 
    digitalWrite(DcMotorDir2, LOW);  
    DistanceAcc = constrain(DistanceAcc , 0, 600);                             // Step 7 deceleration of 600 units
    int DcMotorAcc = map(DistanceAcc, 0, 600, 120, 255  );
    analogWrite(DcMotorDir1, DcMotorAcc);
    }
if (DistanceAcc > 600 && ValTwo == LOW) {
    digitalWrite(DcMotorDir2, LOW);
    analogWrite(DcMotorDir1, 255);
    }
       
if (Distance <0) {
   if(ValTwo == LOW){                                                            //Step 8 Stop arm from moving right. (ready for step 1 to start over)
      digitalWrite(DcMotorDir1, LOW);
      digitalWrite(DcMotorDir2, LOW);
      ValTwo = HIGH;
    }
    }

}


void handleEncoder() {
   if(digitalRead(encoderI) == digitalRead(encoderQ))                      //Reads the encoder = Distance
   { Distance ++;
   }
   else
   { Distance --;
   }
}

Slowman223:
I have been playing around with deceleration for when it goes back to home.
this is what i got, but no luck

int Switch1 = 22;

int Switch2 = 23;
int Switch3 = 24;
int Switch4 = 25;
int Switch5 = 26;

int LimitSwitch = 32;
int DcMotorOn = 8;        //Motor On - Off
int DcMotorDir1 = 9;      //Motor Direction (Dir1 LOW + Dir2 HIGH = Move Right)
int DcMotorDir2 = 10;    //Motor Direction (Dir1 HIGH + Dir2 LOW = Move Left)
#define encoderI 2
#define encoderQ 3
int Distance = 0;        //Distance on linear encoder
int ServoAngle = 170;    //ServoAngle
int DistanceAcc;          //Distance for acceleration / deceleration
int ValTwo = HIGH;

boolean LastSwitch1 = LOW;
boolean FakeSwitch = LOW;

#include <Servo.h>
Servo Servo1;
int pos = 0;

void setup() {

Serial.begin(9600);
Servo1.attach(11);
Servo1.write(180); 
pinMode(encoderI, INPUT);
pinMode(encoderQ, INPUT); attachInterrupt(0, handleEncoder, CHANGE);
pinMode(LimitSwitch, INPUT);
pinMode(Switch1, INPUT);
pinMode(DcMotorOn, OUTPUT);
pinMode(DcMotorDir1, OUTPUT);
pinMode(DcMotorDir2, OUTPUT);
digitalWrite(DcMotorOn, HIGH);
analogWrite(DcMotorDir1, 160);
digitalWrite(DcMotorDir2, LOW);

}

void loop() {

if (digitalRead(LimitSwitch) == HIGH) {  // Moves the Arm to the far rigth and sets the Distance to -150
    digitalWrite(DcMotorDir1, LOW);
    Distance = -150;
    }

if (digitalRead(Switch1) == HIGH) {                    //Switch 1
    FlipSwitch (2870);
    }
if (digitalRead(Switch2) == HIGH) {                      //Switch 2
    FlipSwitch (2570);
    }
if (digitalRead(Switch3) == HIGH) {                        //Switch 3
    FlipSwitch (2250);
    }
if (digitalRead(Switch4) == HIGH) {                      //Switch 4
    FlipSwitch (1900);
    }
if (digitalRead(Switch5) == HIGH) {                    //Switch 5
    FlipSwitch (1600);
    }

//Serial.println(Distance);

}

void FlipSwitch (int ValOne) {                // ValOne = distance to switch

DistanceAcc = Distance;                                         
   
if (DistanceAcc < ValOne-600 && ValTwo == HIGH) {                              //Step 1  Set ValOne to teh distance with deceleration of 600 units (2870-2270=600)
    digitalWrite(DcMotorDir1, LOW);
    analogWrite(DcMotorDir2, 255);
    }
if (DistanceAcc > ValOne-600 && ValTwo == HIGH) {                              //Step 2 Start deceleration from ValOne - 600 to ValOne Units
    digitalWrite(DcMotorDir1, LOW);
    int DcMotorAcc = map(DistanceAcc, ValOne-600, ValOne, 255, 120);
    analogWrite(DcMotorDir2, DcMotorAcc);
    }
     
if (Distance > ValOne && ValTwo == HIGH) {
    digitalWrite(DcMotorDir1, LOW);                                          //Step 3 If > ValOne turn motor off
    digitalWrite(DcMotorDir2, LOW);
    Servo1.write(85);                         
    delay(150);                                                              //Step 4 Move arm up to hit switch
    Servo1.write(170);                                                        //Step 5 Switch hs been hit. Move arm back down
    delay(130);                                         
    ValTwo = LOW;
    }
 
if (DistanceAcc < 600 && ValTwo == LOW) {                                      //Step 6 Move the arm right.
    digitalWrite(DcMotorDir2, LOW); 
    DistanceAcc = constrain(DistanceAcc , 0, 600);                            // Step 7 deceleration of 600 units
    int DcMotorAcc = map(DistanceAcc, 0, 600, 120, 255  );
    analogWrite(DcMotorDir1, DcMotorAcc);
    }
if (DistanceAcc > 600 && ValTwo == LOW) {
    digitalWrite(DcMotorDir2, LOW);
    analogWrite(DcMotorDir1, 255);
    }
     
if (Distance <0) {
  if(ValTwo == LOW){                                                            //Step 8 Stop arm from moving right. (ready for step 1 to start over)
      digitalWrite(DcMotorDir1, LOW);
      digitalWrite(DcMotorDir2, LOW);
      ValTwo = HIGH;
    }
    }

}

void handleEncoder() {
  if(digitalRead(encoderI) == digitalRead(encoderQ))                      //Reads the encoder = Distance
  { Distance ++;
  }
  else
  { Distance --;
  }
}

Try something like this:

if (distance > 600){
speed = 255;  //full speed
} 
else{
speed = map (distance, 0, 600, 0, 255); //adjust speed to account for distance to travel
}
analogWrite(DcMotorDir1, speed);

Then, as your 'head' nears the end stop, it will get progressively slower, assuming that distance gets updated every time through loop().

Thanks i will give it a go :slight_smile:

OK its coming along nice
I have a new vid

As you can see it works from right to left. i need your help to make it turn the switches off in the same order i turn them on

This is the most up to date code

int Switch1 = 22;
int Switch2 = 23;
int Switch3 = 24;
int Switch4 = 25;
int Switch5 = 26;
int Switch6 = 27;
int Switch7 = 28;
int Switch8 = 29;
int Switch9 = 30;
int Switch10 = 31;


int LimitSwitch = 32;
int DcMotorOn = 8;        //Motor On - Off
int DcMotorDir1 = 9;      //Motor Direction (Dir1 LOW + Dir2 HIGH = Move Right)
int DcMotorDir2 = 10;     //Motor Direction (Dir1 HIGH + Dir2 LOW = Move Left)
#define encoderI 2
#define encoderQ 3
int Distance = 0;         //Distance on linear encoder
int ServoAngle = 170;     //ServoAngle
int DistanceAcc;          //Distance for acceleration / deceleration
int ValTwo = HIGH;
int GoHome = LOW;

boolean LastSwitch1 = LOW;
boolean FakeSwitch = LOW;

#include <Servo.h> 
Servo Servo1; 
int pos = 0;

void setup() {

Serial.begin(9600);
Servo1.attach(11);
Servo1.write(180);   
pinMode(encoderI, INPUT);
pinMode(encoderQ, INPUT); attachInterrupt(0, handleEncoder, CHANGE);
pinMode(LimitSwitch, INPUT);
pinMode(DcMotorOn, OUTPUT);
pinMode(DcMotorDir1, OUTPUT);
pinMode(DcMotorDir2, OUTPUT);
digitalWrite(DcMotorOn, HIGH);
analogWrite(DcMotorDir1, 160); 
digitalWrite(DcMotorDir2, LOW); 
}


void loop() {

if (digitalRead(LimitSwitch) == HIGH) {  // Moves the Arm to the far rigth and sets the Distance to -150
    digitalWrite(DcMotorDir1, LOW); 
    Distance = -150;
    }

if (digitalRead(Switch1) == HIGH) {                     //Switch 1
    FlipSwitch (2879);
    }
if (digitalRead(Switch2) == HIGH) {                       //Switch 2
    FlipSwitch (2567);
    }
if (digitalRead(Switch3) == HIGH) {                        //Switch 3
    FlipSwitch (2254);
    }
if (digitalRead(Switch4) == HIGH) {                      //Switch 4
    FlipSwitch (1941);
    }
if (digitalRead(Switch5) == HIGH) {                     //Switch 5 
    FlipSwitch (1620);
    }
if (digitalRead(Switch6) == HIGH) {                     //Switch 6
    FlipSwitch (1300);
    }
if (digitalRead(Switch7) == HIGH) {                       //Switch 7
    FlipSwitch (979);
    }
if (digitalRead(Switch8) == HIGH) {                        //Switch 8
    FlipSwitch (672);
    }
if (digitalRead(Switch9) == HIGH) {                      //Switch 9
    FlipSwitch (356);
    }
if (digitalRead(Switch10) == HIGH) {                     //Switch 10
    FlipSwitch (40);
    }


if (GoHome == HIGH && Distance < 40){
     digitalWrite(DcMotorDir2, LOW);  
     digitalWrite(DcMotorDir1, LOW);
     }

if (Distance <40){
   GoHome = LOW;
   }


Serial.println(Distance);
}




void FlipSwitch (int ValOne) {                 // ValOne = distance to switch


    DistanceAcc = Distance;                                          
    
if (DistanceAcc < ValOne-600 && ValTwo == HIGH) {                               //Step 1  Set ValOne to teh distance with deceleration of 600 units (2870-2270=600)
    digitalWrite(DcMotorDir1, LOW); 
    analogWrite(DcMotorDir2, 255);  
    }
if (DistanceAcc > ValOne-600 && ValTwo == HIGH) {                               //Step 2 Start deceleration from ValOne - 600 to ValOne Units
    digitalWrite(DcMotorDir1, LOW);
    int DcMotorAcc = map(DistanceAcc, ValOne-600, ValOne, 255, 140);
    analogWrite(DcMotorDir2, DcMotorAcc);
    }
       
if (Distance > ValOne && ValTwo == HIGH) { 
     digitalWrite(DcMotorDir1, LOW);                                           //Step 3 If > ValOne turn motor off
     digitalWrite(DcMotorDir2, LOW); 
     Servo1.write(85);                          
     delay(150);                                                               //Step 4 Move arm up to hit switch
     Servo1.write(170);                                                        //Step 5 Switch hs been hit. Move arm back down
     delay(130);                                           
     ValTwo = HIGH;
     }
   
    
   
 
 if (ValTwo == HIGH && digitalRead(Switch1) == LOW && 
     digitalRead(Switch2) == LOW && digitalRead(Switch3) == LOW && 
     digitalRead(Switch4) == LOW && digitalRead(Switch5) == LOW && 
     digitalRead(Switch6) == LOW && digitalRead(Switch7) == LOW && 
     digitalRead(Switch8) == LOW && digitalRead(Switch9) == LOW && 
     digitalRead(Switch10) == LOW) 
     {
     GoHome = HIGH;
     digitalWrite(DcMotorDir2, LOW);  
     digitalWrite(DcMotorDir1, HIGH);
     }

         
}    

       


void handleEncoder() {
   if(digitalRead(encoderI) == digitalRead(encoderQ))                      //Reads the encoder = Distance
   { Distance ++;
   }
   else
   { Distance --;
   }
}

Thanks guys

Looks like a good start.

Slowman223:
As you can see it works from right to left. i need your help to make it turn the switches off in the same order i turn them on

Then you need to keep a record of the order you switched them on. Your record will need to be moved up each time a switch is turned off so that the next recorded switch becomes the first switch to turn off. An array is the simplest way of doing that.
When a switch is turned on, add its number to the next free position in the array.
You'll need a variable that keeps count of the number of switches turned on.
When a switch is turned off, copy array position 1 into array position 0, position 2 into position 1, etc. ( = move the array's contents left by one position, losing the one that you've already turned off)
Decrement the count variable (There's one less switch to turn off).
The next switch to be turned off will be the one in position 0 in the array.
You'll need to check each switch's state each time through loop(), probably with some 'debounce'.

Henry_Best:
Looks like a good start.Then you need to keep a record of the order you switched them on. Your record will need to be moved up each time a switch is turned off so that the next recorded switch becomes the first switch to turn off. An array is the simplest way of doing that.
When a switch is turned on, add its number to the next free position in the array.
You'll need a variable that keeps count of the number of switches turned on.
When a switch is turned off, copy array position 1 into array position 0, position 2 into position 1, etc. ( = move the array's contents left by one position, losing the one that you've already turned off)
Decrement the count variable (There's one less switch to turn off).
The next switch to be turned off will be the one in position 0 in the array.
You'll need to check each switch's state each time through loop(), probably with some 'debounce'.

Yup thats what i need. just dont know how to wright the code. think is called a ring buffer

Generally speaking, a ring buffer is an array and two pointers, the "write" pointer and the "read" pointer.

When you enter a value into the array using the write pointer to index it, you then increment the write pointer.

When you want to read a value, you see whether the read pointer is the same as the write pointer. If it is, there is nothing to be read because that is the position into which the write pointer is about to put the next value. Conversely, before you write something, you check whether incrementing the write pointer will cause it to equal the read pointer in which case you block the write.

Now the pointers must loop. The easiest way to do this is to have an array contain a power of two elements (8, 16, 32, ...) and use a bit mask operation on the (zero-based) pointer after every increment to constrain it to the corresponding power of two.

I see. im just goin to need to look into a array. if you have any code or links you would like share it would be much appreciated.

Paul__B:
Generally speaking, a ring buffer is an array and two pointers, the "write" pointer and the "read" pointer.

When you enter a value into the array using the write pointer to index it, you then increment the write pointer.

When you want to read a value, you see whether the read pointer is the same as the write pointer. If it is, there is nothing to be read because that is the position into which the write pointer is about to put the next value. Conversely, before you write something, you check whether incrementing the write pointer will cause it to equal the read pointer in which case you block the write.

Now the pointers must loop. The easiest way to do this is to have an array contain a power of two elements (8, 16, 32, ...) and use a bit mask operation on the (zero-based) pointer after every increment to constrain it to the corresponding power of two.

I've never come across a ring buffer before. A clever idea that does away with the need to shift the array's contents.
Thanks.

Just need some one to wright the code for me :slight_smile:
thanks guys

Slowman223:
Just need some one to wright the code for me :slight_smile:
thanks guys

How much are you paying?

if i really need to i will. dont know how much your thinking tho hahah

Quote from: Henry_Best Fri Dec 18 2015 23:58:07 GMT-0500 (Eastern Standard Time)

I've never come across a ring buffer before. A clever idea that does away with the need to shift the array's contents.
Thanks.

does the count down part so as a switch is closed, you eliminate that one. move to the next.

if (switch_last_changed == LOW )( // some flag to say you closed the last switch and are ready to close the next switch
rb0=rb1
rb1=rb2
rb2=rb3
rb3=rb4
rb4=rb5
rb5=rb6
rb6=rb7
rb7=rb8
rb8=rb9
rb9=rb10
rb10=rb11
rb11=0 ; //
}

// now, pass rb0 to your code and close whatever value (switch position) rb0 has.

//whatever you do to sense any switch changed state....someone pushed a switch.
// if any switch changed state, new_switch = HIGH

if (new_switch ==HIGH ){
rb11= x // x being whatever value you have for that switch, 1 thru 10 ; // not 0 that is for no switch pressed
new_switch=LOW;
}

NOTE : this does NOT work. it is just an idea how to pass one value to your code to tell you what to do next.
sum (rb0 to rb9) if 0 do nothing.

there is a potential that you could change state of a switch while in the above loop.
if sum(rb0 to rb9) = 0, then scan switch states to make sure they are all closed ( or open)

you could put in two values. the time between switch presses and also the number of the switch you pressed.
then pause that time between closing the switches.

another idea, not seen, you know how, in bowling alleys, they have a large bar ?
you could move all the way past switch 10, grab a bar and close all the switches at once. you know someone will take a ruler and turn them all on at once..... right ?
flip the bar up, then keep your finger pointer up and slide down the arm till it passes switch 0 and falls/springs closed.