Introduction: Tutorial 1: Flashing Light
This is the very first tutorial in a set of tutorials introducing students to Arduinos. The tutorials use Grove modules and Libraries, so that every component added to the project is an object with its own methods to call.
This project is simply making a light flash.
The wiki for the LED can be found here: https://docs.google.com/document/d/16PjgJa6EOTU2Pw7gFAdgUt8tYK8eJsXtQvOHXAlh_6w/edit?usp=sharing.
If you need the LED Library, make a folder called LED in your Arduino Libraries folder, and add these 2 files below to that folder.
Supplies
FROM YOUR KIT:
1 x Arduino with Grove Shield
1 x USB-B (Arduino end) to USB-A (laptop end).
1 x USB-A to USB-C adaptor (if using a MAC with no USB-A port)
1 x Grove LED
1 x Grove cable
BYO:
1 x Laptop with Arduino IDE software
Step 1: Build Circuit
A PDF of the instructions is provided below.
Attachments
Step 2: Code
Open the Arduino IDE Software on your laptop (e.g. do COMMAND SPACE and type 'Arduino').
COMMAND N will create a new document. It will have some precode already in there.
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
Delete all this and then copy and paste the following.
//Add Libraries
#include "Led.h"
//Global Variables
//Create Objects
Led led1(2);
//Setup
void setup() {
}
//Main Loop
void loop() {
led1.on();
delay(500);
led1.off();
delay(500);
}
Let's look at each part and what it does.
#include "Led.h"
This gives us access to the Led Library, a bunch of functions/methods related to the Led.
Led led1(2);
The 'Led' at the start creates a new object, which is an Led (that's the class type).
The 'led1(2)' gives the name for our object, so if we want to give commands for this object, we can address it by typing 'led1'. When we create the object, we have to state which PIN on the Arduino it is connected to. As we plugged the LED into D2, it is connected to PIN 2. So that is what the '2' in the brackets is doing.
void setup() {
}
Note, the setup function is EMPTY as there is no setup code required.
void loop() {
led1.on();
delay(500);
led1.off();
delay(500);
}
This is the main LOOP which keeps repeating from top to bottom until the Arduino is switched off.
What does led1.on() do? Well the 'led1' at the start is telling us we want to give an instruction to our object called 'led1'. We then put a 'dot' or 'full stop' after it, and then we state the method (function) we want that object to execute. In this case, we want it to execute the on() function. After that, we WAIT for 500 ms (half a second) using the delay(500) function. When we once again call the led1 object, but this time we want the object to execute the off() function. We then wait another half second, before returning to the top and repeating it all over again.
Step 3: Run the Program
Once you push UPLOAD, the lights should start flashing. A PDF of these uploading instructions with images and info is given below.
Attachments
Step 4: Challenge - Edit the Code - Make It Flash Faster/Slower
Ok... now it's your turn to go to the code and try and change something so that the light flashes on and off twice as fast.
See how you go. You can check out the answer by going here:
Then try and change the code again to make it flash half as quickly as it did originally.
See how you go. You can check out the answer by going here:
Step 5: An Introduction to the for Loop
Try changing the code to the following:
//Add Libraries
#include "Led.h"
//Global Variables
int i = 0;
//Create Objects
Led led1(2);
//Setup
void setup() {
}
//Main Loop
void loop() {
for (i=1000; i>=100; i=i-100) {
led1.on();
delay(i);
led1.off();
delay(i);
}
for (i=100; i<=1000; i=i+100) {
led1.on();
delay(i);
led1.off();
delay(i);
}
}
What's different? We added a variable with this:
int i = 0;
When you add a variable, you start by giving a data type (integer, character, float etc., then a name for the variable, and optionally, can set it to an initial value (i = 0 means "set the variable 'i' to 0").
When we declare the variable at the start, we start with a data type at the front... but now when we want to use the variable, we simply type the name.
The main changes to our code was in the main loop:
void loop() {
for (i=1000; i>=100; i=i-100) {
led1.on();
delay(i);
led1.off();
delay(i);
}
for (i=100; i<=1000; i=i+100) {
led1.on();
delay(i);
led1.off();
delay(i);
}
}
It's really in 2 parts.
Let's look at the first part.
for (i=1000; i>=100; i=i-100) {
led1.on();
delay(i);
led1.off();
delay(i);
}
Whenever you want to make a FOR loop, you start with this:
for (initial value of variable; condition in which loop continues; how variable changes) {
}
You start by writing for, then a space, then brackets. 3 things go into the brackets, with a semi-colon used to separate them. The first is the initial value of your variable (e.g. i = 1000, so we start at 1000). The second is the condition for which we keep going (e.g. i >=100... this reads as "i is greater than or equal to 100"... so we'll keep iterating through the loop until i is NOT greater than or equal to 100). The final part is how our variable changes each time (e.g. i = i + 100, means for each iteration, i will increase by 100).
After the normal brackets, you then have a set of squiggly brackets. What needs to happen for each iteration of the FOR loop goes inside these squiggly brackets.
For our first LOOP we have:
for (i=1000; i>=100; i=i-100) {
led1.on();
delay(i);
led1.off();
delay(i);
}
Note, it's just like our original program inside the squiggly brackets, but instead of delay(500), we have delay(i). Since i is a defined variable, whatever value it currently holds will be the TIME (in ms) that we wait before doing the next thing. In this for loop, i starts at 1000, and goes down by 100 each time. So the delay times will be 1000, 900, 800, 700, 600, 500, 400, 300, 200, 100. But once we get to 0, i is no longer greater than or equal to 100, so it won't go through. This will be the end of the loop.
This loop should therefore make the flashing get quicker and quicker each time.
Note the second part of the MAIN LOOP is a second FOR LOOP.
for (i=100; i<=1000; i=i+100) {
led1.on();
delay(i);
led1.off();
delay(i);
}
Just look at it... especially the numbers in the normal brackets. What do you think this FOR loop will do?
Try copying and pasting the whole program and uploading it. See what happens to the way the light flashes. Does it make sense based on the code?







