Hi,
I'm Ash from Malaysia.
Anyway about my question could i count the amount of time that a led have turn on with blink without delay?
in my set up i want to make an led to turn on for 5 second and off for 5 second, the led will blink for maybe 50 time counting only when it is on. but for every 10 time it has blink, the amount it turn off reduce by one.
like
count | time on | time off
0-10 5 5
11-20 5 4
21-30 5 3
31-40 5 2
41-50 5 1
should i use if and if...else or is there any other way?
ash901226:
Hi,
I'm Ash from Malaysia.
Anyway about my question could i count the amount of time that a led have turn on with blink without delay?
in my set up i want to make an led to turn on for 5 second and off for 5 second, the led will blink for maybe 50 time counting only when it is on. but for every 10 time it has blink, the amount it turn off reduce by one.
like
count | time on | time off
0-10 5 5
11-20 5 4
21-30 5 3
31-40 5 2
41-50 5 1
should i use if and if...else or is there any other way?
Yes. You can use a if/else if or a switch statement to determine the off time.
A for loop is usually used for doing something a fixed number of times and its counter variable is often used within the loop to do something based on its value. Unless there is something inside the loop to slow it down then it will run very fast as Nick says.
Some people use a while or for-next loop or series of NOP's to delay less than 4 micros. Every instruction has a certain number of cycles, at 16 million cycles per second, to perform.
OP, something else you should know is arrays of variables. You don't need 5 different-name time-off's with a code section for each but a 5-element array of time-off's to choose which to use by index.
Also, time variables should be unsigned long unless you have a huge number of those. Reason is that Arduino time is given in milliseconds since start as an unsigned long and using variables of the same type saves type conversion. If memory space is an issue then yes, let it convert but otherwise it is cleaner and quicker not to.
For type unsigned long add UL to the end of the constant number to tell the compiler "unsigned long".
1 second is 1000UL milliseconds, 5 seconds == 5000UL.
Unsigned math is especially useful for time, even across the rollover differences will be correct.
const int Led=13;
void setup()
{
pinMode(Led,OUTPUT);
}
void loop()
{
static unsigned long startTime;
static int count = 0;
static int state = 0;
switch (state){
case 0: // Turn ON
digitalWrite(Led,HIGH);
startTime = millis();
state = 1; // Stay ON till OFF time
break;
case 1: // Wait until OFF time
if(millis() - startTime > 5000) // After 5 seconds
{
digitalWrite(Led,LOW);
count++;
state=2; // Stay OFF till ON time
startTime = millis();
}
break;
case 2: // Wait until next ON time
if(millis() - startTime > (5000 - (count/10)*1000)) // After 5/4/3/2/1 seconds
{
state=0; // Turn ON
if (count >= 50)
state = 3; // After 50 cycles, stop
}
break;
case 3: // Stopped
break;
}
}
That article is confused. Once you work in unsigned's, you forget about negative.
This time check works no matter what start, end and wait times you feed it, but you need all three.
unsigned long start, end, wait;
start = 0xFFFFFFF0; end = 0x100; wait = 0x100;
if ( end - start >= wait ) {
println( "end - start >= wait" );
}
else
{
Serial.println( "wait is not over yet" );
}
Put all that in setup(), nothing in loop(), change start and end and wait all you want,
there is no need to flag on millis() rollover nor any correction to make upon such flag.
void loop()
{
for (int i = 0; i<50;i++)
{
switch (state){
case 1:
digitalWrite(Led,HIGH);
currentMillis = millis();
state=2;
it compile but cant do anything at all
Hi Ash,
You don't need a for loop because the whole loop() function gets called over and over already by the Arduino framework. You only need to keep track of when things need to happen, and if it's time for something to happen. See if you can understand this version. Good luck!
const int LED=11;
void setup(){
pinMode(LED, OUTPUT);
}
long nextTime=0;
bool nextState=HIGH;
long count;
void loop(){
long currentTime=millis();
if (currentTime < nextTime) return; // if not time for something to happen, just return
// it's now time to make nextState happen
if (nextState==HIGH){
// turn it on and schedule OFF in 5 seconds
digitalWrite(LED, HIGH);
nextState=LOW;
nextTime = currentTime + 5000;
}
else {
// turn it off and schedule ON in the right number of seconds according to "count"
digitalWrite(LED, LOW);
nextState=HIGH;
if (count < 11){ nextTime=currentTime + 5000; }
else if (count < 21){ nextTime=currentTime + 4000; }
else if (count < 31){ nextTime=currentTime + 3000; }
else if (count < 41){ nextTime=currentTime + 2000; }
else if (count < 51){ nextTime=currentTime + 1000; }
else {
// what should happen after it has blinked 50 times?
}
count = count + 1; // or just "count++'"
}
}