Help inserting a start button in the code

I am new to programing and I need help getting this project to work. I am trying to insert a start button in the code. I need the relays to stop in position (1) and wait for button press, then proceed through positions 2-7 and back to position (1) and stop/wait for button press.

#define RELAY1 13

#define RELAY2 12

#define RELAY3 11

#define RELAY4 10

const int pin = 6;

int buttonstate = 0;

void setup()

{

// Initialize the arduino data pins for OUTPUT

pinMode(RELAY1, OUTPUT);

pinMode(RELAY2, OUTPUT);

pinMode(RELAY3, OUTPUT);

pinMode(RELAY4, OUTPUT);

pinMode(pin, INPUT);

digitalRead(pin);

}

void loop()

{

digitalWrite(RELAY1, LOW); // Turns OFF Relay 1

digitalWrite(RELAY2, LOW); // Turns OFF Relay 2

digitalWrite(RELAY3, HIGH); // Turns ON Relay 3

digitalWrite(RELAY4, LOW); // Turns OFF Relay 4

delay(4200); // Wait 2 seconds

if (digitalRead(pin) == HIGH) {

action();

}

action();{

digitalWrite(RELAY1, LOW); // Turns OFF Relay 1

digitalWrite(RELAY2, HIGH); // Turns ON Relay 2

digitalWrite(RELAY3, HIGH); // Turns ON Relay 3

digitalWrite(RELAY4, LOW); // Turns OFF Relay 4

delay(2200); // Wait 2 seconds

digitalWrite(RELAY1, LOW); // Turns OFF Relay 1

digitalWrite(RELAY2, HIGH); // Turns ON Relay 2

digitalWrite(RELAY3, LOW); // Turns OFF Relay 3

digitalWrite(RELAY4, HIGH); // Turns ON Relay 4

delay(2200); // Wait 2 seconds

digitalWrite(RELAY1, HIGH); // Turns ON Relay 1

digitalWrite(RELAY2, HIGH); // Turns ON Relay 2

digitalWrite(RELAY3, LOW); // Turns OFF Relay 3

digitalWrite(RELAY4, HIGH); // Turns ON Relay 4

delay(2200); // Wait 2 seconds

digitalWrite(RELAY1, HIGH); // Turns ON Relay 1

digitalWrite(RELAY2, LOW); // Turns OFF Relay 2

digitalWrite(RELAY3, LOW); // Turns OFF Relay 3

digitalWrite(RELAY4, HIGH); // Turns ON Relay 4

delay(2200); // Wait 2 seconds

digitalWrite(RELAY1, HIGH); // Turns ON Relay 1

digitalWrite(RELAY2, LOW); // Turns OFF Relay 2

digitalWrite(RELAY3, HIGH); // Turns ON Relay 3

digitalWrite(RELAY4, LOW); // Turns OFF Relay 4

delay(2200); // Wait 2 seconds

digitalWrite(RELAY1, HIGH); // Turns ON Relay 1

digitalWrite(RELAY2, LOW); // Turns OFF Relay 2

digitalWrite(RELAY3, HIGH); // Turns ON Relay 3

digitalWrite(RELAY4, LOW); // Turns OFF Relay 4

delay(2200); // Wait 2 seconds

}

What does your code do now and what have you tried for stopping and using a button?

You have no bounce control, you need to use a button library (there are many) so you only act on genuine button presses one time.

Hello and welcome!

One step at a time. Learn how to turn on and off 1 relay, better still, one LED, which is built into every board. When you can do that move on to something more complicated, like 2 relays (or LEDs).

What you are doing is called a 'finite state machine (FSM)'. In your case you are doing the simplest FSM possible as it has 2 states: ON and OFF.

Go to the Arduino IDE and do the examples, especially:
02 digital / button
Which is what you are trying to do.

You will probably discover what @sonofcy said about bounce control, so move on to:
02 digital / debounce

I also have a tutorial about buttons here:

Come back when you've tried the examples, or if you get stuck with the examples.

There are also other tutorials here that might help you, but the things I've shown you should get you started.

Finally, this is wrong:

But I'm going to leave it to you to find out why.

1 Like

How are your buttons wired?

Me too. Fix that and your code works perfectly. But here perfectly is perhaps not satisfactory. You tell us, since...

... this

delay(4200); // Wait 2 seconds

two seconds waiting, every time the loop comes around means you have to hold the button down for an average slow response time of half that.

And you must get your finger off the button or it will action() again; no need to hurry as the button will not be looked at for quite some time.

a7

There's 16+ seconds worth of delay in the loop(), so the button won't poll fast enough to see a bounce.

(That also means it won't poll fast enough to notice most button presses.)

1 Like

And the code you show compiles?

Your code looks pretty close to functional.... I think it you changed this line to actually define a function (Arduino provides this Language reference: https://docs.arduino.cc/language-reference/en/structure/further-syntax/curlyBraces/#functions -- what a terrible reference for teaching functions!) :

like

void action(){

...and inserted a copy of the resetting to position1 code at the end of the action() function, it just might work as desired.

Is this what you want it to do?

-jim lee

What is the task of the sketch in real life?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.