I am looking to use the basic blink without delay program in multiple led blinks.I have made a program using delay().How can i replace delay() with millis().Please advice to what are the changes i have to be done for achieving the target.
i am new in Arduino UNO and try to study the program.
int led = 13;
int led2 = 12;
int led3 = 11;
int timer = 1000;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(timer); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(timer);
digitalWrite(led2, HIGH);
delay(timer);
digitalWrite(led2, LOW);
delay(timer);}
digitalWrite(led3, HIGH);
delay(timer);
digitalWrite(led3, LOW);
delay(timer);}// wait for a second
}
The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking and it blinks a few LEDs. It may help with understanding the technique.
unsigned long startTimes[] = {0, 0, 0, 0}; //start time of current blink for each LED
unsigned long periods[] = {500, 510, 520, 530}; //period for each LED
const byte ledPins[] = {3, 5, 6, 9}; //the LED pins
const int numberOfLeds = sizeof(ledPins) / sizeof(ledPins[0]); //calculate the number of LEDs
void setup()
{
Serial.begin(115200);
Serial.print("Number of LEDs : ");
Serial.println(numberOfLeds);
for (int pin = 0; pin < numberOfLeds; pin++) //use the number of LEDs to set pinMode()s
{
pinMode(ledPins[pin], OUTPUT);
}
}
void loop()
{
unsigned long currentTime = millis(); //get current time for all timing in loop()
for (int led = 0; led < numberOfLeds; led++) //for each LED
{
if (currentTime - startTimes[led] > periods[led]) //if the period has ended
{
digitalWrite(ledPins[led], !digitalRead(ledPins[led])); //change the state of the LED
startTimes[led] = currentTime; //save the start time of the blink for the current LED
} //end of test for period ended
} //end of for loop
} //end of loop()
Please read the code and comments so that you learn something. It presumably does not do exactly what you want but it is easy to change the LED pin numbers, how many LEDs there are and the periods. Note the use of arrays and a for loop to shorten the code and the need to keep loop() running freely
Please read the code and comments so that you learn something. It presumably does not do exactly what you want but it is easy to change the LED pin numbers, how many LEDs there are and the periods. Note the use of arrays and a for loop to shorten the code and the need to keep loop() running freely
Sir,
in my program led will get ON one by one.That is first led will ON after 1 minute it will get OFF and Led2 get ON again after 1 minute Led2 get OFF and Led3 get ON ..This cycle will repeat..
but as per your program leds getting ON/OFF Randomly.
The fact that you say that tells me that you do not understand how the program works so probably don't understand the use of millis() for timing. The LEDs most certainly do not go on and off randomly
I am sorry but your description of what you want is not clear, possibly because you did not use any commas in the description so it is impossible to tell what timing belongs to which LED. From your program that uses delay()s I believe that what you want is for each LED to come on in turn for 1 minute then off for 5 minutes
If that is the case then a 1 minute time period using millis() would be good. Set a counter to zero initially. When the period ends increment the counter. When it reaches 6 set it back to zero. The program can be in one of 6 states. In 3 of them an LED is on, in 3 of them all LEDs are off
Then, in loop() if the counter is 0 turn on LED1 and turn the others off. If the counter is 2 turn on LED2 and turn the others off. If the counter is 4 turn on LED3 and turn the others off. If the counter is 1, 3 or 5 turn off all of the LEDs
If that is not the pattern that you want then please explain in more detail what it is you want
If you are going out at 6pm and its noon, you don't sit and look at your watch for 6 hours waiting for 6pm
(well I hope not). You do other things and regularly check the time. When its time, you go out.
sreekanthmp:
I have made the program using delay() and its working(program already posted in ma first post).Now i want to repalce the delay() with millis().
Have you carefully studied the example programs you have been given in Replies #1 and #2? What have you learned from them?
That is not what your program using delay()s does. In that program there is a delay() between turning LED1 off and LED2 on
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(timer); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(timer);
digitalWrite(led2, HIGH);
delay(timer);
digitalWrite(led2, LOW);
delay(timer);
I suppose that I should point out that the original program that you posted does not compile because the loop() function ends too early and so there is code outside of a function
UKHeliBob:
I suppose that I should point out that the original program that you posted does not compile because the loop() function ends too early and so there is code outside of a function
sorry sir,
Now i corrected that mistake and compile,its ok
int led = 13;
int led2 = 12;
int led3 = 11;
int timer = 1000;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(timer); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
digitalWrite(led2, HIGH);
delay(timer);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
delay(timer);
digitalWrite(led3, LOW);
}
OK, now the code matches your description and it compiles
Now you only have 3 states instead of 6
Start by writing a small program using millis() for timing that increments a counter (starting at zero) each time the timing period ends. When the counter reaches 3 set it back to zero. At the start of each timing period print the value of the counter. Don't worry about the LEDs at this time
HINT : most of the work is already done for you in the BlinkWithoutDelay example. Come back when you have got the simple (no LED) program working or if you are stuck.
Here is your code with millis() function in place of delay() function.
int led = 13;
int led2 = 12;
int led3 = 11;
int timer = 1000;
// the setup routine runs once when you press reset:
void setup()
{
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop()
{
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
timeDelay();//delay(timer); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
digitalWrite(led2, HIGH);
timeDelay();//delay(timer);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
timeDelay();//delay(timer);
digitalWrite(led3, LOW);
}
void timeDelay()
{
unsigned long presentMillis = millis();//read present time of Arduino Timer
while(millis()- presentMillis <= 1000) //checking if 1000 ms has elapsed
{
; //do nothing
}
}
ARKF:
Here is your code with millis() function in place of delay() function.
int led = 13;
int led2 = 12;
int led3 = 11;
int timer = 1000;
// the setup routine runs once when you press reset:
void setup()
{
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop()
{
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
timeDelay();//delay(timer); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
digitalWrite(led2, HIGH);
timeDelay();//delay(timer);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
timeDelay();//delay(timer);
digitalWrite(led3, LOW);
}
void timeDelay()
{
unsigned long presentMillis = millis();//read present time of Arduino Timer
while(millis()- presentMillis <= 1000) //checking if 1000 ms has elapsed
{
; //do nothing
}
}
Here is your code with millis() function in place of delay() function.
Congratulations. You have just invented the delay() function.
Your code blocks the free running of loop() just as effectively as if you had used delay().
Here is a challenge for you. Add code to blink a fourth LED or print a message every 100 milliseconds whilst the three LEDs continue their sequence.
sreekanthmp:
thank you ARKF...its working perfectly...
See my comment above. The delay() function has been replaced by a function that still blocks the free running of loop(). If that is acceptable then you might just as well use delay()
UKHeliBob:
Congratulations. You have just invented the delay() function.
It has been there for years; we failed to discover it; but, @ARKF (with only 5 posts) has brought it to the OP. Congratulation is not enough; I would offer + for his quick understanding of what the OP has wanted -- a simple functional replacement of delay() function by millis() function.
ARKF:
Here is your code with millis() function in place of delay() function.
int led = 13;
int led2 = 12;
int led3 = 11;
int timer = 1000;
// the setup routine runs once when you press reset:
void setup()
{
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop()
{
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
timeDelay();//delay(timer); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
digitalWrite(led2, HIGH);
timeDelay();//delay(timer);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
timeDelay();//delay(timer);
digitalWrite(led3, LOW);
}
void timeDelay()
{
unsigned long presentMillis = millis();//read present time of Arduino Timer
while(millis()- presentMillis <= 1000) //checking if 1000 ms has elapsed
{
; //do nothing
}
}