Finish your programs from last week STOPLIGHT CIRCUIT! You may need … – int – void setup() – void loop() – pinMode – digitalWrite – delay
A Game The rules: We will present a scenario and it is your job to figure out what happened. You can ask questions that can be answered with a true or a false.
What Happened? Mr. Brown was spending the night in a hotel in a strange city. He had never been there before and didn't know anyone in the hotel or the city. He had great difficulty getting to sleep. No matter what, he couldn’t fall asleep. He had a busy day ahead and wanted to have a good night of sleep. He made one local telephone call and shortly thereafter he fell into peaceful slumber. Why?
Solution He couldn't go to sleep because the man next door was having a party. Brown called the night clerk, who went to end the party. When the noise stopped, Mr. Brown had no trouble in dozing off. Computer programming is like this game because we are limited to asking the computer yes/no questions.
If Statement (Conditional Statement) Allows you to make something happen or not depending on whether a given condition is true or not. – Example: Every Friday I nap for 2 hours. – What is the true/false question here? if (day == Friday) { naptime = 2; } What is the difference between = and == ? apples = 5; // ASSIGNS the value 5 to the variable // called apples apples == 5; // LOGICAL statement. Asks, “is the value of // apples 5?” if it is, apples == 5 is TRUE. What will apples == 4 return?
IF-ELSE On Fridays I nap for 2 hours, but every other day I nap for 1 hour. if (day==Friday) { naptime = 2; } else { naptime = 1; } Why are some of the equal signs double (==) and some of them just single (=)?
IF -ELSE-IF On Fridays I nap for 2 hours. On Saturdays, I don’t nap at all. Every other day I nap for 1 hour. if (day == Friday) { naptime = 2; } else if (day == Saturday) { naptime = 0; } else { naptime = 1; }
Recap IF (SOME CONDITION IS TRUE) { //do something } ELSE IF (SOME OTHER CONDITION IS TRUE) { //do something else! } ELSE { //do some other stuff } CONDITIONAL OPERATORS x == y (x is equal to y) x != y (x is not equal to y) x < y (x is less than y) x > y (x is greater than y) x <= y (x is less than or equal to y) x >= y (x is greater than or equal to y)
Let’s Practice So far we’ve only used digitalWrite – to OUTPUT a value on a digital pin. Now we want to read a value. We’ll connect a button to a pin What values can the button have? – HIGH or LOW 5V Arduino Digital pin
Program Outline What happens when we push the button? if (button_value == LOW) { //turn light OFF } else if (button_value == HIGH) { // turn light on! }
Let’s Write the Program! Open a new sketch What three parts does your program need? – Define variables – Run setup – Run loop
“Vocabulary” What functions do we know already? – digitalWrite – pinMode – delay Today we’ll add: – digitalRead – if/else (not a function, but a control statement)
int ledPin = 13; // LED connected to digital pin 13 int buttonPin = 7; // pushbutton connected to // digital pin 7 int buttonState = 0; // variable to store the read value void setup() { // set the digital pin 13 as output pinMode(ledPin, OUTPUT); // set the digital pin 7 as input pinMode(buttonPin, INPUT); }
void loop() { // read the input pin buttonState = digitalRead(buttonPin); // Determine if we should turn on the LED if (buttonState == HIGH) { digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); }
Recap IF (SOME CONDITION IS TRUE) { //do something } ELSE IF (SOME OTHER CONDITION IS TRUE) { //do something else! } ELSE { //do some other stuff } CONDITIONAL OPERATORS x == y (x is equal to y) x != y (x is not equal to y) x < y (x is less than y) x > y (x is greater than y) x <= y (x is less than or equal to y) x >= y (x is greater than or equal to y)