This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
oak:tutorials:blink [2016/03/22 09:05] jwhendy |
oak:tutorials:blink [2016/03/23 16:26] jwhendy |
||
|---|---|---|---|
| Line 73: | Line 73: | ||
| 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. | 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. Make sure the port and board selections are 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: | + | 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 on, make sure the Arduino board selection is correct, and then upload the code with the button above the text window with a right arrow icon: |
| + | |||
| + | [[http://digistump.com/wiki/_media/oak/tutorials/upload.png|{{http://digistump.com/wiki/_media/oak/tutorials/upload.png?300}}]] | ||
| + | |||
| + | You should see this in the message area at the bottom of the Arduino window: | ||
| <code> | <code> | ||
| Line 89: | Line 93: | ||
| [[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}}]] | ||
| - | ===== Code: changing LED behavior ===== | + | === changing LED behavior === |
| Now that you have a general idea of how to turn a light on and off, what 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!) | Now that you have a general idea of how to turn a light on and off, what 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!) | ||
| Line 116: | Line 120: | ||
| </code> | </code> | ||
| - | With this code, the light will blink for a shorter time and stay off longer. | + | With this code, the light will blink for a shorter time and stay off longer. |
| - | + | ||
| - | === Code: a for() loop === | + | |
| - | + | ||
| - | You can also define the delays using variables and loops to make things more interesting: | + | |
| - | + | ||
| - | <code> | + | |
| - | // 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); | + | |
| - | + | ||
| - | } | + | |
| - | + | ||
| - | } | + | |
| - | </code> | + | |
| - | + | ||
| - | Here is what happens each time the loop runs: | + | |
| - | + | ||
| - | ^time through loop ^i ^on_time ^off_time| | + | |
| - | |1|0|0|500| | + | |
| - | |2|50|50|450| | + | |
| - | |3|100|100|300| | + | |
| - | |.|.|.|.| | + | |
| - | |.|.|.|.| | + | |
| - | |10|450|450|50| | + | |
| - | 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): | + | ===== 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! |