# Micros problem

/* Blink without Delay

// constants won't change. Used here to
// set pin numbers:
const int ledPin =  13;      // the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousmicros = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (microseconds)

void setup() {
// set the digital pin as output:
pinMode(ledPin, 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(currentmicros - previousmicros > 1) {
// 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;

// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}

When the program showed interval 1 micro secondif (currentmicros - previousmicros > 1),
why did the oscilloscope result showed 11 micro second? (attachment picture)

Because that takes more than 1 uS to execute.

The idea works OK for longer intervals, but you only get 16 clock cycles in 1 uS and those instructions will take more than 16 cycles.

When the program showed interval 1 micro secondif (currentmicros - previousmicros > 1),

Besides, that would be expected to take at least 2 uS. After all, you are testing for > 1 not >= 1.

thanks nick:D for the reply.
How many cycles do we need for certain arduino instruction?
Is there any reference for it?

How many cycles do we need for certain arduino instruction? Is there any reference for it?

All in the processor datasheet.

The slowest line in that loop is the digitalWrite call. You will get a higher frequency if you use direct port access.

micros() returns an unsigned long, not a long,?