html5-img
1 / 15

Finish your programs from last week

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.

kamana
Download Presentation

Finish your programs from last week

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Finish your programs from last week • STOPLIGHT CIRCUIT! • You may need … • int • void setup() • void loop() • pinMode • digitalWrite • delay

  2. 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.

  3. 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?

  4. 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.

  5. 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?

  6. 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 (=)?

  7. 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; }

  8. 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)

  9. 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 Digital pin Arduino

  10. Program Outline • What happens when we push the button? if (button_value == LOW) { //turn light OFF } else if (button_value == HIGH) { // turn light on! }

  11. Let’s Write the Program! • Open a new sketch • What three parts does your program need? • Define variables • Run setup • Run loop

  12. “Vocabulary” • What functions do we know already? • digitalWrite • pinMode • delay • Today we’ll add: • digitalRead • if/else (not a function, but a control statement)

  13. intledPin = 13; // LED connected to digital pin 13 intbuttonPin = 7; // pushbutton connected to // digital pin 7 intbuttonState = 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); }

  14. 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); } }

  15. 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)

More Related