-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
2249 lines (2026 loc) · 70.7 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Microcontrollers</title>
<link rel="stylesheet" href="dist/reset.css">
<link rel="stylesheet" href="dist/reveal.css">
<link href='https://fonts.googleapis.com/css?family=Roboto Mono' rel='stylesheet'>
<link rel="stylesheet" href="dist/theme/white.css">
<link rel="stylesheet" href="dist/custom.css">
<link rel="stylesheet" href="dist/fancy-buttons.css">
<!-- Theme used for syntax highlighted code -->
<link rel="stylesheet" href="plugin/highlight/zenburn.css">
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h2>An Introduction to
<span style="color:#2a76dd;">
Microcontroller
</span>
Programming
</h2>
<p style="font-size:20pt; color:#2a76dd;margin-top:-0.5cm; margin-bottom: -0.5cm;">
By: Jan Joe S. Carcillar
</p>
<div>
<img src="./media/images/uno.gif" height="240" width="336">
</div>
</section>
<section>
<h3>What are Microcontrollers?</h3>
<section>
<img src="./media/images/uno.gif" height="240" width="336">
</section>
<section>
<figure>
<img src="media/images/microntroler_block.svg" height = "auto" width = "300px">
</figure>
<caption>Microcontroller Elements</caption>
</section>
<section>
<div>
<h4>Elements of a Microcontroller</h4>
</div>
<div style = "justify-content: center;">
<div class="centered-list" style="font-size: 25pt;">
<ol>
<li class="fragment fade-up">Processor - responds to instructions, perform basic arithmetic and logic operations.</li>
<li class="fragment fade-up">
<p>Memory - Store data the processor ouputs or receives.</p>
<ul>
<li class="fragment fade-up">
Program Memory - stores instructions, <span class="fragment highlight-blue">long term memory</span>
</li>
<li class="fragment fade-up">
Data Memory - stores <span class="fragment highlight-blue">volatile memory</span>, cleared when there is no power.
</li>
</ul>
</li>
<li class="fragment fade-up">I/O peripherals - ports, analog-to-digital or digital-to-analog converters.</li>
</ol>
</div>
</div>
</section>
<section data-transition="convex">
<h4><span class="emph-red">Some Popular Microcontrollers</span></h4>
<figure>
<img src="media/images/pop-stm3.jpg" hight="auto" width="500">
</figure>
<caption> STM32F103C8T6 (Blue Pill) </caption>
</section>
<section data-transition="convex">
<h4><span class="emph-red">Some Popular Microcontrollers</span></h4>
<figure>
<img src="media/images/pop-attiny85.jpg" hight="auto" width="300">
</figure>
<caption> ATTINY85 </caption>
</section>
<section data-transition="convex">
<h4><span class="emph-red">Some Popular Microcontrollers</span></h4>
<figure>
<img src="media/images/pop-pic.jpg" hight="auto" width="300">
</figure>
<caption> PIC16F877A </caption>
</section>
<section data-transition="convex">
<h4><span class="emph-red">Some Popular Microcontrollers</span></h4>
<figure>
<img src="media/images/pop-uno.jpg" hight="auto" width="500">
</figure>
<caption> ATMEGA328P (Arduino Uno) </caption>
</section>
<section data-transition="convex">
<h4><span class="emph-red">Some Popular Microcontrollers</span></h4>
<figure>
<img src="media/images/pop-esp32.jpg" hight="auto" width="500">
</figure>
<caption> Tensilica Xtensa LX6 (ESP32) </caption>
</section>
</section>
<section>
<h3>Programming Environment</h3>
<section>
<div style = "text-align: justify;">
An <span class = "emph-red">Integrated Development Environment</span> contains the necessary tools and other helpful features for software development such as:
</div>
<aside class="notes">
Without an IDE, the user can just use any text editor such Notepad. Although the obvious disadvantage of not having tools and having to do most of the work manually will present itself to the user.
</aside>
<div style = "justify-content: center;">
<div class = "centered-list">
<li class ="fragment fade-up"><pre style="margin-top: -1.2cm;"><code data-trim data-noescape>Syntax highlighting</code></pre></li>
<li class ="fragment fade-up">Intelligent code completion</li>
<li class ="fragment fade-up">Testing and debugging</li>
<li class ="fragment fade-up">Code compiler</li>
</ul>
</div>
</div>
</section>
<section data-transition="convex">
<figure style="margin-top: 1cm;">
<img class = "screen-window" src = "media/images/ide_main.png" height = "auto" width = "1000px">
<figcaption class = "emph-red" style="margin-top: -0.5cm;">Arduino IDE</figcaption>
</figure>
</section>
<section data-transition="convex"">
<figure style="margin-top: 1cm;">
<img class = "screen-window" src = "media/images/ide_serial.png" height = "auto" width = "1000px">
<figcaption class = "emph-red" style="margin-top: -0.5cm;">Serial Monitor</figcaption>
</figure>
<aside class = "notes">
The serial monitor can also be configured where printed items are translated into graphs which offers better visualization of
sensor readings and other values which the user might want to monitor during the development phase. The formatting required to
achive this will be discussed in the upcoming activities.
</aside>
</section>
<section data-transition="convex"">
<figure style="margin-top: 1cm;">
<img class = "screen-window" src = "media/images/ide_plotter.png" height = "auto" width = "500px">
<figcaption class = "emph-red" style="margin-top: -0.5cm;">Serial Plotter</figcaption>
</figure>
<aside class="notes">
Aside from the few tools shown, the IDE has a lot more to offer for example [Open Arduino IDE]:
1. Board Selector
2. Library Manager
3. Debugger
</aside>
</section>
<section>
<p class = "small-instruction">
When microcontrollers first came out, they were programmed using the C language. Now, there are a
few programming language than you can work with namely:
</p>
<div style = "justify-content: center;">
<div class = "centered-list">
<ul>
<li class ="fragment fade-up">
<div class = "heading-gif">
Python
<img src ="media/images/python.png" height = "auto" width = "30" style="padding-bottom: 30px;">
</div>
</li>
<li class ="fragment fade-up">
<div class = "heading-gif">
C++
<img src ="media/images/cpp.png" height = "auto" width = "30" style="padding-bottom: 30px;">
</div>
</li>
<li class ="fragment fade-up">
<div class = "heading-gif">
Javascript
<img src ="media/images/js.png" height = "auto" width = "30" style="padding-bottom: 30px;">
</div>
</li>
</ul>
</div>
</div>
</section>
</section>
<section>
<div class = "heading-gif">
<h3>Project 0: Blink for Me</h3>
<img src ="media/images/eye.gif" height = "auto" width = "100" style="padding-bottom: 30px;">
</div>
</section>
<section>
<div class = "heading-gif">
<h3>Project 0: Blink for Me</h3>
<img src ="media/images/eye.gif" height = "auto" width = "100" style="padding-bottom: 30px;">
</div>
<section>
<div style="text-align: left; padding-top:1cm;">
Materials Needed:
</div>
<div style = "display:flex; justify-content: left;">
<div class = "left-list">
<ol class="small-instruction">
<li>Any Arduino Board</li>
<li>Arduino IDE</li>
</ol>
</div>
</div>
<div style="text-align: left;">
Project Objectives:
</div>
<div style = "display:flex; justify-content: left;">
<div class = "left-list">
<ol class="small-instruction">
<li>Run a simple microcontroller program.</li>
<li>Introduce basic parts of a microcontroller code.</li>
<li>Introduce the process of board programming.</li>
</ol>
</div>
</div>
</section>
<section>
<pre>
<code data-line-numbers ="2-5|8-13|3-4|9-10|11-12" data-trim data-noescape>
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
</code>
</pre>
</section>
<section>
<iframe
width="725"
height="453"
src="https://www.tinkercad.com/embed/4oZ7Fegh9Kd"
frameborder="0"
marginwidth="0"
marginheight="0"
scrolling="no">
</iframe>
</section>
<section>
<h4 class = "fragment fade-up"><span class="emph-red">Recap</span></h4>
<div style = "justify-content: center; margin-top:-50px;">
<div class="centered-list">
<ol>
<li class="fragment fade-up">What part of the code runs once after boot up?</li>
<li class="fragment fade-up">What part of the code runs indefinitely?</li>
<li class="fragment fade-up">How many seconds of delay will there be in the code below?<br>
<pre><code class="arduino">delay(10000);</code></pre>
</li>
</ol>
</div>
</div>
</section>
</section>
<section>
<div class = "heading-gif">
<h3>Project 1: Blink Without Delay⚡</h3>
<!--img src ="media/images/eye.gif" height = "auto" width = "100" style="padding-bottom: 30px;"-->
</div>
<figure class = "fragment fade-up">
<img src = "media/images/blink-no-delay.gif" height = "auto" width = "200px">
</figure>
</section>
<section>
<div class = "heading-gif">
<h3>Project 1: Blink Without Delay⚡</h3>
<!--img src ="media/images/eye.gif" height = "auto" width = "100" style="padding-bottom: 30px;"-->
</div>
<section>
<div style="text-align: left; padding-top:1cm;">
Materials Needed:
</div>
<div style = "display:flex; justify-content: left;">
<div class = "left-list">
<ol class = "small-instruction">
<li>Any Arduino Board</li>
<li>Arduino IDE</li>
</ol>
</div>
</div>
<div style="text-align: left;">
Project Objectives:
</div>
<div style = "display:flex; justify-content: left;">
<div class = "left-list">
<ol class = "small-instruction">
<li>Know the effect of delays in the program flow.</li>
<li>Introduce timing macros in Arduino.</li>
<li>Perform actions based on a condition.</li>
</ol>
</div>
</div>
</section>
<section>
<pre>
<code data-line-numbers="1-4|11|12|13-14|16" data-trim data-noescape>
bool ledState = LOW;
unsigned long prevMillis = 0;
unsigned long currentMillis = 0;
const unsigned long blinkInterval = 1000;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
currentMillis = millis(); //get the number of milliseconds the microcontroller has run since boot up
if (currentMillis - prevMillis >= blinkInterval){ //if current time exceeds the previous time by the set interval
ledState = !ledState; //toggle the state of the LED to its opposite using "!"
prevMillis = currentMillis; //set the current time as the previous time to be used in the next iteration
}
digitalWrite(LED_BUILTIN, ledState); //turn on or turn off the LED depending on the condition above
}
</code>
</pre>
</section>
<section>
<iframe
width="725"
height="453"
src="https://www.tinkercad.com/embed/iMUWrfy44eH"
frameborder="0"
marginwidth="0"
marginheight="0"
scrolling="no">
</iframe>
</section>
<section>
<h4 class = "fragment fade-up"><span class="emph-red">Recap</span></h4>
<div style = "justify-content: center; margin-top:-50px;">
<div class="centered-list">
<ol>
<li class="fragment fade-up">Why is it not a good pratice to add lots of delays in the program?</li>
<li class="fragment fade-up">How does the program know when to switch the LED state?</li>
<li class="fragment fade-up">Why is the highlighted code below needed?
<pre>
<code class = "arduino" data-trim data-noescape data-line-numbers="3">
if (currentMillis - prevMillis > = blinkInterval) {
ledState = !ledState;
prevMillis = currentMillis;
}
</code>
</pre>
</li>
</ol>
</div>
</div>
<aside class="notes">
In this section, there are a lot of things that were introduced. In the topmost part of the program,
we have some variable assignment and some prefixes before them. These indicate their data types.
On the other hand, we have seen the conditional part which is called an "if" statement that is one of the many possible ways to control
the program flow. These types of constructs are also known as control structures.
In the next section, we're gonna have a slight detour from projects and focus on each of the said concepts.
</aside>
</section>
</section>
<section>
<div class = "heading-gif">
<h3>CONCEPTS 1: Data Types</h3>
<img src ="media/images/binary.gif" height = "auto" width = "80" style="padding-bottom: 30px;">
</div>
</section>
<section>
<div class = "heading-gif">
<h3>CONCEPTS 1: Data Types</h3>
<img src ="media/images/binary.gif" height = "auto" width = "80" style="padding-bottom: 30px;">
</div>
<section data-transition="convex" data-markdown>
<textarea data-template>
| prefix | name | <div style="width:260px">size</div> | <div style="width:90px">values</div> |
| ------ | ---- | ---------- | ------ |
| bool |boolean| 8 bits or 1 byte| true or false|
| byte | byte | 8 bits or 1 byte | 0 to 255 |
<pre>
<code class="arduino" data-noescape data-trim>
//use when there a binary data than can only be true or false
//1 and 0 would also work
bool ledState = true;
bool isSensorOn = false;
//use when storing a small range of number
byte smallNumber = 255;
</code>
</pre>
</textarea>
</section>
<section data-transition="convex" data-markdown>
<textarea data-template>
| prefix | name | <div style="width:260px">size</div> | <div style="width:200px">values</div> |
| ------ | ---- | ---------- | ------ |
| char | character | 8 bits or 1 byte | -127 to 128|
| word | word | 16 bits or 2 bytes| 0 to 65535 |
<pre style = "width: 1000px; margin-left:-25px;">
<code class="arduino" data-noescape data-trim>
//these two variables will have the same output
//these two would be displayed as 'a'
char myCharacter1 = 'a'; //enclosed in single-quotes
char myCharacter2 = 97; //ASCII code for 'a'
//the word type is generally used to store a hexadecimal number
//hexadecimal system has 16 distinct symbols, 0 to 9 and A to F
word myWord = 0xF;//hexadecimal notation to write the decimal number 15
</code>
</pre>
</textarea>
</section>
<section data-transition="convex" data-markdown>
<textarea data-template>
| prefix | name | <div style="width:310px">size</div> | <div style="width:179px">values</div> |
| ------ | ---- | ---------- | ------ |
| int | integer | 16 bits or 2 bytes| -32768 to 32767 |
| unsigned int | unsigned integer | 16 bits or 2 bytes| 0 to 65535 |
<pre>
<code class="arduino" data-noescape data-trim>
//stores positive and negative whole numbers
int myInteger = 32767;
//can only store postive whole numbers
unsingned int myUsignedInteger = 65535;
</code>
</pre>
</textarea>
</section>
<section data-transition="convex" data-markdown style = "margin-left:-140px;">
<textarea data-template>
| prefix | <div style="width:290px">name</div> | <div style="width:310px">size</div> | <div style="width:310px">values</div> |
| ------ | ---- | ---------- | ------ |
| long | long integer | 32 bits or 4 bytes| -2,147,483, 648 to 2,147,483,647|
| unsigned long | unsigned long integer | 32 bits or 4 bytes | 0 to 4,294,967,295|
<pre style = "width: 800px; margin-left:200px;">
<code class = "arduino" data-noescape data-trim>
//store large positive or negative integers
long largeNumber1 = -2147483648;
//store large positive integers
unsigned long largeNumber2 = 2147483647;
</code>
</pre>
</textarea>
</section>
<section data-transition="convex" data-markdown style = "margin-left:-140px;margin-top:40px;">
<textarea data-template>
| prefix | <div style="width:300px">name</div> | <div style="width:200px">size</div> | <div style="width:450px">values</div> |
| ------ | ---- | ---------- | ------ |
| float | floating point number | 32 bits or 4 bytes | -3.4028235 × 10<sup>38</sup> to 3.4028235 × 10<sup>38</sup> |
| double| double precision number | 64 bits* or 8 bytes | -1.7 × 10<sup>308</sup> to 1.7 × 10<sup>308</sup>|
<pre style = "width: 920px; margin-left:160px;">
<code class = "arduino" data-noescape data-trim>
//store numbers that contain decimal places
long numberWithDecimals1 = 0.01;
//store larger numbers that contain decimal places
//in most boards, floats and doubles are the same size (4 bytes)
//with the exception of *Arduino Due
unsigned long numberWithDecimals2 = -0.02;
</code>
</pre>
</textarea>
</section>
<section data-transition="convex">
<h4> Array </h4>
<p>An array is a collection of variables that are accessed with an index number.</p>
<pre>
<code class = "arduino" data-noescape data-trim data-line-numbers="1-20|1-6|8-13|15-20" style = "height: 250px;">
//initializing arrays
int myInts[6]; //values default to 0
int myPins[] = {2, 4, 8, 3, 6};
int mySensVals[5] = {2, 4, -8, 3, 2};
char message[] = "level"; //enclose in double-quotes
char *messages[] = {"message1", "message2", "message3"}; //* indicates pointers
//assigning value to arrays
//indexing starts at 0
//which corresponds to the first element
myInts[0] = 100;
myInts[5] = 99;
//myInts -> {100, 0, 0, 0, 0, 99}
//retrieving values from arrays
message[4] //-> 'l' (last character)
message[3] //-> 'e'
message[2] //-> 'v'
message[1] //-> 'e'
message[0] //-> 'l' (first character)
</code>
</pre>
</section>
<section data-transition="convex">
<div style = "margin-top:1.8cm; margin-bottom: 0.5cm;">
<h4> String </h4>
</div>
An Arduino class for strings. This class allows for easier operations with strings.
<pre>
<code class="arduino"
data-noescape
data-trim
data-line-numbers="1-22|12-22"
style = "height: 320px; width: 1100px; margin-left:-120px;">
//initializing the string object
String myString = "Hello String"; // using a constant String
String myString = String('a'); // converting a constant char into a String
String newString = String("This is a string"); // converting a constant string into a String object
String myString = String(newString + " with more"); // concatenating two strings
String myString = String(13); // using a constant integer
String myString = String(45, HEX); // using an int and a base (hexadecimal)
String myString = String(255, BIN); // using an int and a base (binary)
String myString = String(millis(), DEC); // using a long and a base
String myString = String(5.698, 3); // using a float and the decimal places
//operations on string
myString.indexOf() //returns index of a character in a string
myString.length() //returns length of a string
myString.remove() //remove character at specified location
myString.replace() //replaces parts of a string
myString.toDouble() //convert string to double
myString.toInt() //convert string to integer
myString.toFloat() //convert string to float
myString.toLowerCase() //convert string to lower case
myString.toUpperCase() //convert string to upper case
myString.trim() //remove leading and trailing spaces
</code>
</pre>
</section>
<section>
<div class = "fragment fade-up">
<h4><span class="emph-red">Recap</span></h4>
<p class="small-instruction">Identify which type is the most suitable for following data description.</p>
</div>
<div style = "justify-content: center; margin-top:-50px;">
<div class="centered-list">
<ol>
<li class="fragment fade-up">A sensor reading that detects if a device is on or off.</li>
<li class="fragment fade-up">A gyroscope reading angles from 0° to 180°</li>
<li class="fragment fade-up">A value from the millis() macro.</li>
<li class="fragment fade-up">The message "turn on motor".</li>
<li class="fragment fade-up">A temperature reading from a sensor.</li>
</ol>
</div>
</div>
</section>
</section>
<section>
<div class = "heading-gif" style="flex-wrap:wrap;">
<div style="display:flex;align-items: center;">
<h3>CONCEPTS 2: Control Structures</h3>
<img src ="media/images/mixer.gif" height = "auto" width = "80" style="padding-bottom: 30px;">
</div>
</div>
</section>
<section>
<div class = "heading-gif" style="flex-wrap:wrap;">
<div style="display:flex;align-items: center;">
<h3>CONCEPTS 2: Control Structures</h3>
<img src ="media/images/mixer.gif" height = "auto" width = "80" style="padding-bottom: 30px;">
</div>
</div>
<section>
<h4> <span class="emph-red">IF</span> Statements</h4>
<p style = "font-size: 20pt;">The use of IF statements is very common in the any field of programming.
This allows for actions to happen when a certain condition is met.
</p>
<pre>
<code class="arduino" data-line-numbers data-noescape data-trim>
float waterLevel = 3.4;
float alarmThreshold = 5;
if (waterLevel >= alarmThreshold){
activateAlarm(); //📢
}
</code>
</pre>
</section>
<section>
<div style = "margin-top: -1cm;">
<h4> <span class="emph-red">IF-ELSE</span> Statements</h4>
</div>
<p style = "font-size: 20pt;">Alternative conditions can also be addressed using the ELSE statement.</p>
<pre>
<code class="arduino" data-line-numbers data-noescape data-trim>
float waterLevel = 3.4;
float alarmThreshold = 5;
if (waterLevel >= alarmThreshold){
//activates when the condition is true
activateAlarm(); //📢
} else {
//activates when the above condition is false 🪅
playPartyMusic();
}
</code>
</pre>
</section>
<section>
<div style = "margin-top: 2cm;">
<h4> <span class="emph-red">IF-ELSE-IF</span> Statements</h4>
</div>
<p style = "width: 1010px; font-size: 20pt;">Sometimes there could be many alternative conditions.</p>
<pre style = "width: 1010px;">
<code class="arduino" data-line-numbers data-noescape data-trim>
float waterLevel = 3.4;
float alarmThreshold = 5;
if (waterLevel >= 0.9*alarmThreshold){
activateLoudestAlarm();
} else if (waterLevel >= 0.75*alarmThreshold){
activateModerateAlarm();
} else if (waterLevel >= 0.5*alarmThreshold){
activateSoftAlarm();
} else {
//activates when all of the previous conditions are false 🪅
playPartyMusic();
}
</code>
</pre>
</section>
<section>
<div style = "margin-top: 3cm;">
<h4> <span class="emph-red">SWITCH</span> Case</h4>
</div>
<p style = "width: 1000px; font-size: 20pt;">
If the IF-ELSE-IF statements only contain checks for equality, the switch case control
can be used to provide a more concise syntax.
</p>
<pre style = "width: 1010px;">
<code class="arduino" data-line-numbers = "1-13|15-28" data-noescape data-trim>
//if-else-if statement
int fanSpeed = 0;
if (fanSpeed == 0) {
turnOffFan();
} else if (fanSpeed == 1) { //double equals checks for equality
setLowFanSpeed();
} else if (fanSpeed == 2) {
setMediumFanSpeed();
} else if (fanSpeed == 3) {
setHighFanSpeed();
} else {
activateBrokenKnobAlarm(); //oh no 😧
}
//switch case implementation
int fanSpeed = 0;
switch (fanSpeed) {
case 0:
turnOffFan(); break;
case 1:
setLowFanSpeed(); break;
case 2:
setHighFanSpeed(); break;
case 3:
setHighFanSpeed(); break;
default: //optional
activateBrokenKnobAlarm(); break; //oh no 😧
}
</code>
</pre>
</section>
<section>
<div style = "margin-top: 3cm;">
<h4> <span class="emph-red">FOR</span> Loop</h4>
</div>
<p style = "width: 1000px; font-size: 20pt;">
FOR loops are control structures that repeats tasks by keeping track of an incrementing variable.
</p>
<pre style = "width: 1010px;">
<code class="arduino" data-line-numbers = "1-13|15-28" data-noescape data-trim>
int NUM_REPEAT = 5;
String firstPart = "Went over the loop ";
String message = "";
for (int i = 0; i < NUM_REPEAT; i++) {
message = String(firstPart + String(i+1) + " times");
print(message);
}
</code>
</pre>
</section>
<section>
<div style = "margin-top: 3cm;">
<h4> <span class="emph-red">FOR</span> Loop</h4>
</div>
<p style = "width: 1000px; font-size: 20pt;">
The way the loop variable changes can also be changed.
</p>
<pre style = "width: 1010px;">
<code class="arduino" data-line-numbers = "1-5|7-11|13-17" data-noescape data-trim>
//prints number starting from 1 increasing by 2
//while the number is less than 10
for (int i = 1; i < 10; i += 2) {
print(i); //output -> 1, 3, 5 , 7, 9
}
//prints number starting from 15 decreasing by 1
//while the number is greater than 10
for (int j = 15; j > 10; j--){
print(j); //output -> 15, 14, 13, 12, 11
}
//prints powers of 2
//while the value is less than 100
for (int k = 1; k < 100 ; k = 2*k){
print(k); //output -> 1, 2, 4, 8, 16, 32, 64
}
</code>
</pre>
</section>
<section>
<div style = "margin-top: 3cm;">
<h4> <span class="emph-red">WHILE</span> Loop</h4>
</div>
<p style = "width: 1000px; font-size: 20pt;">
Sometimes, you want to repeat something while a condition is true. For this, the WHILE loop can be used.
</p>
<pre style = "width: 1010px;">
<code class="arduino" data-line-numbers data-noescape data-trim>
int myNumber = 0;
while (myNumber < 10) {
print(myNumber); //output -> 0, 2, 4, 6, 8
myNumber += 2;
}
</code>
</pre>
</section>
<section>
<div style = "margin-top: 3cm;">
<h4> <span class="emph-red">WHILE</span> Loop</h4>
</div>
<div style = "display:flex; justify-content:center; align-items: center; width: 1000px; font-size: 20pt;">
Loops can be interrupted using
<div style = "display:inline-block; width: 100px;">
<pre><code class="cpp">continue;</code></pre>
</div> and
<div style = "display:inline-block; width: 100px;">
<pre><code class="cpp">break;</code></pre>
</div> keywords.
</div>
<pre style = "width: 1010px;">
<code class="arduino" data-line-numbers data-noescape data-trim>
int myNumber = 0;
while (myNumber < 10) {
if (myNumber == 2){
myNumber += 2;
continue; //skip current iteration
}
if (myNumber == 6){
break; //end loop here
}
print(myNumber); //output -> 0, 4, 6
myNumber += 2;
}
</code>
</pre>
</section>
<section>
<div style = "margin-top: 3cm;">
<h4> <span class="emph-red">DO WHILE</span> Loop</h4>
</div>
<p style = "width: 1000px; font-size: 20pt;">
A WHILE loop where the condition is evaluated after the action. Ensures that at least one iteration happens.
</p>
<pre style = "width: 1010px;">
<code class="arduino" data-line-numbers data-noescape data-trim>
int myNumber = 0;
do {
print(myNumber); //output -> 0, 2, 4, 6, 8
myNumber += 2;
} while (myNumber < 10);
</code>
</pre>
</section>
<section>
<div class = "fragment fade-up" style = "margin-top: 3cm;">
<h4><span class="emph-red">Recap</span></h4>
</div>
<div style = "justify-content: center; margin-top:0px;">
<div class="centered-list" style="position:relative; width:1000px;">
<ol>
<li class="fragment fade-in-then-out" style="position:absolute;">
Which action will be peformed in this IF statement?
<pre>
<code class="arduino" data-line-numbers data-noescape data-trim>
int i = 0;
i -= 1;
if (i == 0){
doAction1();
} else if (i == 1) {
doAction2();
} else if (i < 2) {
doAction3();
} else {
doAction4();
}
</code>
</pre>
</li>
<li class="fragment fade-in-then-out" style="position:absolute;">
Modify this code such that the strings are printed with correct grammar.
<pre>
<code class="arduino" data-line-numbers data-noescape data-trim>
int NUM_REPEAT = 5;
String firstPart = "Went over the loop ";
String message = "";
for (int i = 0; i < NUM_REPEAT; i++) {
message = String(firstPart + String(i+1) + " times.");
print(message);
}
</code>
</pre>
</li>
<li class="fragment fade-in-then-out" style="position:absolute;">
What do you think will happen in this WHILE loop?
<pre>
<code class="arduino" data-line-numbers data-noescape data-trim>
int i = 0;
while (i < 10) {
print(i);
}
</code>
</pre>
</li>
<li class="fragment fade-up">Can the switch case control structure be used <br>
for conditions involving inequalities?
</li>
<li class="fragment fade-up">
<div style = "display:flex; justify-content:left; align-items: center; width:1500px;">
What's the difference between
<div style = "display:inline-block; width: 180px;">
<pre><code class="cpp">continue;</code></pre>
</div> and
<div style = "display:inline-block; width: 150px;">
<pre><code class="cpp">break;</code></pre>
</div>?
</div>
</li>
</ol>
</div>
</div>
</section>
</section>
<section>
<div class = "heading-gif" style="flex-wrap:wrap;">
<div style="display:flex;align-items: center;">
<h3>CONCEPTS 3: Boolean Algebra</h3>
<img src ="media/images/car-lights.gif" height = "auto" width = "80" style="padding-bottom: 30px;">
</div>
</div>
</section>
<section>
<div class = "heading-gif" style="flex-wrap:wrap;">
<div style="display:flex;align-items: center;">
<h3>CONCEPTS 3: Boolean Algebra</h3>
<img src ="media/images/car-lights.gif" height = "auto" width = "80" style="padding-bottom: 30px;">
</div>
</div>
<section>
<p style="text-align: justify;">
Using boolean algebra there is way to combine simple conditions, such as equality and inequality checks into a single statement.
Arduino has three boolean algebra operators:
</p>
<div style = "justify-content: center; margin-top:0px;">
<div class="left-list">
<ol>
<li class="fragment fade-up">AND (&&)</li>
<li class="fragment fade-up">OR (||)</li>
<li class="fragment fade-up">NOT (!)</li>
</ol>
</div>
</div>
</section>
<section data-markdown>
<textarea data-template>
<span style = "width:10px;"> </span>
### <span class = "emph-red">AND</span> Truth Table
<p style="font-style: italic;font-size:25pt; color: #3f3f3f; text-align: center;">
The AND operator written as <span class = "emph-red">&&</span>
in Arduino, requires both inputs to be true for its output to be true.
</p>
| A | B | Output |
|:-:|:-:|:------:|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
</textarea>
</section>
<section>
<div style = "margin-top:1cm;">
<h3><span class="emph-red">AND</span> Code Implementation</h3>
</div>
<pre style = "width:1050px; height: 430px; margin-left:-1.2cm;">
<code class="arduino" data-line-numbers data-noescape data-trim>
int numLegs = 2;
String movementMethod = "crawling";
String conclusion = "";
if (numLegs == 2) && (movementMethod == String("crawling")) {
conclusion = String("This could be a human baby. 👶");
}
else if (numLegs == 2) && (movementMethod == String("flying")) {
conclusion = String("This could be a bird. 🦜");
}
else if (numLegs = 3) && (movementMethod == String("flying")){
conclusion = String("I have no idea what this is. 👽");
}
else if (numLegs > 100) && (movementMethod == String("crawling")) {
conclusion = String("This could be a millipede. 🪱");
}
else {
conclusion = String("I need further research. 🔬")
}
</code>
</pre>
</section>
<section data-markdown>
<textarea data-template>
<span style = "width:10px;"> </span>
### <span class = "emph-red">OR</span> Truth Table
<p style="font-style: italic;font-size:25pt; color: #3f3f3f; text-align: center;">
The OR operator written as <span class = "emph-red">||</span>
in Arduino, requires any input to be true for its output to be true.
</p>
| A | B | Output |
|:-:|:-:|:------:|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
</textarea>
</section>
<section>
<div style = "margin-top:1cm;">