Note: This guide assumes you have the Blink Sketch from the Examples menu of the Arduino IDE loaded and that the Qbot is successfully connected to your computer with the power off. To be clear I am not trying to turn anyone into a coder (though some of you may find this addictive and I apologize for that). I do want you to have a real sense of what coding is all about and what it's basic features are. Be calm!
Step 1: A simple view of an Arduino/Qbot
It helps to have a mental picture of what's going on inside the little brain that is the Arduino Uno (and all it's cousins like the Qbot). We upload a sketch (a program), which is a set of instructions, to the Uno and as long as the Uno has power it will do what that sketch tells it to do. If you turn the Uno off it remembers what is currently in it's brain (like a recipe) and next time you turn it on it goes back to doing what you asked. The IDE is how we create and upload new recipes. It may be helpful to realize that the Uno speaks a different language than we do and the process of translating what we write in the sketch into the language that the Uno can understand is one way of describing what the act of compiling does. You may have noticed earlier that when you uploaded the Blink sketch the first thing the IDE did was compile the code.
Step 2: Comments and Code:
In all coding languages you can broadly separate what is written in a sketch (program) into comments and and active code. While the comments don't 'do' anything they are a critical part of the general code. Even relatively simple code can be tough to figure out what it's doing depending on how it's written. Comments are the tool that we and all coders use to explain to other readers what is happening in the code. In the code for the Blink sketch you will notice lots of text that seems like a description of the code (which it is). How is the poor little brain of the Uno supposed to know this?
Notice that the first chunk of text has a /* on the line before the text starts and a */ further down. The /* and the */ indicate that anything between those two markers should be understood as commentary and not code.
Notice that further down in the sketch there are some odd looking lines and towards the end of the line there is this symbol - // - and what follows seems like normal text. This is another way to make a comment that only applies to what follows the // on the same line. Look through the code and find the examples of different ways to make comments.
/* Blink Turns an LED on for one second, then off for one second, repeatedly. Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to the correct LED pin independent of which board is used. If you want to know what pin the on-board LED is connected to on your Arduino model, check the Technical Specs of your board at: https://www.arduino.cc/en/Main/Products modified 8 May 2014 by Scott Fitzgerald modified 2 Sep 2016 by Arturo Guadalupi modified 8 Sep 2016 by Colby Newman This example code is in the public domain. http://www.arduino.cc/en/Tutorial/Blink */ // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin LED_BUILTIN as an output. pinMode(LED_BUILTIN, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level) delay(100); // wait for a second digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW delay(100); // wait for a second } }
Now what you're going to do is create a new sketch and copy and paste some code I wrote into that new sketch.
Step 3: QBlink: Start your first sketch
First create a folder on your computer for the Qbot Sketches you will create this term. Return to the IDE and the Blink sketch that you have open. From the File menu and selecting 'Save As' and save the sketch to your new folder with the name QBlink (or something else if you prefer). Delete all of the existing code in the sketch and then copy the code below and paste it into your QBlink sketch. If this is a new process for you ask around or ask me during class.
I hope that you end up with all of the code below pasted into your sketch with little difficulty. Check to be sure you got the /* at the top and the '}' at the end. These are easy to miss in the copy/paste process. Save the QBlink sketch to capture these changes. Find my name in the comment block at the beginning and add your name below it using the same format. Time to own any changes you make!
/* QBlink Turns an LED on for one second, then off for one second, repeatedly. This version of Blink allows the user to make an arbitrary pin to 'blink' Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to the correct LED pin independent of which board is used. For the purpose of this version of Blink you can chose which pin you wish to use to 'blink' and LED. To chose a new pin merely change the pin number after #define testPin 13 to whatever pin you would like to use. You can change the timing of the blinks just as you did with the original Blink sketch. modified 8 May 2014 by Scott Fitzgerald modified 2 Sep 2016 by Arturo Guadalupi modified 8 Sep 2016 by Colby Newman modified 6/15/20 by Bruce Emerson */ //Define the pin I'm testing with this sketch #define testPin 13 // the setup function runs once when you press reset or power the board void setup() { // initialize the testPin as an output. pinMode(testPin, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(testPin, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for some time in ms digitalWrite(testPin, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for some time in ms // You can turn the pin off and on in more complex patterns to mimic Morse code // or other patterns of interest by adding more lines after the ones above. }
In the long run you may choose to start with a completely blank sketch to write your code but this is the easiest way to get you off to a quick start.
Step 4: QBlink: Structure of the Sketch
Now we need to talk about the structure of a sketch. Think of this as much like discussions you've had in writing classes about the structure of an essay. At first it may seem confusing but it will become clearer as you keep practicing and thinking about it.
There are three sections to the code part of the sketch. Only the setup() and loop() are required but if that's all we talk about then you'll be confused when something happens outside of those two sections.
The Preamble
Imagine you are going to drive your car to school or go for a bike ride. There are a bunch of things you do even before you get ready to go. You may check to see if your friend wants a ride, you remember whether you filled it with gas yesterday or pumped up the tires. These are all things that take place in preparation for what you are going to do to make the process smoother. They are not absolutely required in order to drive to school or ride your bike but they help set the stage so it goes more smoothly. This is like the preamble.
This is where we can (but don't have to) set the stage for our sketch. We can define useful variables or bring on useful tools that other coders have developed called libraries. We will see some of these later.
In the QBlink sketch notice that the first thing that isn't a comment or a blank line is a statement that says
#define testPin 13
This is a specific command to the IDE that any time I use the name testPin I want the sketch to know it means the number 13 (kinda like that algebra stuff from long ago). If I want to change the meaning of testPin I just edit the sketch to change the 13 to something else. This all takes place before the first formal section of code called ....
The setup()
When you are headed off the drive the car there are a number of critical things you need to do before actually driving to school. You have to find the right key, make sure you have your glasses and license, and finally you need to start the car which you do first and hopefully only once before doing the repetitive task of driving from place to place.
This is section of the code is what the sketch does first and only once. As we will see the main portion of the code (the loop() section) is repeated forever until the power is turned off. The setup() is executed first and only once. We can do a lot of different things in the setup() portion of the code. The formal structure of the setup is
void setup(){
// in here goes anything we want to do first and only once in our sketch.
}
One of the things we will learn about code is that it is very uptight about details. That means the ( ) and the { } and the spelling of the words are all critical. Mess it up even a little and the IDE will freak out. Nobody will be hurt but it can be frustrating trying to notice where you used a ; instead of a : in a long bit of code.
Inside the set() is what we call any code that occurs between the { and the }. In our QBlink code there is this statement:
pinMode(testPin, OUTPUT);
Each pin on the Arduino or Qbot is a place where it connects to the outside world. The pin can be a place where we send information to the world (like turning on a wheel motor) or a place where we are getting information from the world (like does the collision avoidance system see a wall nearby). The Qbot needs to know what we want to do. pinMode() is a special command for setting up a pin for either INPUT or OUTPUT. In this case we want pin 13 to be an output pin (to turn on an LED) so we use the command above. Remember that we defined testPin to be 13 in the preamble.
NOTICE: Notice the ';' at the end of the line of code. This is critical for the IDE to know where the end of your command is. The most common early error we all make writing sketches is to forget to put the ';' at the end of a line
Once the Qbot has done everything inside the setup() it moves on to.....
The loop()
This is the main portion of the sketch and is much like the actual task of driving a car. Driving a car or riding a bike is the continuous execution of a set of skills and decisions that keep repeating over and over again until you turn off the car. We're assuming you're not getting distracted.
Here is what is in the loop() section of the sketch. Notice that what we're looking at lies between a { and a } many lines later much like the setup() portion of the sketch.
// the loop function runs over and over again forever void loop() { digitalWrite(testPin, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for some time in ms digitalWrite(testPin, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for some time in ms // You can turn the pin off and on in more complex patterns to mimic Morse code // or other patterns of interest by adding more lines after the ones above. }
There are only two commands here so lets look at them individually.
digitalWrite(testPin, HIGH);
digitalWrite is a command that works sort of like a light switch. If I make a pin HIGH that is like turning on a switch. If I make it LOW it's like turning it off. Inside the parentheses (testPin, HIGH) there are two bits of information. These are called arguments which will be hard to remember but it's easier to remember that if you tell the Qbot to turn the switch (digitalWrite) then the Qbot needs to know which switch (testPin, ) and whether to turn it on ( ,HIGH) or off ( ,LOW). You could translate the command above to say "Qbot, please turn switch 13 on.". Are you confident what this next command means?
digitalWrite(testPin, LOW);
The last command we need to learn is the delay() command;
delay(1000);
delay() tells the Qbot just what you would expect - "Sit tight please for 1000 somethings before you do the next thing in the loop()." The command is expressed in units of ms (1/1000 of a sec). So delay(1000); is 1000 ms which is just 1 second.
Translated into a form of English the whole loop says:
Turn switch 13 on
Wait 1 second before
Turn switch 13 off
Wait 1 second
Go back to the begining of the loop()
That's a pretty good description of a blinking light eh?
Your task:
Change the delay times to make the red LED (pin 13) blink more slowly or more rapidly. Also make it stay on longer than it stays off and vice versa.