Blink Chip’s Eyes with Arduino (Preview)
In this lesson we will learn how to control Chip’s Eyes with an Arduino and some code.
What you will need for this lesson:
- Chip
- A Salduino UNO R3 or an Arduino Uno R3 (or any compatible platform)
- USB Cable
- Arduino IDE installed on your PC or MAC
Previously you completed the Arduino IDE install and you tried the Blink example from Arduino. The Blink example only blinks one led, connected to pin 13 on the Arduino. Chip has two LEDs, connected to pin 10 and pin 11 on the Arduino.
Read the code below. Step through it line by line. What do you think it will do? What do you think each line means?
int rEye = 10; int lEye = 11; void setup(){ pinMode(rEye, OUTPUT); pinMode(lEye, OUTPUT); } void loop(){ digitalWrite(rEye, HIGH); digitalWrite(lEye, HIGH); delay (1000); digitalWrite(rEye, LOW); digitalWrite(lEye, LOW); delay (1000); }
Have some ideas about what that code does? Great! Lets step through that code again, this time with some comments that explain what each section does. The “//” signifies a comment. A comment is ignored and not sent to the Arduino. Comments are there for us humans, to make code more readable.
// Chip has 3 important pins // Ground (GND) -- plugged into to Arduino GND // Right Eye LED (D10) --> plugged into to Arduino D10 // Left Eye LED (D11) --> plugged into to Arduino D11 // // We need to define Chip's eyes so we can use them in our code int rEye = 10; //define right eye as pin10 on the Arduino int lEye = 11; //define right eye as pin11 on the Arduino // Next we need to initialize the Arduino pins // setup() only runs once when you power on or reset an Arduino void setup(){ pinMode(rEye, OUTPUT); //set right eye for output pinMode(lEye, OUTPUT); //set left eye for output } //loop() runs forever - this is our main code void loop(){ // turn both eyes on. HIGH means set voltage to HIGH digitalWrite(rEye, HIGH); digitalWrite(lEye, HIGH); delay (1000); //wait for 1 second. Delay is set in miliseconds or 1/1000 of a second // turn right eye off. LOW means set voltage to LOW. digitalWrite(rEye, LOW); digitalWrite(lEye, LOW); delay (1000); //wait for 1 second. Delay is set in miliseconds or 1/1000 of a second // we are at end of loop - we go back to line 21 }
Time to make Chip blink.
- Plug Chip into your Arduino.
- Connect the Arduino to your computer.
- Launch the Arduino IDE.
- Start a new sketch. On the Arduino menu go to FILE -> NEW
- Copy the code from the example sketch above. Paste into your Arduino IDE sketch.
- Save the sketch. File -> SAVE (name it something such as sketch_Chip_Blink)
You should have a screen that looks like this:
Verify the Sketch – click on the check mark icon. This will verify that your code does not have any syntax or procedural errors. However, it does not verify that your code will do what you want. That is up to you.
If the code is ok, you will not see an errors. If there are any errors, double check that you did not miss anything when you copied it from the above example.
You are ready to upload the code from your computer to the Arduino. Click the Upload Icon arrow, which is next to the verify button.
After the upload is successful, Chip should be blinking at you. Congrats!
Now we can play. Try these challenges:
– What happens with if you change the delay to larger or smaller values?
delay (1000);
– What happens with if you change put a delay in a different spot? Try different variations, different values.
digitalWrite(rEye, HIGH); delay (1000); digitalWrite(lEye, HIGH); delay (1000); digitalWrite(rEye, LOW); delay (1000); digitalWrite(lEye, LOW); delay (1000);
Don’t forget to verify and upload your code after each change.
Play around, you can not break anything. You can always re-copy and upload the original sketch.
Access all the code for Chip
GitHub is a website where you can store, share, and manage code for projects. We use it store all the code for Chip’s lessons so that it is easy for you to download. If you have code you would like to contribute, please do so. You can access Chip’s Github Repository here.