Hello,
I am using Elegoo Uno, Elegoo 8 channel relay module and 4 push buttons.
I am trying to turn on the LED on relay using the push putton
If i press 1st push button all LEDs on the relay are on for 15, push button 2nd turns on the LEDs for 30 seconds, push button 3rd turns on the LEDs for 45 seconds and the last push button turn the device on or off.
/*
const int pushButton[] ={2,3,4,5};// define push button inputs
const int relayPin[]={11,10,9,8};// output pins where 4 relays will be connected
//String relayNames[] ={"CH1", "CH2","CH3","CH4"};// Just put name for 4 relays
int pushed[] ={0,0,0,0};// status of each buttons
int relayStatus[] ={HIGH,HIGH,HIGH,HIGH};// initial status of relay
int ledOn = 0; // Variable for all leds to be on
void setup() {
//Initaite serial communication ,Setting up serial print library
Serial.begin(9600);
for(int i=0; i<4; i++)
{
pinMode(pushButton[i], INPUT_PULLUP);
pinMode(relayPin[i], OUTPUT);
digitalWrite(relayPin[i], HIGH);// initial relay status to be OFF
}
}
void loop() {
for (int i=0; i<4; i++)
{
int val = digitalRead(pushButton[i]);
if(val == HIGH && relayStatus[i] == LOW){
pushed[i] = 1-pushed[i];
delay(100);
}
relayStatus[i] = val;
if (pushed[0] == 1){
Serial.println("Led On for 15 seconds");
Serial.println("============================");
ledOn = true;
delay(15000); // led on for 15 seconds
}
if (pushed[1] == 1){
Serial.println("Led On for 30 seconds");
Serial.println("============================");
ledOn = true;
delay(30000); // led on for 30 seconds
}
if (pushed[3] == 1){
Serial.println("Led On for 45 seconds");
Serial.println("============================");
ledOn = true;
delay(45000); // led on for 45 seconds
else {
ledOn = 0; // as off button for all leds using pin 5
}
}
if (ledOn) {
digitalWrite(relayPin[0,1,2,3], HIGH); // turning all the leds on
}
}
After I press the buttons couple of times in the Serial monitor it gives it the Line " Led On for 15 seconds "
Then "Led On for 45 seconds" but the lights do on turn on at all.
I am trying to change the code I found in this youtube video