User Tools

Site Tools


oak:tutorials:advanced-leds

This is an old revision of the document!


In progress!

Oak: Advanced LEDs


Code: for() loop

You can also define the delays using variables and loops to make things more interesting:

// now we've added two variables to hold our
// on and off time delays
// the "int" variable is able to hold integer values
int on_time;
int off_time;

void setup() 
{                

  pinMode(1, OUTPUT); //LED on Oak

}

void loop()
{

  // the for() loop will initialize a new temporary variable, i
  // it runs until i >= 500, adding 50 each time
  for(int i = 0; i < 500; i = i+50)
  {

    // the on time will equal i (0, 50, 100, ...)
    on_time = i;
    
    // the off time will equal 500 - i (500, 450, 400, ...)
    off_time = 500 - i;

    digitalWrite(1, HIGH);
    delay(on_time);
  
    digitalWrite(1, LOW);
    delay(off_time);
  
  }
  
}

Here is what happens each time the loop runs:

time through loop i on_time off_time
100500
25050450
3100100300
....
....
1045045050

When the for() loop above runs, i is equal to 0. It calculatesIf you upload this code, you will see this (click for enlarged view in new tab):

oak/tutorials/advanced-leds.1458694812.txt.gz · Last modified: 2016/03/22 18:00 by jwhendy