If I may ask
I am using the function millis and micros to make the led on leg 13 to blink without delay in other functions
I'm defining the blinking with the pulse rise time and number of blinks. But in many times it exceed the time it supposed to finish in it
For example if I am using micros and the rise time is 500 and number of blinking is 20000
The the full cycle time is 1000 milliseconds and the full time is 20 seconds
In many cases it finish either before or after the supposed time
// constants won't change. Used here to
// set pin numbers:
const int led = 13; // the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
double interval =500; // interval at which to blink (milliseconds)
int value=10000; //number of BLink time
int X=0; // counter
void setup() {
// set the digital pin as output:
pinMode(led, OUTPUT);
}
void loop()
{
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = micros();
if(X<(unsigned long)value)
{
if((double)(currentMillis - previousMillis) > interval)
{
// save the last time you blinked the LED
previousMillis= currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
{ ledState = HIGH; }
else
{ledState = LOW; X=X+1; }
}
}
digitalWrite(led, ledState);
// when X reaches to required value , stop this program
switch (X)
{
case 1000:
{
break;
}
}
}
Moderator edit: </mark> <mark>[code]</mark> <mark>
This is how you get your program to stop blinking after 1000 blinks. At 500 microseconds per half cycle that will be one millisecond per blink and therefore ten seconds for 10,000 blinks.
// constants won't change. Used here to
// set pin numbers:
const int led = 13; // the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
unsigned long previousMicros = 0; // will store last time LED was updated
unsigned long interval = 500; // interval at which to blink (microseconds)
const int value = 10000; //number of Blink time
int X = 0; // counter
void setup() {
// set the digital pin as output:
pinMode(led, OUTPUT);
}
void loop() {
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMicros = micros();
if(X < value) {
// Every 500 microseconds
if(currentMicros - previousMicros > interval) {
// save the last time you blinked the LED
previousMicros= currentMicros;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else {
ledState = LOW;
X += 1;
}
}
digitalWrite(led, ledState);
}
}