Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "ME 120: Arduino PWM For Loops in Arduino ME 120 Mechanical and Materials Engineering Portland State University"— Presentation transcript:

1 ME 120: Arduino PWM For Loops in Arduino ME 120 Mechanical and Materials Engineering Portland State University http://web.cecs.pdx.edu/~me120

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

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

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

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

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

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

8 ME 120: Arduino PWM “for” loops 8

9 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

10 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

11 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 64 105<=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

12 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

13 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); }

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

15 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); }

16 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); }

17 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"); }

18 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"); }


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

Similar presentations


Ads by Google