I am trying to lit LEDs of an 8-bit LED arry one after one with the delay of 1 second. I want to eliminate the delay() used in for loop & use millis() instead. Any clues on how to implement the logic?
Any help shall be appreciated.
int i; // Led pin variable (PIN 6 to 13 to be used)
//unsigned long change = 0;
void setup() {
// put your setup code here, to run once:
digitalWrite (i, LOW); // pull-down activated
}
void loop() {
// put your main code here, to run repeatedly:
for ( i = 6; i <= 13; i++ ){ // lit every led & stay for 1 sec
pinMode (i, OUTPUT); // set pin as output
digitalWrite (i,HIGH); // lit LED
delay(1000); // stay lit
digitalWrite (i,LOW);// off led
}
}
I omitted the "for" loop, the code is working but are there any other trickier way of doing this?
int i = 6; // Led pin variable (PIN 6 to 13 to be used)
unsigned long change = 0; // hold the timestamp of last led on
void setup() {
digitalWrite (i, LOW); // pull-down activated
}
void loop() {
if((millis() - change) >= 1000){ // interval of every 1 second
if (i == 14){ // if pin 13 is exceeded, reset to pin 6
digitalWrite((i-1),LOW);// off previous led
i = 6; // reset to pin 6
}
digitalWrite((i-1),LOW);// off previous led
pinMode (i, OUTPUT); // set pin as output
digitalWrite (i,HIGH); // lit LED
i++; // increase pin no of led to lit.
change = millis(); // catch the latest time stamp
}
}
add this after loop, so you're creating a for:next loop with time element mixed in to control when the for:next is advanced:
void loop(){
currentMicros = micros();
elapsedMicros1 = currentMicros - previousMicros1;
// time to change to next led?
if (elapsedMicros1 >= ledTime){ // ledTime = 1000000UL, 1 second
previousMicros1 = previousMicros1 + ledTime; // time of next led change
// turn off current LED
digitalWrite (ledPin, LOW);// off led - pin 6 turned on in setup
// point to next LED, if have reached end then reset point
ledPin = ledPin +1;
if (ledPin == 14){
ledPin = 6;
}
// turn on next LED
digitalWrite (ledPin,HIGH);// on led
} // end time check
} //end loop
Thanks for the solution, I want to know a few points, why & when should we use micros() instead of millis()? Can't find any satisfactory answer in the micros() reference page. micros() - Arduino Reference
I put unsigned long ledTime = 1000000; it is working so when & where to declare the interval followed by UL?
One last thing, after uploading the sketch the first LED that lits is LED 2 (Pin7) not LED 1.
I think we have already increased the ledPin number in the first loop. The code is working absolutely fine just wanted to get things done in a more accurate way.
CrossRoads:
pull-down activated
There are no pull-down resistors, if that is what this refers to.
No, there is no resistors, I mean initially off the LED.
Thanks for all your help.
int ledPin = 6; // Led pin variable (PIN 6 to 13 to be used)
unsigned long ledTime = 1000000;
unsigned long currentMicros; // holds current timestamp
unsigned long elapsedMicros1; // holds time elapsed since last blink
unsigned long previousMicros1; // holds the timestamp os last blink
void setup() {
}
void loop(){
currentMicros = micros(); // check the time
elapsedMicros1 = currentMicros - previousMicros1; // check the elapsed time since last blink
// time to change to next led?
if (elapsedMicros1 >= ledTime){ // ledTime = 1000000UL, 1 second
previousMicros1 = previousMicros1 + ledTime; // time of next led change
// turn off current LED
digitalWrite (ledPin, LOW);// off led - pin 6 turned on in setup
// point to next LED, if have reached end then reset point
ledPin = ledPin +1;
if (ledPin == 14){
ledPin = 6;
}
// turn on next LED
pinMode(ledPin,OUTPUT);
digitalWrite (ledPin,HIGH);// on led
} // end time check
} //end loop
why & when should we use micros() instead of millis()?
micros as the name says is for timing microseconds and as such the counter wraps round much sooner than the millis. Also the millis time is more accurate than the micros.
digitalWrite (ledPin, LOW);// off led - pin 6 turned on in setup
micros is more accurate then millis, as GM says. I find myself using it more frequently.
UL for time related elements - I've been adding that with more regularity to avoid type mismatch inconsistency issues.
If you don't know this, use Tools/Auto Format Ctrl+T to format your code.
/*
LEDs using the TimedAction library for the Arduino.
*/
#include <TimedAction.h> //see: http://playground.arduino.cc/Code/TimedAction
TimedAction leds = TimedAction(1000UL,ledChange); //every 1000ms Call ledChange
// ***********************************************
byte ledPin = 5;
// ***********************************************
void setup()
{
} //********* END OF setup()*********************
void loop()
{
leds.check(); //is it time to check the leds?
// Other things to be done goes here
} //********* END OF loop()**********************
// Functions
// ***********************************************
void ledChange() //update leds
{
digitalWrite (ledPin, LOW);// off led - pin 6 turned on in setup
// point to next LED, if have reached end then reset point
ledPin = ledPin +1;
if (ledPin == 14)
{
ledPin = 6;
}
// turn on next LED
pinMode(ledPin,OUTPUT);
digitalWrite (ledPin,HIGH);// on led
} // **** END OF ledChange() ****
// ***********************************************
// END OF CODE
// ***********************************************
Thanks all for the great support. Get to know about something new, the TimedAction. I shall keep that in mind. Pardon, I didn't know the auto-format thing, I shall use that option further on.
Thanks for the huge support
Good day.