User Tools

Site Tools


oak:tutorials:blink

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
oak:tutorials:blink [2016/03/19 12:51]
jwhendy
oak:tutorials:blink [2016/03/22 14:02]
jwhendy
Line 1: Line 1:
-==== Blinking ​the on-board LED ====+===== Oak: 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.+A common first project ​with a microcontroller ​is to "​blink" ​an LED. It's very simple, is a good way to get familiar with the basics of writing code, and many boards (including the Oak) include an on-board LED, so no additional components are necessary. A tutorial using additional LEDs can be found [[oak:​tutorials:​leds|here]].
  
-Start by making ​sure your Oak is associated with [[https://​www.particle.io/​|particle.io]] and that your Arduino ​environment ​is [[http://​digistump.com/​wiki/​oak/​tutorials/​arduino|properly ​set up]]. We'll start with simply making ​the on-board LED (pin 1) blink:+===== Components used ===== 
 + 
 +^ Part       ^ Quantity ​         ^Identification^ 
 +| Oak with soldered headers |1| | 
 + 
 +While we only need an Oak for this lesson, be sure to make sure you have completed the steps in the "​Getting Started"​ section of the [[http://​digistump.com/​wiki/​oak?​redirect=1|tutorials]]. In particular, ensure that your Oak is associated with [[https://​www.particle.io/​|particle.io]] and that your Arduino ​IDE  ​is [[http://​digistump.com/​wiki/​oak/​tutorials/​arduino|properly ​configured]]. 
 + 
 +===== Concepts ===== 
 + 
 +=== digitalWrite() === 
 + 
 +''​digitalWrite(pin_number,​ value)''​ is the command used to send a ''​HIGH''​ or ''​LOW''​ voltage to the pin of a microcontroller. In the case of the Oak, pin 1 is connected to a small surface mounted LED on the board. We will be turning the voltage on and off to this pin to turn this LED on and off. 
 + 
 +=== pinMode() === 
 + 
 +Microcontrollers can often change the functionality of a pin internally by using code. For pins that are meant to "​listen,"​ for example when connected to a sensor, we use ''​pinMode(pin_number,​ INPUT)''​. When we plan to "​send"​ a signal, we use ''​pinMode(pin_number,​ OUTPUT)''​. Since we will be sending voltage //to// the LED, we will use ''​OUTPUT''​ for the mode of pin 1
 + 
 + 
 +=== delay(=== 
 + 
 +The ''​delay(milliseconds)''​ function is used to take a break from the code that's being run. Code is written in lines, and each line is read and execute one at a time. When the microcontroller encounters the ''​delay()''​ function, it waits for that many milliseconds before moving on to the next line. This is important for timing the blinking of the LED. 
 + 
 + 
 +===== Code ===== 
 + 
 +=== The blink sketch === 
 + 
 +This code will make the on-board LED connected to pin 1 blink. It will turn on for 1 second, and then off for one second, over and over:
  
 <​code>​ <​code>​
Line 14: Line 41:
 {                ​ {                ​
  
-  // initialize ​the digital pin as an output.+  // initialize digital pin as an output.
   pinMode(1, OUTPUT); //LED on Oak   pinMode(1, OUTPUT); //LED on Oak
  
Line 25: Line 52:
  
   // turn pin 1 "​on"​ by making the voltage HIGH)   // turn pin 1 "​on"​ by making the voltage HIGH)
 +  // the light will turn on here
   digitalWrite(1,​ HIGH);   digitalWrite(1,​ HIGH);
   ​   ​
   // wait a second (1000 milliseconds)   // wait a second (1000 milliseconds)
 +  // since we told the voltage to be on,
 +  // it will stay on while the Oak waits
   delay(1000);​   delay(1000);​
   ​   ​
Line 34: Line 64:
  
   // wait another second   // wait another second
 +  // since the voltage was low when the Oak
 +  // started to wait, it will stay off during this time
   delay(1000);​   delay(1000);​
  
Line 39: Line 71:
 </​code>​ </​code>​
  
-You can find a template like this in the Arduino IDE under ''​File -> Examples -> Oak Examples -> Start''​. ​Upload ​the codeat which point you should see this in the message area at the bottom of the Arduino window:+This code tells the Oak that we will be using pin 1 as an output, and then loops over and over sending a ''​HIGH''​ signal to pin 1 (turns the LED on), waits 1 second, and then turns the LED off by sending a ''​LOW''​ signal. 
 + 
 +You can find a template like this in the Arduino IDE under ''​File -> Examples -> Oak Examples -> Start''​. ​Otherwise, start a new sketch and copy/​paste ​the above into your file. With your Oak plugged powered onmake sure the Arduino board selection is correct, and then upload the code (the button above the text window with a right arrow icon). You should see this in the message area at the bottom of the Arduino window:
  
 <​code>​ <​code>​
Line 51: Line 85:
 </​code>​ </​code>​
  
-You should see something like this (if the light isn'​t ​blinking below, click on the image to open in a new tab):+Once the Oak receives the code, it will blink as shown here (if the image isn'​t ​animated, click to open in a new tab):
  
 [[http://​digistump.com/​wiki/​_media/​oak/​tutorials/​blink-simple.gif|{{oak:​tutorials:​blink-simple.gif?​300}}]] [[http://​digistump.com/​wiki/​_media/​oak/​tutorials/​blink-simple.gif|{{oak:​tutorials:​blink-simple.gif?​300}}]]
  
-While the ''​blink''​ routine is microcontroller classicit'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):+=== changing LED behavior === 
 + 
 +Now that you have general idea of how to turn a light on and offwhat else could you do? How might you make the blink shorter or longer? What do you think this changed code might do? (Hint: compare ​the delay values ​after each step!)
  
 <​code>​ <​code>​
 +void setup()
 +{                ​
 +
 +  pinMode(1, OUTPUT); //LED on Oak
 +
 +}
 +
 // the loop() routine runs over and over again // the loop() routine runs over and over again
 void loop() void loop()
Line 73: Line 116:
 </​code>​ </​code>​
  
-You can also define the delays using variables and loops to make things more interesting:​ +With this code, the light will blink for a shorter ​time and stay off longer
- +
-<code+
-// now we've added two variables to hold our +
-// on and off time delays +
-int on_time; +
-int off_time; +
- +
-void setup()  +
-{                 +
-  pinMode(1OUTPUT); //LED on Oak +
-+
- +
-void loop() +
-+
- +
-  // the for() loop will initialize ​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);​ +
-   +
-  } +
-   +
-+
-</​code>​+
  
-If you upload this code, you will see this (click for enlarged view in new tab):+===== Conclusion =====
  
-[[http://​digistump.com/​wiki/​_media/​oak/​tutorials/​loop-changing-blink.gif|{{oak:tutorials:​loop-changing-blink.gif?​300}}]]+Congratulations ​-- you've uploaded your first sketch to the Oak! In this lesson, you were able to tackle all of these first steps, which is a definite accomplishment:
  
 +  * pre-requisites:​ upgrading your Oak and setting up the Arduino IDE
 +  * writing a sketch in the Arduino IDE
 +  * uploading a sketch
 +  * setting a pin as an ''​OUTPUT''​
 +  * understanding the ''​loop()''​ section, which does something again and again
 +  * sending a ''​HIGH''​ and ''​LOW''​ voltage to a pin
 +  * using ''​delay()''​ to affect the behavior of an LED
  
-Congratulations! You've taken your first steps with the Oak. Take a look at some of the other [[http://​digistump.com/​wiki/​oak?​redirect=1|tutorials]] ​for more!+Keep your momentum going and take a look at the [[http://​digistump.com/​wiki/​oak/tutorials/leds|next tutorial]] which will get you started wiring up LED bulbs, resistors, and some more advanced coding!
oak/tutorials/blink.txt · Last modified: 2016/03/23 16:26 by jwhendy