Controlling Multiple Relays

Hi Folks,

I’m trying to control multiple relays, each with it’s own dedicated button.

The goal is to have each button turn on a relay and keep it on for a certain amount of time.

Ex: Button 1 turns on Relay 1 for 5sec
Button 2 turns on Relay 2 for 20sec
Button 3 turns on Relay 3 for 10sec
etc.

I know I can’t do this with delay and am trying to do it using millis (). I currently only have buttonPin4 scripted to include the millis. What I am trying to have happen is once I press the button, the relay stays active for 1min. What actually happens is only get enough power to light up the LED and the relay doesn’t activate. It also only stays lit for the duration of the button push not the full one minute.

How do I utilize the millis script to keep the relay active for one min and still allow other buttons to be triggered?

Thanks!

This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/Button
 */

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin4 = 2;     // the number of the pushbutton pin
const int buttonPin3 = 3;
const int buttonPin2 = 4;
const int buttonPin1 = 5;

const long interval1 = 60000 // 1,5 and 15 min intervals
const long interval5 = 300000
const long interval11 = 900000


const int ledPin4 =  9;      // the number of the LED pin
const int ledPin3 =  12;
const int ledPin2 =  11;
const int ledPin1 =  10;

// variables will change:
int button4State = 0;         // variable for reading the pushbutton status
int button3State = 0;  
int button2State = 0;
int button1State = 0;

long previousMillis1 = 0;
long previousMillis2 = 0;
long previousMillis3 = 0;
long previousMillis4 = 0;


void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin1, OUTPUT);
  
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin4, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin1, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  button4State = digitalRead(buttonPin4);
  button3State = digitalRead(buttonPin3);
  button2State = digitalRead(buttonPin2);
  button1State = digitalRead(buttonPin1);

  unsigned long currentMillis = millis();

  
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (button4State == HIGH) {
    // turn LED on:
    digitalWrite(ledPin4, LOW);
  }
      {
       if (currentMillis > interval1)
           digitalWrite(ledPin4, HIGH);
      }
  else {
    // turn LED off:
    digitalWrite(ledPin4, HIGH);
 
  }
  if (button3State == HIGH) {
    // turn LED on:
    digitalWrite(ledPin3, LOW);
  }
 else {
    // turn LED off:
    digitalWrite(ledPin3, HIGH);
  }
   if (button2State == HIGH) {
    // turn LED on:
    digitalWrite(ledPin2, LOW);
  }
 else {
    // turn LED off:
    digitalWrite(ledPin2, HIGH);
  }
   if (button1State == HIGH) {
    // turn LED on:
    digitalWrite(ledPin1, LOW);
  }
 else {
    // turn LED off:
    digitalWrite(ledPin1, HIGH);
  }
}

Relay_Button_.ino.ino (2.44 KB)

I know I can't do this with delay and am trying to do it using millis (). I'm just not sure how to best go about it.

The best way is the one that works.

Any advice?

Read the damned stickies at the top of the forum. Post your code CORRECTLY.
Describe what the code ACTUALLY does.
Describe how that differs from what you want.

Original posted edited for clarity and in an attempt to follow forum protocol.

const long interval1 = 60000 // 1,5 and 15 min intervals
const long interval5 = 300000
const long interval11 = 900000

The numbers in the names do not make sense in light of the comment and the values.

I'm confused as to why you have relays connected to ledPins. Are you planning to connect LEDs to relayPins?

      {
       if (currentMillis > interval1)
           digitalWrite(ledPin4, HIGH);
      }

Most people consider the difference between now and then to be important, not just now.

The curly braces there are useless.

Posting code that ACTUALLY compiles is the first step. Try again.

Ok, here is the updated code. It seems to compile fine now.

The LEDs I'm referring to are the ones on the relay board. When I have the millis command in place the LED lights up but the relay itself does not actuate.

I have also corrected the 11 to a 15 in interval 15.

I'm not sure what "Most people consider the difference between now and then to be important, not just now" means. Could you explain?

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin4 = 2;     // the number of the pushbutton pin
const int buttonPin3 = 3;
const int buttonPin2 = 4;
const int buttonPin1 = 5;

const long interval1 = 60000; // 1,5 and 15 min intervals
const long interval5 = 300000;
const long interval15 = 900000;


const int ledPin4 =  9;      // the number of the LED pin
const int ledPin3 =  12;
const int ledPin2 =  11;
const int ledPin1 =  10;

// variables will change:
int button4State = 0;         // variable for reading the pushbutton status
int button3State = 0; 
int button2State = 0;
int button1State = 0;

long previousMillis1 = 0;
long previousMillis2 = 0;
long previousMillis3 = 0;
long previousMillis4 = 0;


void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin1, OUTPUT);
 
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin4, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin1, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  button4State = digitalRead(buttonPin4);
  button3State = digitalRead(buttonPin3);
  button2State = digitalRead(buttonPin2);
  button1State = digitalRead(buttonPin1);

  unsigned long currentMillis = millis();

 
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (button4State == HIGH) {
    // turn LED on:
    digitalWrite(ledPin4, LOW);
  }
      
       if (currentMillis > interval1)
           digitalWrite(ledPin4, HIGH);
      
  else {
    // turn LED off:
    digitalWrite(ledPin4, HIGH);
 
  }
  if (button3State == HIGH) {
    // turn LED on:
    digitalWrite(ledPin3, LOW);
  }
 else {
    // turn LED off:
    digitalWrite(ledPin3, HIGH);
  }
   if (button2State == HIGH) {
    // turn LED on:
    digitalWrite(ledPin2, LOW);
  }
 else {
    // turn LED off:
    digitalWrite(ledPin2, HIGH);
  }
   if (button1State == HIGH) {
    // turn LED on:
    digitalWrite(ledPin1, LOW);
  }
 else {
    // turn LED off:
    digitalWrite(ledPin1, HIGH);
  }
}

The LEDs I'm referring to are the ones on the relay board. When I have the millis command in place the LED lights up but the relay itself does not actuate.

How do those LEDs relate to the relays? It seems to me that the relay should be controlling the LED.

I'm not sure what "Most people consider the difference between now and then to be important, not just now" means. Could you explain?

If I asked you to call me every hour, what would you need to know to know whether it is, at any given point in time, time to call me again? You'd need to know what time it is now, right? But that isn't sufficient. You need to know the time between calls AND you'd need to know when you called me last time.

Now minus then is the time since the last event. When THAT exceeds the threshold, it's time to do something.

The LEDs are indicator lights on the relay shield. The illuminate when a given relay is active.

Ok I get what you are saying about the times. How can I work that into my code? I've tried to watch a few tutorials but I'm not getting how to actually write the script in this case.

You are not using millis() correctly. Have a look at several things at a time. Also, you need separate intervals for each of the actions.

And, while the compiler won't care, it is much easier to understand and debug code if you indent it carefully so you can easily see what belongs with what.

...R

The LEDs are indicator lights on the relay shield. The illuminate when a given relay is active.

But not because you do something to the LED pin. They light because you do something to the relay pin. The schematic will show a relay connected to that pin, not an LED. The name in the code should reflect that.

Are you trying to power the relay coils from the Arduino output pins?
Can you provide a link to your relay board?

I agree with all above.

If the LED turns on but the relay doesn't switch I think the board is connected wrong.

Second, if you do of have a lot of thinks that look similar (like button1, button 2 etc) there is a easier way of doing it. In this case, array's