ME 120: Arduino PWM For Loops in Arduino ME 120 Mechanical and Materials Engineering Portland State University

Slides:



Advertisements
Similar presentations
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Advertisements

Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
ITC 240: Web Application Programming
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 4 Loops and Character Manipulation Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Week 11 Recap CSE 115 Spring Want to write a programming language? You’ll need three things: You’ll need three things: –Sequencing –Selection Polymorphism.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Intro to Arduino Programming. Draw your circuits before you build them From Arduino 330 Ohm From Arduino 330 Ohm From Arduino 330 Ohm.
Engineering 1020 Introduction to Programming Peter King Winter 2010.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Computer Science 12 Mr. Jean May 2 nd, The plan: Video clip of the day Review of common errors in programs 2D Arrays.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
For Loop Lesson 1 CS1313 Spring for Loop Lesson 1 Outline 1. for Loop Lesson 1 Outline 2.A while Loop That Counts #1 3.A while Loop That Counts.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
By Chad Blankenbeker.  The for-loop is best used when you know how many times it is going to be looped  So if you know you want it to only loop 10 times,
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Code The Arduino Environment.
Introduction to Loops For Loops. Motivation for Using Loops So far, everything we’ve done in MATLAB, you could probably do by hand: Mathematical operations.
Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
While Loops 10/14/13. Repetition Allows a program to do tasks over and over. Very powerful because the computer is fast and accurate.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
BY ILTAF MEHDI (MCS, MCSE, CCNA)1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI (MCS, MCSE, CCNA)2 Chapter No: 04 “Loops”
Input Boxes, List Boxes, and Loops Chapter 5. 2 Input Boxes Method for getting user’s attention to obtain input. InputBox() for obtaining input MessageBox()
Review Expressions and operators Iteration – while-loop – for-loop.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Looping Increment/Decrement Switch. Flow of Control Iteration/Switch Statements.
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/19 Outline The if Statement and Conditions Other Conditional.
ME 120: User-defined functions: average analog input reading Arduino Programming – Part 5: User-defined functions ME 120 Mechanical and Materials Engineering.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
ME 120: Arduino Programming Arduino Programming Part II ME 120 Mechanical and Materials Engineering Portland State University
ME 120: Arduino Programming Arduino Programming Part 1 ME 120 Mechanical and Materials Engineering Portland State University
ME 120: Photoresistors and Arduino Programming Arduino Programming Case Study: Photoresistor Measurements ME 120 Mechanical and Materials Engineering Portland.
Chapter 9 Repetition.
REPETITION CONTROL STRUCTURE
while Repetition Structure
Lecture 7: Repeating a Known Number of Times
for Loop 1 Outline for Loop 1 Outline for Loop
Chapter 5: Loops and Files.
Repetition-Counter control Loop
Ch 7: JavaScript Control Statements I.
Repetition Structures (Loops)
While Loops in Python.
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 5 Repetition.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Arrays, For loop While loop Do while loop
1 Code
Alternate Version of STARTING OUT WITH C++ 4th Edition
Java Programming Loops
Topics: Programming Constructs: loops & conditionals Digital Input
Java Programming Loops
Repetition Statements (Loops) - 2
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
REPETITION Why Repetition?
Arduino Programming: “if” statements
Presentation transcript:

ME 120: Arduino PWM For Loops in Arduino ME 120 Mechanical and Materials Engineering Portland State University

ME 120: Arduino PWM Motivation “for” loops are essential code constructs ❖ Allow for repetition of code blocks ❖ Specify the number of repetitions in advance ❖ Loop counters can be used as indices in arrays “for” loops are used to sweep a servo back and forth ❖ Key component in desktop fan project 2 A typographical convention: We will write “for” and “while” and “loop” in quotation marks when we are referring to the for-loop structure, the while-loop structure and the required loop function in Arduino sketches. This excessive use of quotes is generally considered bad form. We risk offending in order to distinguish “for” as a code construct and the ordinary word for as regular preposition.

ME 120: Arduino PWM But first... let’s review the structure of Arduino code 3

ME 120: Arduino PWM Three parts of a basic Arduino sketch 4

ME 120: Arduino PWM “loop” accepts no inputs and returns no values 5

ME 120: Arduino PWM The hidden master calls “loop” 6

ME 120: Arduino PWM What does this mean? “loop” is the ultimate loop: it is the main part of the Arduino sketch that is repeated indefinitely ❖ “loop” is a function that is necessary for all Arduino codes. ❖ If in your code, nothing needs to be repeated, the function “loop” will still exist but will be empty. All the code would then be entered in the Void setup() function ❖ “loop” may contain “for loops” and “while loops” 7 Void setup() { // code will go here } Void loop() { // nothing goes there }

ME 120: Arduino PWM “for” loops 8

ME 120: Arduino PWM Basic “for” loop structure Index controls consist of three parts ❖ partA: Starting condition: the initial value of the loop variable ❖ partB: Stopping condition: a logical test that must be true for the loop to execute ❖ PartC: Increment/decrement: the rule for changing the loop variable 9 for ( partA; partB; partC) { // the body of the for loop goes here } Index controls

ME 120: Arduino PWM Example i=0 is the starting condition. It establishes the value of i on the first (and only the first) trip through the loop i<5 must be true for the loop body to be executed i++ means that the value of i is incremented by one after the loop body is executed. 10

ME 120: Arduino PWM Example: add up 10 integers 11 int i, sum; sum = 0; for ( i=1; i<=10; i++) { sum = sum + i; } sumiverify icomments 01<=10, let's go in the "for" loopinitialization 12<=10, let's stay in the "for" loopfirst time we go in the block 33<=10, let's stay in the "for" loop <=10, let's stay in the "for" loop 156<=10, let's stay in the "for" loop 217<=10, let's stay in the "for" loop 288<=10, let's stay in the "for" loop 369<=10, let's stay in the "for" loop 4510<=10, let's stay in the "for" loop 5511i would be >10 when we incrementwe get out of the "for" loop

ME 120: Arduino PWM Common increment expressions 12 ExpressionMeaning i++ Increment by 1 at end of the loop i-- Decrement by 1 at end of the loop i+=1 Same as i++ i-=1 Same as i-- i+=2 Increment by 2 at end of the loop i-=2 Decrement by 2 at end of the loop

ME 120: Arduino PWM Loop 1: What does this code do? 13 void setup() { Serial.begin(9600); } void loop() { int i = 0; i = i + 1; Serial.println(i); }

ME 120: Arduino PWM Loop 2: What does this code do? 14 int i = 0; void setup() { Serial.begin(9600); } void loop() { i = i + 1; Serial.println(i); }

ME 120: Arduino PWM Loop 3: What does this code do? 15 void setup() { Serial.begin(9600); } void loop() { int i; for ( i=0; i<5; i++ ) { Serial.println(i); }

ME 120: Arduino PWM Loop 4: What does this code do? 16 void setup() { Serial.begin(9600); } void loop() { int i; for ( i=0; i<5; i+=2 ) { Serial.println(i); }

ME 120: Arduino PWM Loop 5: What does this code do? 17 void setup() { Serial.begin(9600); } void loop() { int i; for ( i=0; i<10; i++) { i = 5; Serial.println(i); } Serial.println("for loop over\n"); }

ME 120: Arduino PWM Loop 6: What does this code do? 18 void setup() { Serial.begin(9600); } void loop() { int i; for ( i=0; i<10; i++) { Serial.println(i); delay(100); } Serial.println("for loop over\n"); }