How do i blink the LED's for 100 times and then turn them off with the micros()?

I tried adding a <while(blink<100){CODE}> but doesn't work :frowning:

// Which pins are connected to which LED
const byte greenLED = 12;
const byte redLED = 13;

// Time periods of blinks in milliseconds (1000 to a second).
const unsigned long greenLEDinterval = 500;
const unsigned long redLEDinterval = 1000;

// Variable holding the timer value so far. One for each "Timer"
unsigned long greenLEDtimer;
unsigned long redLEDtimer;

void setup ()
{
pinMode (greenLED, OUTPUT);
pinMode (redLED, OUTPUT);
greenLEDtimer = millis ();
redLEDtimer = millis ();
} // end of setup

void toggleGreenLED ()
{
if (digitalRead (greenLED) == LOW)
digitalWrite (greenLED, HIGH);
else
digitalWrite (greenLED, LOW);

// remember when we toggled it
greenLEDtimer = millis ();
} // end of toggleGreenLED

void toggleRedLED ()
{
if (digitalRead (redLED) == LOW)
digitalWrite (redLED, HIGH);
else
digitalWrite (redLED, LOW);

// remember when we toggled it
redLEDtimer = millis ();
} // end of toggleRedLED

void loop ()
{

// Handling the blink of one LED.
if ( (millis () - greenLEDtimer) >= greenLEDinterval)
toggleGreenLED ();

// The other LED is controlled the same way. Repeat for more LEDs
if ( (millis () - redLEDtimer) >= redLEDinterval)
toggleRedLED ();

/* Other code that needs to execute goes here.
It will be called many thousand times per second because the above code
does not wait for the LED blink interval to finish. */

} // end of loop

Your topic was MOVED to its current forum category as it is more suitable than the original

Add a counter in each toggle function that is incremented each time that the function is called.

Add a check for the value of the counter at the start of each function and if it 100 then return from the function immediately

I tried like this and does not work.

How should it be done?`


// Which pins are connected to which LED
const byte greenLED = 12;
const byte redLED = 13;

// Time periods of blinks in milliseconds (1000 to a second).
const unsigned long greenLEDinterval = 1000;
const unsigned long redLEDinterval = 1000;

// Variable holding the timer value so far. One for each "Timer"
unsigned long greenLEDtimer;
unsigned long redLEDtimer;

int counter=0;
void setup ()
{
pinMode (greenLED, OUTPUT);
pinMode (redLED, OUTPUT);
greenLEDtimer = millis ();
redLEDtimer = millis ();
} // end of setup

void toggleGreenLED ()
{
if(counter<=5){
if (digitalRead (greenLED) == LOW)
digitalWrite (greenLED, HIGH);
else
digitalWrite (greenLED, LOW);

// remember when we toggled it
greenLEDtimer = millis ();
}
counter++;

} // end of toggleGreenLED

void toggleRedLED ()
{if(counter<=5){
if (digitalRead (redLED) == LOW)
digitalWrite (redLED, HIGH);
else
digitalWrite (redLED, LOW);

// remember when we toggled it
redLEDtimer = millis ();
}
counter++;

} // end of toggleRedLED

void loop ()
{

// Handling the blink of one LED.
if ( (millis () - greenLEDtimer) >= greenLEDinterval)
toggleGreenLED ();

// The other LED is controlled the same way. Repeat for more LEDs
if ( (millis () - redLEDtimer) >= redLEDinterval)
toggleRedLED ();

/* Other code that needs to execute goes here.
It will be called many thousand times per second because the above code
does not wait for the LED blink interval to finish. */

} // end of loop

since green/redLEDtimers are only reset for the counter < 5 case, toggleGreenLED() and toggleRedLED() are called each iteration of loop after the counter exceeds 5.

this results in counter quickly incrementing and wrapping back to zero so that it looks like the counter is never stopping the toggle

counter should only be incremented for the case where the LED is toggled

it's also confusing that the same counter variable is incremented in both routines

You have changed the structure of the sketch. As now written the counter should only be incremented each time the state of the LED is changed

You should also consider using different counter variables for each LED to avoid interaction between them

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.