Help with relay timing..................please

Hi, I am quite new to all this, but I have managed to find some code that “almost” does the job. (NOT MY CODE) :confused:

I have a 4 relay board and what I want to do is this:

press button once to start sequence,

relay 1 comes on.

after a short delay relay 2 comes on.

another delay and relay 3 comes on

another delay and relay 3 goes off

another delay and relay 3 comes on

another delay and relay 3 goes off

another delay and relay 3 comes on

another delay and relay 3 goes off

another delay and relay 3 comes on

another delay and relay 3 goes off

another delay and relay 4 comes on

another delay and all relays go off.

all interval times to be set as required, not sure what they will be yet?

please see attached sketch, it is almost there, but after hours of puzzling I cant work out how to make

relay 3 turn on and off multiple times as required. if it is possible at all ?? :o

hoping one of you good people will be willing to modify the code for me, I just cant get my head around

the problem.

#define RELAY_ON LOW

#define RELAY_OFF HIGH

// these are the pins the relays are connected to

#define RELAY1 2

#define RELAY2 3

#define RELAY3 4

#define RELAY4 5

// this is where the momentary switch is connected

#define STARTBUTTON 8

// set up the start and stop times, in milliseconds

// relay1 starts immediately

#define RELAY2_START_TIME 10000 // 0:10

#define RELAY3_START_TIME 22000 // 0:22

#define RELAY4_START_TIME 28000 //

#define ACTION_STOP_TIME 112000 // 1:52 - stops relays 1 and 2

#define RELAY3_STOP_TIME 24000 // 1:40

#define RELAY4_STOP_TIME 34000 //

// this keeps track of the time since the button press

long startTime;

long lastTimeButtonPressed;

long lastOutputTime;

// active is TRUE when the actions are happening

boolean active;

boolean relay2_active;

boolean relay3_active;

boolean relay4_active;

void setup() {

// start the serial for debug

Serial.begin(9600);

Serial.println(“Starting application.”);

// set the relay pins as outputs

pinMode(RELAY1, OUTPUT);

pinMode(RELAY2, OUTPUT);

pinMode(RELAY3, OUTPUT);

pinMode(RELAY4, OUTPUT);

// make sure the relays are all off to start with

digitalWrite(RELAY1, RELAY_OFF); // turn off relay1

digitalWrite(RELAY2, RELAY_OFF); // turn off relay2

digitalWrite(RELAY3, RELAY_OFF); // turn off relay3

digitalWrite(RELAY4, RELAY_OFF); // turn off relay4

// set the pushbutton pin as an input

pinMode(STARTBUTTON, INPUT);

startTime = 0;

active = false;

relay2_active = false;

relay3_active = false;

lastTimeButtonPressed = -1000; // to avoid the initial 1 second button ignore

lastOutputTime = 0;

}

