Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.                                                                                   Upcoming SlideShare
Loading in …5
×

# MzTEK Programming - Part 3

Third lecture on introductory programming for MzTEK. It covers loops and and introduction to functions. It is assumed you are at least a little familiar with Processing and Arduino.

• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here • Login to see the comments

• Be the first to like this

### MzTEK Programming - Part 3

1. 1. REVIEW LAST WEEK
2. 2. To create a new list (to declare it): int[] x = new int;
3. 3. To create a new list (to declare it):what kind of things are in the list?i.e. how much space needs to be reserved for each item int[] x = new int;
4. 4. To create a new list (to declare it): what kind of things are in the list? i.e. how much space needs to be reserved for each item int[] x = new int;[ ] means the data type is an array
5. 5. To create a new list (to declare it): what kind of things are in the list? i.e. how much space needs to be reserved for each item int[] x = new int;[ ] means the data type is an array what is the name of the list?
6. 6. To create a new list (to declare it): what kind of things are in the list? i.e. how much space needs to be reserved for each item how long will the list be? int[] x = new int;[ ] means the data type is an array what is the name of the list?
7. 7. if ( ){}else {}
8. 8. put in a comparison statement (like < or >)if ( ){}else {}
9. 9. put in a comparison statement (like < or >)if ( ) what to do if our comparison{ statement is true}else {}
10. 10. put in a comparison statement (like < or >)if ( ) what to do if our comparison{ statement is true}else {} what to do if our comparison statement is false
11. 11. put in a comparison statement (like < or >) if ( ) what to do if our comparison { statement is true } we don’t have to always have an else else {statement, sometimes you only care if the } statement is true what to do if our comparison statement is false
12. 12. What if we want to use multiple if statements? int counter; // some other code... if (counter < 10) { if (counter > 0 ) { counter++; } }
13. 13. What if we want to use multiple if statements? If counter is less than 10 and greater than 0, then int counter; increase counter by 1. // some other code... if (counter < 10) { if (counter > 0 ) { counter++; } }
14. 14. What if we want to use multiple if statements? If counter is less than 10 and greater than 0, then int counter; increase counter by 1. // some other code... if (counter < 10) { if (counter > 0 ) { counter++; } } counter is only increased if both if statements are true.
15. 15. What if we want to use multiple if statements? If counter is less than 10 and greater than 0, then int counter; increase counter by 1. // some other code... if (counter < 10) { if (counter > 0 ) { counter++; } } counter is only increased ifThese are called nested if both if statements are true.statements, because one is inside the { } of the other.
16. 16. What if we want to use multiple if statements? int counter; // some other code... if (counter > 10) { counter = 0; } if (counter < 0 ) { counter = 0; } }
17. 17. What if we want to use multiple if statements? If counter is greater than 10 or less than 0, then reset counter to 0. int counter; // some other code... if (counter > 10) { counter = 0; } if (counter < 0 ) { counter = 0; } }
18. 18. What if we want to use multiple if statements? If counter is greater than 10 or less than 0, then reset counter to 0. int counter; // some other code... if (counter > 10) { counter = 0; } if (counter < 0 ) { counter = 0; } } counter is reset if either if statements are true.
19. 19. BOOLEAN OPERATOR ORTrue OR False isFalse OR True isTrue OR True isFalse OR False is
20. 20. BOOLEAN OPERATOR ORTrue OR False is TrueFalse OR True isTrue OR True isFalse OR False is
21. 21. BOOLEAN OPERATOR ORTrue OR False is TrueFalse OR True is TrueTrue OR True isFalse OR False is
22. 22. BOOLEAN OPERATOR ORTrue OR False is TrueFalse OR True is TrueTrue OR True is TrueFalse OR False is
23. 23. BOOLEAN OPERATOR ORTrue OR False is TrueFalse OR True is TrueTrue OR True is TrueFalse OR False is False
24. 24. BOOLEAN OPERATOR ORTrue OR False is TrueFalse OR True is True When using OR in code, type ||True OR True is TrueFalse OR False is False
25. 25. int counter;// some other code...if (counter > 10) { counter = 0;}if (counter < 0 ) { counter = 0; }}
26. 26. int counter;// some other code...if (counter > 10) { counter = 0;}if (counter < 0 ) { counter = 0; } int counter;} // some other code... if ((counter < 0) || counter > 10)) { counter = 0; }
27. 27. int counter;// some other code...if (counter > 10) { counter = 0;}if (counter < 0 ) { OR counter = 0; } int counter;} // some other code... if ((counter < 0) || counter > 10)) { counter = 0; }
28. 28. BOOLEAN OPERATOR AND True AND False is False AND True is True AND True is False AND False is
29. 29. BOOLEAN OPERATOR AND True AND False is False False AND True is True AND True is False AND False is
30. 30. BOOLEAN OPERATOR AND True AND False is False False AND True is False True AND True is False AND False is
31. 31. BOOLEAN OPERATOR AND True AND False is False False AND True is False True AND True is True False AND False is
32. 32. BOOLEAN OPERATOR AND True AND False is False False AND True is False True AND True is True False AND False is False
33. 33. BOOLEAN OPERATOR AND True AND False is False False AND True is False When using AND in code, type && True AND True is True False AND False is False
34. 34. int counter;// some other code...if (counter < 10) { if (counter > 0 ) { counter++; }}
35. 35. int counter;// some other code...if (counter < 10) { if (counter > 0 ) { counter++; }} int counter; // some other code... if ((counter > 0) && (counter < 10)) { counter++; }
36. 36. int counter;// some other code...if (counter < 10) { if (counter > 0 ) { counter++; AND }} int counter; // some other code... if ((counter > 0) && (counter < 10)) { counter++; }
37. 37. BOOLEAN OPERATOR NOT NOT True is NOT False is
38. 38. BOOLEAN OPERATOR NOT NOT True is False NOT False is
39. 39. BOOLEAN OPERATOR NOT NOT True is False NOT False is True
40. 40. BOOLEAN OPERATOR NOT NOT True is False NOT False is True When using NOT in code, type !
41. 41. int stopLoop = 0;// some other code...if(!stopLoop) { // some more code...}
42. 42. NOT int stopLoop = 0; // some other code... if(!stopLoop) { // some more code... }
43. 43. LOOPS There are two ways to repeat something:1. Do this N number of times.2. Keep doing this until something else happens.
44. 44. LOOPS There are two ways to repeat something: Repeat this event in1. Do this N the calendar of number this times. many times.2. Keep doing this until something else happens.
45. 45. LOOPS There are two ways to repeat something: Repeat this event in1. Do this N the calendar of number this times. many times.2. Keep doing this event in the Repeat this until calendar until a certain something else occurs. date happens.
46. 46. DO THIS N TIMESint i;for(i=0; i<4; i++) {}
47. 47. DO THIS N TIMESstart with a number, in this case 0 int i; for(i=0; i<4; i++) { }
48. 48. DO THIS N TIMESstart with a number, in if this statement is this case 0 true int i; for(i=0; i<4; i++) { }
49. 49. DO THIS N TIMESstart with a number, in if this statement is this case 0 true int i; for(i=0; i<4; i++) { } then do whatever is written here
50. 50. DO THIS N TIMESstart with a number, in if this statement is this case 0 true int i; for(i=0; i<4; i++) { } when you’ve done what’s then do whatever is in the { } once, do this, in written here this case add make i equal to its current value plus 1
51. 51. DO THIS N TIMESstart with a number, in if this statement is true go back to see if the this case 0 middle statement is still int i; true for(i=0; i<4; i++) { } when you’ve done what’s then do whatever is in the { } once, do this, in written here this case add make i equal to its current value plus 1
52. 52. KEEP DOING THIS UNTILSOMETHING ELSE HAPPENS while ( ) { }
53. 53. KEEP DOING THIS UNTILSOMETHING ELSE HAPPENS if the statement here is true while ( ) { }
54. 54. KEEP DOING THIS UNTILSOMETHING ELSE HAPPENS if the statement here is true while ( ) { } then do what is between { } once
55. 55. KEEP DOING THIS UNTIL SOMETHING ELSE HAPPENS if the statement here is true while ( ) {then repeat by checking the statement again } then do what is between { } once
56. 56. EXERCISEWrite out each iteration of these loops and what the variablesequal at the end of each loop.int i; int k = 100;int j = 15; while ( k > 0 ) {for (i=0; i<5; i++) { k = k -10; j = j * 2 - i; }}
57. 57. EXERCISEGo through code at http://processing.org/learning/basics/embeddediteration.html• identify all of the variables, why were those data types chosen?• identify all of the comparisons made• identify all control structures• draw a diagram explaining what is happening in the code
58. 58. float box_size = 11;float box_space = 12;int margin = 7;size(200, 200);background(0);noStroke();// Draw gray boxesfor (int i = margin; i < height-margin; i += box_space){ if(box_size > 0){ for(int j = margin; j < width-margin; j+= box_space){ fill(255-box_size*10); rect(j, i, box_size, box_size); } box_size = box_size - 0.6; }}
59. 59. float box_size = 11;float box_space = 12;int margin = 7;size(200, 200);background(0);noStroke();// Draw gray boxesfor (int i = margin; i < height-margin; i += box_space){ if(box_size > 0){ for(int j = margin; j < width-margin; j+= box_space){ fill(255-box_size*10); rect(j, i, box_size, box_size); } box_size = box_size - 0.6; }}
60. 60. float box_size = 11;float box_space = 12;int margin = 7;size(200, 200);background(0);noStroke();// Draw gray boxesfor (int i = margin; i < height-margin; i += box_space){ if(box_size > 0){ for(int j = margin; j < width-margin; j+= box_space){ fill(255-box_size*10); rect(j, i, box_size, box_size); } box_size = box_size - 0.6; }}
61. 61. float box_size = 11;float box_space = 12;int margin = 7;size(200, 200);background(0);noStroke();// Draw gray boxesfor (int i = margin; i < height-margin; i += box_space){ if(box_size > 0){ for(int j = margin; j < width-margin; j+= box_space){ fill(255-box_size*10); rect(j, i, box_size, box_size); } box_size = box_size - 0.6; }}
62. 62. float box_size = 11;float box_space = 12;int margin = 7;size(200, 200);background(0);noStroke();// Draw gray boxesfor (int i = margin; i < height-margin; i += box_space){ if(box_size > 0){ for(int j = margin; j < width-margin; j+= box_space){ fill(255-box_size*10); rect(j, i, box_size, box_size); } box_size = box_size - 0.6; }}
63. 63. FUNCTIONS
64. 64. inputfunctionoutput
65. 65. A function is something that can take input,do something, and then output something. input function output
66. 66. A function is something that can take input,do something, and then output something. input The input and output are optional, some functions do not have both. function output
67. 67. A function is something that can take input,do something, and then output something. input The input and output are optional, some functions do not have both. function Functions exist so that you don’t have to output write a lot of code.
68. 68. When you call the function size( )size(300, 400);
69. 69. When you call the function size( ) it creates a window with thesize(300, 400); parameters you entered
70. 70. When you call the function size( ) it creates a window with thesize(300, 400); parameters you enteredand then the program continues with the next line of code.
71. 71. When you call the function size( ) it creates a window with thesize(300, 400); parameters you enteredand then the program continues with the next line of code.It has a return type of void, so there’s nothing given backdirectly to your program, but it does some work for you. Itcreated the window.
72. 72. When you call the function size( ) it creates a window with thesize(300, 400); parameters you enteredand then the program continues with the next line of code.It has a return type of void, so there’s nothing given backdirectly to your program, but it does some work for you. Itcreated the window.In Processing, (as far as I know) all functions have a voidreturn type.
73. 73. void means that nothing is returned.
74. 74. void means that nothing is returned.void setup( ) {}
75. 75. void means that nothing is returned.void setup( ) { is typed when you want to have something happen between { }.}
76. 76. void means that nothing is returned.void setup( ) { is typed when you want to have something happen between { }.} The void in front of setup means nothing is returned after setup( ) is ﬁnished.
77. 77. In Arduino:int val = 0;int inPin = 7;val = digitalRead(inPin);
78. 78. In Arduino:int val = 0;int inPin = 7;val = digitalRead(inPin); function name
79. 79. In Arduino: input parameters andint val = 0; typeint inPin = 7;val = digitalRead(inPin); function name
80. 80. In Arduino: input parameters and int val = 0; type int inPin = 7; val = digitalRead(inPin); function namean int is returned, so it needs to be stored somewhere
81. 81. FINAL EXERCISE
82. 82. Create a Processing program that generatively draws depending on the mouse position. void setup() { // create the window size(400, 400);Within your program use: } • Variables void draw() { // set the colour • For or while loop fill(10, 10, 255); • If or if/else // draw the circle ellipse(mouseX, mouseY, 100, 100);Start with this code. }