Hey guys,
I have been breaking my head over millis the past few days.
I want three different timings for a single led.
blink 3 times with a period of 500milliseconds; stay off for 10seconds.
unsigned long currentMillis = 0; // stores the value of millis() in each iteration of loop()
unsigned long previousOnBoardLedMillis = 0; // will store last time the LED was updated
byte ledstate=LOW;
void setup() {
pinMode(9,OUTPUT);
}
void loop() {
currentMillis=millis();
for(unsigned i=0; i<3; ++i){
blinkled();
digitalWrite(9,ledstate);
}
shut();
digitalWrite(9,ledstate);
}
void blinkled() {
if(currentMillis - previousOnBoardLedMillis >=500) {
// save the last time you blinked the LED
previousOnBoardLedMillis+=500;
// if the LED is off turn it on and vice-versa:
if (ledstate == LOW)
{
ledstate = HIGH;
}
else
{
ledstate = LOW;
}
}
}
void shut(){
if(currentMillis - previousOnBoardLedMillis >=10000){
previousOnBoardLedMillis+=10000;
ledstate=LOW;
}
}
This code doesnt seem to work. It blinks with 500millisec period but doesnt stay off for 10 seconds.
Your code seems simpler but the there seems to be a rather big glitch in it. Every once in a while, instead of staying off for 10 seconds, it would stay on. Any thoughts?
fame3096:
Your code seems simpler but the there seems to be a rather big glitch in it.
Every once in a while, instead of staying off for 10 seconds, it would stay on.
Any thoughts?
Yes.
You did not use my code (there is no 'once in a while' possibility in it).
unsigned long currentMillis = 0; // stores the value of millis() in each iteration of loop()
unsigned long previousOnBoardLedMillis = 0; // will store last time the LED was updated
byte ledstate=LOW;
void setup() {
pinMode(9,OUTPUT);
}
void loop() {
currentMillis=millis();
for(unsigned i=0; i<3; ++i){
blinkled();
digitalWrite(9,ledstate);
}
shut();
digitalWrite(9,ledstate);
}
void blinkled() {
if(currentMillis - previousOnBoardLedMillis >=500) {
// save the last time you blinked the LED
previousOnBoardLedMillis+=500;
// if the LED is off turn it on and vice-versa:
if (ledstate == LOW)
{
ledstate = HIGH;
}
else
{
ledstate = LOW;
}
}
}
void shut(){
if(currentMillis - previousOnBoardLedMillis >=10000){
previousOnBoardLedMillis+=10000;
ledstate=LOW;
}
}
You make a problem here. You set currentMillis once and then try to blink 3 times with the same value.
You did not use my code (there is no 'once in a while' possibility in it).
You could be writing to the pin with other code.
Sorry about that. I got rid of the issue. I made a mistake in implementing your code.
I added an else statement after your snippet : digitalwrite(ledpin,0) which I think contradicted with digitalWrite(ledPin, !digitalRead(ledPin));
Can you explain how digitalWrite(ledPin, !digitalRead(ledPin)); works?
I am sorry but my logic comprehension is poor. what state does true and false imply? For the 10 seconds off time, blinking is true, so I thought it should be the same in the else statement also.
Blinking means the blinking phase of your led cycle (3 on/off),
not blinking is the 10 second off phase.
It depends in which state you want to start, when the rate reaches the threshold
There it will be no real problem if you use the first version.
Normally the ten second no-blink-phase will be long over when the heartrate rises the next time.
fame3096:
I am sorry but my logic comprehension is poor. what state does true and false imply? For the 10 seconds off time, blinking is true, so I thought it should be the same in the else statement also.
false is zero, true is non-zero -- but you can make of them what you want.
This works for bits as well as variables which is why I doubt it will change with binary hardware. The CompSci priesthood likes to say it could change at any time but then they relate to hardware like a Victorian Maiden Aunt relates to sex.
How many programmers does it take to change a lightbulb? Can't be done, it's a HARDWARE PROBLEM.
But really, learn bit math. A single byte can hold 8 T/F states in order, an unsigned long holds 32. There are operations that you work all the bits between 2 integers in a single cycle rather than large/cumbersome if-type logic. http://playground.arduino.cc/Code/BitMath