Programming 101: YABWD

I got a new Mega 2560 and had trouble getting the 64-bit Win7 machine to communicate with it. After finding the solution here (thanks!) I was testing it out with the \File\Examples\02.Digital\BlinkWithoutDelay sketch.

Just to prove to myself that upload and execution were occurring as expected I changed the value of interval. After that, and purely out of curiosity about the millis() function, I had to introduce a variable interval for the blink.

In case anyone is interested I am attaching the modified code.

-CH-

BlinkWithoutDelayVariableInterval.ino (2.78 KB)

Don't make people download such a small piece of code.

/* Blink without Delay
 
 Turns on and off a light emitting diode(LED) connected to a digital  
 pin, without using the delay() function.  This means that other code
 can run at the same time without being interrupted by the LED code.
 
 The circuit:
 * LED attached from pin 13 to ground.
 * Note: on most Arduinos, there is already an LED on the board
 that's attached to pin 13, so no hardware is needed for this example.
 
 created 2005
 by David A. Mellis
 modified 8 Feb 2010
 by Paul Stoffregen
 modified 11 Nov 2013
 by Scott Fitzgerald
 modified 27 Jan 2016
 by Charles Hudson
 
 
 This example code is in the public domain.
 
 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
 */

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

// Variables will change :
int ledState = LOW;             // ledState used to set the LED

// Generally, you shuould use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

long interval = 100;           // interval at which to blink (milliseconds)

int bump = 100;   // amount to increase or decrease interval

void setup()     // runs once
{
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);      
}

void loop()
{
  // runs continually; 
  // note that variables pass and currentMillis are declared anew with each iteration of loop()
  
unsigned long currentMillis;   // for keeping track of the time in the current interval
int pass = 1;   // for controlling on-off of LED

  // 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.
 
    do{
      
      currentMillis = millis();
      if(currentMillis - previousMillis >= interval) // it's time for a change of state on the LED, so:
      {  
        // save the last time you changed the LED's on / off state 
        previousMillis = currentMillis; 
        
        // 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);
        pass++;
       }
      
     // make two passes of the do-while loop; once for on, once for off 
     } while (pass < 3);

    // at interval upper and lower limits change direction to increase or decrease interval 
    // from interval of 100 up to 1200 ms and back down again 
    if ((interval < 100) || (interval > 1200)) bump = bump * -1;  

    interval = interval + bump;  
    // back to top of loop() to repeat using a different interval
}