Hi everyone,
i'm trying to make a project to turn off the LED for 1 min (5sec for testing) if detected a HIGH input, turn on the LED if detected LOW input and blink the LED if it is turn on for more than 3 mins(20sec for testing). I'm stuck at this coding for days but still can't figure out how to correct it. Any help would be great!
Below is the coding that i came up.
unsigned long offTime[4]; //Time individual input are turn "ON/HIGH"
unsigned long blink_interval = 1000;
unsigned long LED_ontime [4]; //Time individual input are left idle
unsigned long LED_blinktime [4]; //Time individual light last blinked
unsigned long idle_interval = 20000; //20sec for testing
unsigned long interval = 5000; //5sec for testing
int box[4]; //Array for 4 box
int pinCount = 4; //Assign pinCount as 4
int outputPins[] = {5, 4, 3, 2}; //Array for output pins in sequence
int inputPins[] = {A0, A1, A2, A3}; //Array for input pins in sequence
void setup() {
for (int thisPin = 0; thisPin < pinCount; thisPin++)
{
pinMode(inputPins[thisPin], INPUT);
}
for (int thisPin = 0; thisPin < pinCount; thisPin++)
{
pinMode(outputPins[thisPin], OUTPUT);
}
Serial.begin(9600);
}
void loop() {
for (int i = 0; i < pinCount; i++)
{
chamber[i] = digitalRead (inputPins[i]);
}
unsigned long currentMillis = millis(); //start millis
for (int i = 0; i < pinCount; i++) //use a for loop to check all 4 box
{
if(chamber[i] == HIGH) //if input == HIGH
{
digitalWrite(outputPins[i], LOW); //Set output==LOW to turn off the LED
offTime[i] = currentMillis; //Set offTime to current time when it is turned OFF/LOW
}
if( (chamber[i] == LOW) && (currentMillis - offTime[i] > interval) )
//if input==LOW and if time is more than interval
{
digitalWrite(outputPins[i], HIGH); //Set output==HIGH to turn on the LED
LED_ontime[i] = currentMillis - offTime[i] ; //Save the time the LED is last turn on
}
if (((currentMillis - LED_ontime[i]- offTime[i] > idle_interval) && (currentMillis - LED_blinktime[i] > blink_interval))|| ((LED_ontime[i] > idle_interval) && (offTime[i] == 0) && (currentMillis - LED_blinktime[i] > blink_interval)))
{
LED_blinktime[i] = currentMillis;
if (digitalRead(outputPins[i]) == LOW)
{
digitalWrite(outputPins[i], HIGH);
}
else digitalWrite(outputPins[i], LOW);
}
}
}
Thanks for reading and any input given!