Arduino Programming: “if” statements

Slides:



Advertisements
Similar presentations
Selection Feature of C: Decision making statements: It allow us to take decisions as to which code is to be executed next. Since these statements control.
Advertisements

Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
If Statements, Try Catch and Validation. The main statement used in C# for making decisions depending on different conditions is called the If statement.
Lecture 3: Control Structures - Selection BJ Furman 10SEP2012.
Digital & Analog Inputs. Review Fundamental parts of an Arduino program are … Setting output types using pinMode. Declaring variables Can write a digital.
Intro to Programming and Microcontrollers. Activity Group into pairs and sit back-to-back. Pick one person who is going to draw. The other person will.
Control Flow C and Data Structures Baojian Hua
Week 10 Recap CSE 115 Spring For-each loop When we have a collection and want to do something to all elements of that collection we use the for-each.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
Finish your programs from last week STOPLIGHT CIRCUIT! You may need … – int – void setup() – void loop() – pinMode – digitalWrite – delay.
Selection. Structured Components There are three basic constructs that can be used to build programs Sequence One statement follows another. Selection.
Embedded Programming and Robotics Lesson 2 C Programming Refresher C Programming1.
chipKit Sense Switch & Control LED
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 3. Selection Constructs.
Cascade switching of an LED EAS 199B Winter 2013.
Chapter 4 Selection Structures: Making Decisions.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Good LED Circuit 5V0 GND. What Voltage Does Meter See? Answer: 5 V.
1 Chapter 4: Basic Control Flow ► Chapter Goals  To be able to implement decisions using if statements  To understand statement blocks  To learn how.
Introduction 2 Smart Design. What is smart design? 2 Everything is designed except nature The packaging of a Band-Aid The curve of a bike frame The shape.
The IF-statements 31-Jan-2005 Venkatesh Ramamoorthy.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
A3144 SENSITIVE HALL-EFFECT SWITCHES & AH Linear Hall sensor
Lecture 15: Course Review BJ Furman ME 30 16MAY2011.
Autumn, 2012C.-S. Shieh, EC, KUAS, Taiwan1 智慧電子應用設計導論 (1/3) Sensors I Chin-Shiuh Shieh ( 謝欽旭 ) Department of Electronic.
Photoresistor resistance changes dramatically with light level living with the lab Using Photoresistors with an Arduino © 2011 LWTL faculty team.
ME 120: Arduino PWM For Loops in Arduino ME 120 Mechanical and Materials Engineering Portland State University
ME 120: User-defined functions: average analog input reading Arduino Programming – Part 5: User-defined functions ME 120 Mechanical and Materials Engineering.
Microcontroller basics Embedded systems for mortals.
Arduino + Bluetooth TYWu. Connection Arduino + Bluetooth Module.
Control Structure  What is control Structure?  Types of Controls  Use the control structure in VBScript.  Example Summery.
Introduction to Programming the Arduino Dr Gaw 3/21/14.
Pulse-Width Modulation: Simulating variable DC output
ME 120: Arduino PWM Breathing LED Code: Implementation on Arduino ME 120 Mechanical and Materials Engineering Portland State University
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
ME 120: Photoresistors and Arduino Programming Arduino Programming Case Study: Photoresistor Measurements ME 120 Mechanical and Materials Engineering Portland.
A3144 SENSITIVE HALL-EFFECT SWITCHES & AH Linear Hall sensor
Exam 2 Review.
Microcontroller basics
Sequence, Selection, Iteration The IF Statement
Cascade switching of an LED
Decisions Chapter 4.
European Robotic LABoratory
3.0 ARDUINO WORKSHOP PRESENTATION FOR STUDENTS IN 4º DEGREE OF COMPULSORY SECONDARY EDUCATION 3.0.
The Selection Structure
Programming Fundamentals
Bools & Ifs.
IF if (condition) { Process… }
CS-4540 Robotics - Lab 05 Switch inputs to the Arduino Uno
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Control Structures: Selection Statement
Topics: Programming Constructs: loops & conditionals Digital Input
Pages:51-59 Section: Control1 : decisions
Visual Basic – Decision Statements
Computer Science Core Concepts
Program Flow.
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
EECE.2160 ECE Application Programming
Control Structures: Selection Statement
Decision making and control functions
Pages:51-59 Section: Control1 : decisions
Lesson 3. Controlling program flow. Loops. Methods. Arrays.
Pulse-Width Modulation: Simulating variable DC output
Presentation transcript:

Arduino Programming: “if” statements Mechanical and Materials Engineering Portland State University http://me120.mme.pdx.edu/

Overview Motivation: The need for conditional execution “if” statement concepts Logical expressions Examples On-line reference: https://www.arduino.cc/reference/en/language/structure/control-structure/if/

Use case: Making decisions in programs Build a system to maintain water level in a tank

Use case: Making decisions in programs Build a system to maintain water level in a tank Arduino reads output from the level sensor If water level is low, open the supply valve An “if” statement is used to implement the decision

Logical statements control code execution “if” construct to make a single choice “if – else” construct to choose either/or if ( something is true ) { code block } if ( something is true ) { code block 1 } else { code block 2 } See http://arduino.cc/en/Reference/If http://arduino.cc/en/Reference/Else

Syntax of “if” construct “if” construct to make a single choice “something is true” must be a logical expression Example: if ( something is true ) { code block } Symbol Meaning < Is less than > Is greater than == Is equal to >= Is greater than or equal to <= Is less than or equal to != Is not equal to if ( x>0 ) { y = sqrt(x); }

Test your understanding What is the value of z? a. x = 2; y = 5; z = 0; if ( x < y ) { z = y – x; } c. x = 2; y = 5; z = 0; if ( (y-x)<= 3 ) { z = y/x; } b. x = 2; y = 5; z = 0; if ( x > y ) { z = y – x; } d. x = 2; y = 5; if ( (y-x)<= 3 ) { z = y/x; } else { z = 0.0; }

Example: Night light int LED_pin = 12; void setup() { pinMode(LED_pin, OUTPUT); } void loop() { int reading, sensor_pin=3, threshold = 150; reading = analogRead(sensor_pin); if ( reading < threshold ) { digitalWrite(LED_pin, HIGH); } else { digitalWrite(LED_pin, LOW);

Example: Night light int LED_pin = 12; void setup() { pinMode(LED_pin, OUTPUT); } void loop() { int reading, sensor_pin=3, threshold = 250; reading = analogRead(sensor_pin); if ( reading < threshold ) { digitalWrite(LED_pin, HIGH); } else { digitalWrite(LED_pin, LOW); analogRead returns a value in the range [0, 1023]

Compound “if-else-if” Implement a piecewise function Notice the error-handling for x<x1 and x>x4 if ( x<x1 ) { Serial.println(“Error: x<x1”); } else if ( x<x2 ) { y = ya; } else if ( x<x3 ) { y = yb; } else if ( x<x4 ) { y = yc; } else { Serial.println(“Error: x>x4”); }