I've been trying many solutions and I can't get over it. Please help. I'd really appreciated.
I'd like to turn on and off the LED on the specific time like at the 2 second has passed turn on the LED1 then stay until at 56 second, turn it off, etc.
I don't wanna use delay cause it only do 1 work at a time.
......
There are 3 LEDs in this case.
LED1 : turn on at time 00.02s ( 2 second hass passed) then turn OFF at 00.56s (stay ON until 56 second has passed then turn it OFF ).
LED2 : turn on at the beginning at 0 second then turn it OFF at the very end of the process which is at 56 second later ( means stay ON for 56 second)
LED3 :
turn on at 00.20s to 00.22s then turn if OFF at 00.39s (means stay ON for 2 second)
turn on at 00.37s to 00.39s then turn if OFF at 00.39s (means stay ON for 2 second)
turn on at 00.54s to 00.56s then turn if OFF at 00.56s (means stay ON for 2 second)
The best thing i can do is only 2 led the third LED I don't how it'll work because it take 2 different timeframe the first one wait aroud 18 second to turn LED3 ON, the second event need only 10 second to turn LED3 ON.
I can't figure it out pleaseeeee, i'm begging you guys. Thank you
// These variables store the flash pattern
// and the current state of the LED
int led1 = 1; // the number of the LED pin
int ledState1 = LOW; // ledState used to set the LED
unsigned long previoustime1 = 0; // will store last time LED was updated
long OnTime1 = 56000; // milliseconds of on-time
long OffTime1 = 2000; // milliseconds of off-time
int m2 = 2; // the number of the LED pin
int ledState2 = LOW; // ledState used to set the LED
unsigned long previoustime2 = 0; // will store last time LED was updated
long OnTime2 = 2000; // milliseconds of on-time
long OffTime2 = 0; // milliseconds of off-time
void setup()
{
// set the digital pin as output:
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
}
void loop()
{
// check to see if it's time to change the state of the LED
unsigned long currentMillis = millis();
if((ledState1 == HIGH) && (currentMillis - previoustime1 >= OnTime1))
{
ledState1 = LOW; // Turn it off
previoustime1 = currentMillis; // Remember the time
digitalWrite(led1, ledState1); // Update the actual LED
}
else if ((ledState1 == LOW) && (currentMillis - previoustime1 >= OffTime1))
{
ledState1 = HIGH; // turn it on
previoustime1 = currentMillis; // Remember the time
digitalWrite(led1, ledState1); // Update the actual LED
}
if((ledState2 == HIGH) && (currentMillis - previoustime2 >= OnTime2))
{
ledState2 = LOW; // Turn it off
previoustime2 = currentMillis; // Remember the time
digitalWrite(led2, ledState2); // Update the actual LED
}
else if ((ledState2 == LOW) && (currentMillis - previoustime2 >= OffTime2))
{
ledState2 = HIGH; // turn it on
previoustime2 = currentMillis; // Remember the time
digitalWrite(led2, ledState2); // Update the actual LED
}
}
This is not very clear. Do these cycles repeat every minute? Does , for example, 'turn on at 00.20s' mean that it turns on at 20 seconds into each new minute?
first, good recognizing delay() is a garbage way to code.
couple general suggestions:
everything should be unsigned in your case, you have literally no need for negative values. and the rollovers may cause you problems, though they are will happen eventually anyways.
and since you are new, I suggest the "uint8_t" system, thats an 8bit unsigned int. an unsigned long would be "uint32_t". this system works much better cross platforms. You dont need to add any libraries, or do anythiing special in arduino.
you may consider making an array of nextOn & nextOff time, tiddy it up a bit.
why is offtime2 set to 0? the end of your last else if will always be true.
I appologize for unclear, do only once, no repeat.
for example, it's like a clock but it doesn't relate to real time. Time start when arduino start at the same time. In 1 minutes (60seconds) when the time passed by 2 seconds from the beginning led the LED1 ON, after that the clock is still running 3 seconds 4 seconds, 5seconds,....,etc.
then the clock reaches 20 seconds which realate to arduino clock, LED3 is ON for only 2 seconds then the if OFF, the clock is still runing 22 seconds, 23 seconds,......
until it reaches 37 seconds, let LED3 ON again for 2 seconds and then turn it OFF .
Maybe it would be clearer if you draw a time line that shows the state of each led.
On the Y axis show seconds starting a 0 and progressing to say 60.
On the X axis show the ON and OFF states for each of the 3 leds.
Do it by hand and post a picture of it.
** edit **
Ok. In the meantime it has been clarified.
No functional difference, but by declaring a variable it automatically has a data type, whereas using a #define it doesn't, which is all too easy to forget and have it default to being an int