How to have an on and off Switch for X amount of time with 12v Relays

Hi everyone first time posting and first time working with Arduinos, I have some experience with coding but with java, I have been working on this project for a good two days and have been stuck on how I could implement this idea onto the Code I have or if I have to rewrite everything i have.

what i want to do is have the arduino to control the relays with buttons and once the button is pressed the relay would switch on but turn off after X amount of seconds i might have come close but the results i would get is that the relay would stay on for about a 7 seconds (i implemented it on millis if statement) and starts blinking erratically after the 7 seconds. if anyone can give me some pointers or let me know what im doing wrong i'd greatly appreciate it!

also all of this is on a arduino mega and a 16 channel relay
here's the code i have:

int buttonPin = 2;// Connect output to push button
int relayPin = 9;// Connected to relay (LED)

int buttonPin2 = 3;// Connect output to push button 2
int relayPin2 = 10;// Connected to relay 2 (LED)


unsigned long relayOnTime; //reference for relay on
unsigned long relay2OnTime;

bool relayOn; // true or false if relay is on
bool relay2On;


void setup() {

 Serial.begin(9600);
 
 pinMode(buttonPin, INPUT);
 pinMode(relayPin, OUTPUT);
 
 pinMode(buttonPin2, INPUT);
 pinMode(relayPin2, OUTPUT);

 relayOn = false;
//  relay2On = false;

}

void loop() {

 if (digitalRead(buttonPin) == HIGH) {
   Serial.println("Light ON");
   digitalWrite(relayPin, LOW);
   relayOn = true; 
   relayOnTime = millis();
   
 } else if (digitalRead(buttonPin) == LOW) {
   Serial.println("Light OFF");
   digitalWrite(relayPin, HIGH);
   
   
   }
   if(relayOn){
     if(millis()- relayOnTime > 7000){
       digitalWrite(relayPin, HIGH);
       relayOn = false; 
       }
   }
 delay(100);
}

sketch_aug24MEGA.ino (1006 Bytes)

Just a small thought. Have you separately tested the timer code on its own and the relay code on its own by pressing button twice to reset.
Only when really happy with both, then try putting them together.

Wonder if your relay has diode on it..

OK, a number of points here.

  • Since you can read back the state of "relayPin", you do not need to use "relayOn" at all. Admittedly, it is trivially faster to read a variable than access a single pin, but this is so trivial that it is pretty irrelevant.
  • And speaking of "relayPin", you want to set it HIGH in setup() and do so before setting its pinMode, otherwise it will flash briefly at start-up.
  • Now, notice that your relay module (which you did not specify :roll_eyes: ) is active LOW? Well, your pushbuttons should also be wired between a pin and ground, not Vcc, and you can use a pinMode of INPUT_PULLUP. This is much neater in the wiring and avoids running Vcc anywhere it is not absolutely needed, a safety matter.
  • Where you specify the time delay as 7000, this needs to be 7000UL. This may be the problem you are seeing; I can't figure it yet.
  • Actually, the code you post so far seems to merely cause the relay to follow the button state - whenever the button is pressed, you turn it on, whenever the button is released, you turn it off.You need first to decide the relationship between holding the button and the time delay - do you want the relay to stay on while ever the button is held, or should it time out even if the button is held?