void loop() {

// check for button push

// read the state of the switch into a local variable:

int reading = digitalRead(STARTBUTTON);

// ignore if button is pressed more than once per second

// this also simplifies debounce

if (millis() - lastTimeButtonPressed > 1000 && reading == HIGH) {

lastTimeButtonPressed = millis();

Serial.println(“Button was pressed.”);

if (active) {

// pushing the button when things are going on stops everything

digitalWrite(RELAY1, RELAY_OFF); // turn off relay1

digitalWrite(RELAY2, RELAY_OFF); // turn off relay2

digitalWrite(RELAY3, RELAY_OFF); // turn off relay3

digitalWrite(RELAY4, RELAY_OFF); // turn off relay4

active = false;

}

else {

active = true;

startTime = millis();

Serial.println(“Turning ON relay 1”);

digitalWrite(RELAY1, RELAY_ON); // turn on relay1

}

}

if (active) {

// show time once per second

if((millis() - lastOutputTime) > 1000) {

Serial.print("Time: ");

Serial.println((millis() - startTime) / 1000);

lastOutputTime = millis();

}

if (!relay2_active && millis() - startTime > RELAY2_START_TIME && millis() - startTime < ACTION_STOP_TIME) {

Serial.println(“Turning ON relay 2”);

digitalWrite(RELAY2, RELAY_ON); // turn on relay2

relay2_active = true;

}

if (!relay3_active && millis() - startTime > RELAY3_START_TIME && millis() - startTime < RELAY3_STOP_TIME) {

Serial.println(“Turning ON relay 3”);

digitalWrite(RELAY3, RELAY_ON); // turn on relay3

relay3_active = true;

}

if (relay3_active && millis() - startTime > RELAY3_STOP_TIME) {

Serial.println(“Turning OFF relay 3”);

digitalWrite(RELAY3, RELAY_OFF); // turn off relay3

relay3_active = false;

}

if (!relay4_active && millis() - startTime > RELAY4_START_TIME && millis() - startTime < RELAY4_STOP_TIME) {

Serial.println(“Turning ON relay 4”);

digitalWrite(RELAY4, RELAY_ON); // turn on relay4

relay4_active = true;

}

if (relay4_active && millis() - startTime > RELAY4_STOP_TIME) {

Serial.println(“Turning OFF relay 4”);

digitalWrite(RELAY4, RELAY_OFF); // turn off relay4

relay4_active = false;

}

if (active && millis() - startTime > ACTION_STOP_TIME) {

Serial.println(“Turning OFF all relays”);

// stop everything

// relay3 should already be off,

// but this makes sure it’s all off in case there was a delay

digitalWrite(RELAY1, RELAY_OFF); // turn off relay1

digitalWrite(RELAY2, RELAY_OFF); // turn off relay2

digitalWrite(RELAY3, RELAY_OFF); // turn off relay3

digitalWrite(RELAY4, RELAY_OFF); // turn off relay4

active = false;

relay2_active = false;

relay3_active = false;

}

}

}

for controlling the switching of the relays why not do a simple sequence (assume button just been pressed)

     digitalWrite(RELAY1, RELAY_ON); // turn on relay1
     delay(delay1)
     digitalWrite(RELAY2, RELAY_ON); // turn on relay2
     delay(delay2);
     for(int i=0;i<3;i++){
        digitalWrite(RELAY3, RELAY_ON); // turn on relay3
        delay(delay3a);
        digitalWrite(RELAY3, RELAY_OFF); // turn off relay3
        delay(delay3b);
     }
      digitalWrite(RELAY4, RELAY_ON); // turn on relay4
      delay(delay4);

you need to define delay1, delay2, etc as required

Okay, that post was a real mess. Please read the permanent posts at the top of the forum to learn how to post, and how to post code, correctly.

You need a counter variable for how many time you have turned it on/off.
The counter starts at zero when the sequence starts, and when the counter gets to the required number you know you are done.

if (!relay3_active && relay3_count < X && millis() - startTime > relay3_start_time[relay3_count]) {
  Serial.println("Turning ON relay 3");
  digitalWrite(RELAY3, RELAY_ON); // turn on relay3
  relay3_active = true;
}

if (relay3_active && relay3_count < X && millis() - startTime > relay3_stop_time[relay3_count]) {
  Serial.println("Turning OFF relay 3");
  digitalWrite(RELAY3, RELAY_OFF); // turn off relay3
  relay3_active = false;
  relay3_count++;
}

Hi, Thank you for the fast reply, looks interesting will give it a try. :slight_smile:

aarg:
Okay, that post was a real mess. Please read the permanent posts at the top of the forum to learn how to post, and how to post code, correctly.

thanks for making me feel welcome here............. not helpful or nice.

Some people here are direct when you mess up, deal with it… Worth a read: http://www.catb.org/~esr/faqs/smart-questions.html

Back to the question: My advice would be to learn about state-machines, and idioms for coding them,
this is a case of a behaviour that would be quite easy to formulate as a state-transition diagram, and
then transliterate to code.

There’s a lot about state machines out there, this might be a starting point: Programming embedded systems the easy way – with state machines - Embedded.com

You can edit your op to include code tags. It is helpful for you! The forum guidelines are there to give you the best chance of getting a useful response by informing you how to post in a way that makes it easy to help you.