User Tools

Site Tools


oak:tutorials:blink

This is an old revision of the document!


Blinking the on-board LED

As with most microcontrollers, the first step is often to flash a “blink” program. It's very simple, is a good way to get familiar with the basics of writing code, and many boards (including the Oak) even include an on-board LED, so no additional components are necessary.

Start by making sure your Oak is associated with particle.io and that your Arduino environment is properly set up. We'll start with simply making the on-board LED (pin 1) blink:

// comments are inserted by putting two forward
// slashes at the beginning of a line

// the setup() loop sets pinModes, starts things like Serial,
// and is run once when the Oak powers on
void setup()
{                

  // initialize the digital pin as an output.
  pinMode(1, OUTPUT); //LED on Oak

}


// the loop() routine runs over and over again
void loop()
{

  // turn pin 1 "on" by making the voltage HIGH)
  digitalWrite(1, HIGH);
  
  // wait a second (1000 milliseconds)
  delay(1000);
  
  // turn the LED off by making the voltage LOW
  digitalWrite(1, LOW);

  // wait another second
  delay(1000);

}

You can find a template like this in the Arduino IDE under File → Examples → Oak Examples → Start. Upload the code, at which point you should see this in the message area at the bottom of the Arduino window:

Sketch uses 259,578 bytes (24%) of program storage space. Maximum is 1,040,368 bytes.
Global variables use 51,048 bytes (62%) of dynamic memory, leaving 30,872 bytes for local variables. Maximum is 81,920 bytes.
Using config file at: /path/to/.oak/config.json
Sending file to cloud, to flash device-name (Device ID: 123456789)
Flashing..........
Rebooting Oak.........
Oak Ready

You should see something like this (if the light isn't blinking below, click on the image to open in a new tab):

While the blink routine is a microcontroller classic, it's rarely very satisfying. Try playing with the delay values (for brevity, only the contents of loop() is being shown; you'll still need the setup() loop from above):

// the loop() routine runs over and over again
void loop()
{

  // faster blink
  digitalWrite(1, HIGH);
  delay(100);
  
  // longer off time
  digitalWrite(1, LOW);
  delay(2000);

}

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

If you upload this code, you will see this (click for enlarged view in new tab):

Congratulations! You've taken your first steps with the Oak. Take a look at some of the other tutorials for more!

oak/tutorials/blink.1458417084.txt.gz · Last modified: 2016/03/19 12:51 by jwhendy