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)