Millis and micros issues

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

Can anyone help me??
Many thanks.

dark_zone:
Can anyone help me??

No.

Not until you post the code you are using. Using the "code" tags - the scroll with the brackets icon.

// 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>

</mark> <mark>[/code]</mark> <mark>
tags added.

There's at least part of your problem:

double interval = 500;

ALWAYS use 'unsigned long' type when using millis() or micros().

This section does exactly nothing:

  switch (X)
  {
   case 1000:  
     { 
     break;
     }
  }

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);
  } 
}

There are some numbers that aren't done in the correct time

And I can't find a pattern in the error :confused: :slightly_frowning_face:

unsigned long currentMillis = micros();

Confusing variable name or what ?

As to your problem. micros() does not increment in steps of 1, rather it increments in steps of 4. Could this be a contributory factor ?

dark_zone:
There are some numbers that aren't done in the correct time

And I can't find a pattern in the error :confused: :slightly_frowning_face:

What numbers are producing incorrect results?

Yes it's confusing name

The first no. Will be the time in microseconds and the second will be the no. Of times

80000,1000

90000,1000
91000,1000
93000,1000
94000,1000

This is the no. I tried

Thanks for trying to help me