Controlling Multiple Loops With a Select Switch and Start Button

Hi all,

I'm wondering if I can get some answers to some questions I have about programming. I'm still fairly new to the Arduino, but have a basic understanding of how it works. I'm trying to build a controller for a plate processor at work. It would have 3 outputs. Here is what I have in mind:

A 'start' button would be pressed. Based on the position of a 4-position switch, one of four loops would run. Each loop would be a simple program controlling the three outputs, and run only once before waiting for the next press of the 'start' button. Each loop would be identical, with the only difference being how long each output is run (each output is connected to a relay which would run either a pump or a solenoid).

My Questions:

  1. Is this feasable?
  2. How would I set up the program to start with a pushbutton of sorts?
  3. How would I set up the multiple loops to run based on the position of the selector switch?
  4. How would I make each loop run only once?

Any help would be greatly appreciated!!!
Thank you in advance!
-Mike

A 'start' button would be pressed. Based on the position of a 4-position switch, one of four loops would run.

One of 4 functions. Those functions may, or may not, involve looping.

  1. Is this feasable?

Yes.

  1. How would I set up the program to start with a pushbutton of sorts?

Reading the state of a switch is trivial.

  1. How would I set up the multiple loops to run based on the position of the selector switch?

How do you determine the position of the selector switch?

  1. How would I make each loop run only once?

Look at the state change detection example. Execute the appropriate function only when the switch becomes pressed.

PaulS,
As for reading the position of the switch, the common would be tied either high or low, and one of the four inputs associated with the switch would go (high or low) depending on the position of the knob.
I will look for the state change detection example.
Thanks!
-Mike

You might find planning and implementing a program useful as it shows how to put different activities in different functions.

You can use your switch to change the value of a variable (the state variable). Then, for example

else if (switchState == 4) {
    myFunction4();
}

but give your functions meaningful names :slight_smile:

...R

I would have one loop only detecting the button and the 4 position switch
Using state change to set a variable that determines duration.

When the button is pressed pass to a function that runs once using that variable .
When the function is complete return to loop to wait for another button press.
no need for multiple loops.

Here's what I came up with. It does exactly what I need! Thank you all for your help. At the moment, it runs the same routine with different timing depending on the position of the switch, and I will probably tweak it and maybe add an output or two before I'm done, but the hard part is done :slight_smile:

const int develop =  2; // relay connected to digital pin 2
const int etch = 3; // relay connected to digital pin 3
const int dirinse = 4;  // relay connected to digital pin 4
const int motor = 5; //motor relay connected to digital pin 5
int p1 = 6; // input switch position 1, digital pin 6
int p2 = 7; // input switch position 2, digital pin 7
int p3 = 8; // input switch position 3, digital pin 8
int p4 = 9; // input switch position 4, digital pin 9
int st = 10; // input start switch, digital pin 10
int stopnow = 0; //value to set loops to cycle once
int goahead = 0; //start button state
int switchstate = 0; //selector switch position
int program1 = 0;
int program2 = 0;
int program3 = 0;
int program4 = 0;


// The setup() method runs once, when the sketch starts

void setup()   {                
 // initialize the pins as input or output:
 pinMode(develop, OUTPUT);
 pinMode(etch, OUTPUT);
 pinMode(dirinse, OUTPUT);
 pinMode(motor, OUTPUT);
 pinMode(p1, INPUT);
 pinMode(p2, INPUT);
 pinMode(p3, INPUT);
 pinMode(p4, INPUT);
 pinMode(st, INPUT);
 
}
 
