06.2: Time to take the wheel
Assignment #06
Name Driver’s Ed / Arduino Party Game
Assigned 2013-10-22
Due In class

When you do drive your car, how often are you driving somewhere you’ve already been? Most of the time, right? You drive to school, you drive to work, you drive home, you drive to the store… most of the time, you’re going to the exact same places you always go to – it’s really pretty simple. Every now and then, though, something comes up and you drive somewhere new. Thankfully, you know how to drive and you have access to a car – so you can drive yourself there. But before cars existed, do you think people had any idea what they were missing out on? Nope! That’s just how things were, so nobody realized what life could be like.

Now, how often do you use a computer? Every four hours? Every two hours? Every ten minutes? We use computers all the time. Think back about that small handful of places you drive to – what if you didn’t have a car? What if you had to use public transportation to make your way to those places? A little inconvenient, maybe, but you could probably make it work, right? But what happens if you need to go somewhere new? You check the bus routes and hope one of them is nearby, but if not, what do you do? Pay a taxi driver a lot of money so that you can pretend to be interested in the conversation while secretly hoping he doesn’t realize how uncomfortable you are, or that you’re planning escape methods if he turns out to be crazy.

Yeah, computers are like cars. Except you use computers more frequently while knowing less about how to use them. Sorta like being a kid and not being able to drive yourself wherever or whenever. Consider the following: most of our drives are uneventful. Driving is so mundane that we have write laws to remind people that it’s a bad idea to use a cell phone while steering a hunk of metal attached to wheels. But people still do it – even when traveling 88 feet per second! People don’t appreciate how complicated driving really is. You follow the speed limit, stay in your lane, stop at lights, signal your turns, merge onto highways, check your blind spots, avoid all of the terrible drivers, negotiate round-abouts, and arrive safely. You probably don’t think about it much, but that really takes some serious skill, and you weren’t always that good at it.

So, welcome back to Driver’s Ed – today, you take the wheel. Here are some basics.

Comments

// Two forward-slashes indicate a comment
// Comments are notes to humans reading the program
// The computer will kill all humans ignore all comments

I’ll use comments in example below to explain what the code will do

Variables

Variables are like storage containers in a house. The Arduino likes to be efficient, so when you need storage, you have to tell it two things: what type of storage you need, and the name that you’ll use to retrieve the contents of that storage (that would be the data type and variable name.

int myAge;  // This tells the computer we want it to remember an integer
            // and we'll call that variable "myAge"

myAge = 28; // Tells the computer to store the number "28" in the
            // container named "myAge"

We can put a value into a variable as soon as it’s created as well:

float myHeightInFeet = 5.583;

Functions

A program is like a house, and functions are like rooms in a house. Each room has a specific purpose, and when you want to do something, you go to the room designated for that.

A function must indicate 4 things: what type of data it returns, the name of the function, what type(s) of data it expects, and the list of instructions for this function.

Enter the room w/ empty pockets, leave empty handed

void sayHi(){
    Serial.println("Howdy!"); // Sends text over the USB cable
}

Enter the room with an ‘int’ in your pocket, leave the room with a ‘float’ in your hands:

float cutInHalf(int number){
    return 0.5 * number;
}

Default functions for Arduino

Every Arduino program must have a setup and loop function defined.

The setup function is only processed once, and it happens as soon as the Arduino receives power. The loop function is called immediately after that, and as soon as the loop function is complete, it’s processed again – and again, and again, and again… forever… in a loop!

void setup(){
    // Instructions to process exactly one time when Arduino is powered on
    // Often used to set pin modes
}

void loop(){
    // Instructions to process repeatedly after setup() is complete
    // As soon as these instructions are complete, Arduino does them again
}

pinMode

If you want to use a pin on the Arduino for input or output, you have to tell the Arduino. You only need to tell it once, so this is typically performed in the setup function:

void setup(){
    pinMode(3, OUTPUT); // Set pin 3 for output
    pinMode(4, INPUT);  // Set pin 4 for input
}

Controlling Electricity

You can supply 5 volts of electricity on any of the Arduino pins or take 5 volts of electricity from a pin.

void setup(){
    pinMode(3, OUTPUT); // Set pin 3 for output
}

void loop(){
    digitalWrite(3, HIGH); // supply 5 volts
    delay(1000);           // sleep for 1 second
    digitalWrite(3, LOW);  // take 5 volts
    delay(1000);           // sleep for 1 second
}

Practical variable use

In the above example, we have to indicate pin 3 multiple times. Instead of remembering which pin we’re using and retyping it each time, we can have the computer remember for us:

int LED_PIN = 3;

void setup(){
    pinMode(LED_PIN, OUTPUT);
}

void loop(){
    digitalWrite(LED_PIN, HIGH); // supply 5 volts
    delay(1000);                 // sleep for 1 second
    digitalWrite(LED_PIN, LOW);  // take 5 volts
    delay(1000);                 // sleep for 1 second
}

Conditional statements

A conditional statement allows us to process a set of instructions only when some conditions are true. For example, we may want to do something when a button is pressed. Here’s an example program which reads from a button (you may want to ask how to wire this up!):

int BUTTON_PIN = 4;

void setup(){
    pinMode(BUTTON_PIN, INPUT);
}

void loop(){
    int buttonValue = digitalRead(BUTTON_PIN);
    if(buttonValue == LOW) {
        // do something!
    }
}


More resources:

I can try to give you directions to every possible destination, but I’d be doing you a disservice if you didn’t learn how to navigate yourselves.

There are a LOT of good examples built directly into the Arduino IDE. Just click on “File” and then “Examples”! You can find more examples all over the internet, and a good place to start is the official Arduino Tutorial.

On the Arduino website, you’ll also find a good reference manual.


Trade me seats!

You probably feel like you had more instruction before you sat behind the wheel for the first time. Thankfully, computers are a little more forgiving. If you crash this bad boy, it’s really that anybody will die or even be hurt, or that you’ll really do any damage whatsoever… imagine if you had that sort of freedom with a car!


Arduino Party Game

Objective: Earn the highest level in the class by completing tasks with an Arduino.

Rank Tasks
Lvl 1 Pawn None – Everybody starts here
Lvl 2 Pawn Make an LED blink
Lvl 3 Pawn Make a motor “blink”
 
Lvl 4 Knight Change the LED blink speed
Lvl 5 Knight Make 2 LEDs blink using 2 different pins
 
Lvl 6 Rook Control LED with a button
Lvl 7 Rook Control LED blink rate with a potentiometer
 
Lvl 8 Queen Light 3 LEDs, in sequence, repeatedly
Lvl 9 Queen  Press the button to enable/disable the above sequence
Lvl 10 Queen Control the speed of the above sequence with a potentiometer
 
Boss Challenge Using buttons and LED’s, build a traffic light system

Rules:

  1. Initially, Tom, Dom, and the peer leaders are Supreme Rulers of All Living Things (and a few that are dead). Everybody else is a lame pawn.
  2. To be promoted a person has to show that they can complete the given tasks which earn the next ranking.
  3. Anybody who has a ranking is qualified to help, evaluate, and finally promote somebody who has a lower level than them.
  4. Help each other and at the same time, honor the Honor System regarding evaluation and promotion. Please don’t promote somebody until you are absolutely sure they understand how to do the tasks. If you aren’t sure, find a Supreme Ruler of All Living Things (and a few that are dead).
  5. Each rank earns a sticker. Wear your stickers on your left front so people know your ranking and can ask you for help/evaluation/promotion.

Print This Page