Successfully reported this slideshow.

# MzTEK Programming - Part 3   ×

1 of 83 Ad
1 of 83 Ad

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

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.

### 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 if These 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 OR True OR False is False OR True is True OR True is False OR False is
20. 20. BOOLEAN OPERATOR OR True OR False is True False OR True is True OR True is False OR False is
21. 21. BOOLEAN OPERATOR OR True OR False is True False OR True is True True OR True is False OR False is
22. 22. BOOLEAN OPERATOR OR True OR False is True False OR True is True True OR True is True False OR False is
23. 23. BOOLEAN OPERATOR OR True OR False is True False OR True is True True OR True is True False OR False is False
24. 24. BOOLEAN OPERATOR OR True OR False is True False OR True is True When using OR in code, type || True OR True is True False 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 in 1. 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 in 1. 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 TIMES int i; for(i=0; i<4; i++) { }
47. 47. DO THIS N TIMES start with a number, in this case 0 int i; for(i=0; i<4; i++) { }
48. 48. DO THIS N TIMES start 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 TIMES start 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 TIMES start 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 TIMES start 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 UNTIL SOMETHING ELSE HAPPENS while ( ) { }
53. 53. KEEP DOING THIS UNTIL SOMETHING ELSE HAPPENS if the statement here is true while ( ) { }
54. 54. KEEP DOING THIS UNTIL SOMETHING 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. EXERCISE Write out each iteration of these loops and what the variables equal 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. EXERCISE Go 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 boxes for (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 boxes for (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 boxes for (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 boxes for (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 boxes for (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. input function output
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 the size(300, 400); parameters you entered
70. 70. When you call the function size( ) it creates a window with the size(300, 400); parameters you entered and then the program continues with the next line of code.
71. 71. When you call the function size( ) it creates a window with the size(300, 400); parameters you entered and then the program continues with the next line of code. It has a return type of void, so there’s nothing given back directly to your program, but it does some work for you. It created the window.
72. 72. When you call the function size( ) it creates a window with the size(300, 400); parameters you entered and then the program continues with the next line of code. It has a return type of void, so there’s nothing given back directly to your program, but it does some work for you. It created the window. In Processing, (as far as I know) all functions have a void return 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 and int val = 0; type int 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 name an 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. }