void loop() {

goahead = digitalRead(st);

if (goahead == HIGH)


{


  program1= digitalRead(p1);
  program2 = digitalRead(p2);
  program3 = digitalRead(p3);
  program4 = digitalRead(p4);
  
  if (program1 == HIGH)
 {
 digitalWrite(motor, HIGH); // start spin motor
 delay(2000);
 digitalWrite(develop, HIGH);   // close relay
 delay(5000);             // wait for develop time delay
 digitalWrite(develop, LOW);    // open relay
 delay(500);            // slight delay to allow for spray to stop
 digitalWrite(dirinse, HIGH);   // close relay
 delay(5000);             // wait for rinse time delay
 digitalWrite(dirinse, LOW);    // open relay
 delay(500);            // delay to allow spray to stop
 digitalWrite(etch, HIGH);   // close relay
 delay(5000);             // wait for etch time delay
 digitalWrite(etch, LOW);    // open relay
 delay(500);            // delay to allow spray to stop
 digitalWrite(dirinse, HIGH);   // close relay
 delay(5000);             // wait for rinse time delay
 digitalWrite(dirinse, LOW);    // open relay
 delay(5000);            // final drying spin before end
 digitalWrite(motor, LOW); // stop spin motor
 }
 
 else if (program2 == HIGH)
 {
 digitalWrite(motor, HIGH); // start spin motor
 delay(2000);
 digitalWrite(develop, HIGH);   // close relay
 delay(10000);             // wait for develop time delay
 digitalWrite(develop, LOW);    // open relay
 delay(500);            // slight delay to allow for spray to stop
 digitalWrite(dirinse, HIGH);   // close relay
 delay(10000);             // wait for rinse time delay
 digitalWrite(dirinse, LOW);    // open relay
 delay(500);            // delay to allow spray to stop
 digitalWrite(etch, HIGH);   // close relay
 delay(10000);             // wait for etch time delay
 digitalWrite(etch, LOW);    // open relay
 delay(500);            // delay to allow spray to stop
 digitalWrite(dirinse, HIGH);   // close relay
 delay(10000);             // wait for rinse time delay
 digitalWrite(dirinse, LOW);    // open relay
 delay(10000);            // final drying spin before end
 digitalWrite(motor, LOW); // stop spin motor
 }
 
 else if (program3 == HIGH)
 {
 digitalWrite(motor, HIGH); // start spin motor
 delay(2000);
 digitalWrite(develop, HIGH);   // close relay
 delay(20000);             // wait for develop time delay
 digitalWrite(develop, LOW);    // open relay
 delay(500);            // slight delay to allow for spray to stop
 digitalWrite(dirinse, HIGH);   // close relay
 delay(20000);             // wait for rinse time delay
 digitalWrite(dirinse, LOW);    // open relay
 delay(500);            // delay to allow spray to stop
 digitalWrite(etch, HIGH);   // close relay
 delay(20000);             // wait for etch time delay
 digitalWrite(etch, LOW);    // open relay
 delay(500);            // delay to allow spray to stop
 digitalWrite(dirinse, HIGH);   // close relay
 delay(20000);             // wait for rinse time delay
 digitalWrite(dirinse, LOW);    // open relay
 delay(20000);            // final drying spin before end
 digitalWrite(motor, LOW); // stop spin motor
 }
 
 else if (program4 == HIGH)
 {
 digitalWrite(motor, HIGH); // start spin motor
 delay(2000);
 digitalWrite(develop, HIGH);   // close relay
 delay(1000);             // wait for develop time delay
 digitalWrite(develop, LOW);    // open relay
 delay(500);            // slight delay to allow for spray to stop
 digitalWrite(dirinse, HIGH);   // close relay
 delay(1000);             // wait for rinse time delay
 digitalWrite(dirinse, LOW);    // open relay
 delay(500);            // delay to allow spray to stop
 digitalWrite(etch, HIGH);   // close relay
 delay(1000);             // wait for etch time delay
 digitalWrite(etch, LOW);    // open relay
 delay(500);            // delay to allow spray to stop
 digitalWrite(dirinse, HIGH);   // close relay
 delay(2000);             // wait for rinse time delay
 digitalWrite(dirinse, LOW);    // open relay
 delay(1000);            // final drying spin before end
 digitalWrite(motor, LOW); // stop spin motor
 }
 }
}

At the moment, it runs the same routine with different timing depending on the position of the switch

Your sketch isn't going to be responsive to a change in the switch position for many seconds because of all the calls to "delay()".
This may or may not be a problem, but if it is (or you want to add a stop function that doesn't involve simply cutting the power) you may want to look at implementing your functions as a state machine.