arduino part 2
DESCRIPTION
Arduino Part 2. Topics: Serial Communication Programming Constructs: functions, loops and conditionals Digital Input. Serial Communication. todbot.com/blog/bionicarduino. todbot.com/blog/bionicarduino. Serial Communication. Compiling turns your program into binary data (ones and zeros) - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Arduino Part 2](https://reader036.vdocuments.site/reader036/viewer/2022081421/56812d55550346895d925d41/html5/thumbnails/1.jpg)
Arduino Part 2
Topics:Serial Communication
Programming Constructs: functions, loops and conditionals
Digital Input
![Page 2: Arduino Part 2](https://reader036.vdocuments.site/reader036/viewer/2022081421/56812d55550346895d925d41/html5/thumbnails/2.jpg)
Serial Communication
todbot.com/blog/bionicarduino
![Page 3: Arduino Part 2](https://reader036.vdocuments.site/reader036/viewer/2022081421/56812d55550346895d925d41/html5/thumbnails/3.jpg)
todbot.com/blog/bionicarduino
![Page 4: Arduino Part 2](https://reader036.vdocuments.site/reader036/viewer/2022081421/56812d55550346895d925d41/html5/thumbnails/4.jpg)
Serial Communication
• Compiling turns your program into binary data (ones and zeros)
• Uploading sends the bits through USB cable to the Arduino
• The two LEDs near the USB connector blink when data is transmitted• RX blinks when the Arduino is
receiving data• TX blinks when the Arduino is
transmitting data todbot.com/blog/bionicarduino
![Page 5: Arduino Part 2](https://reader036.vdocuments.site/reader036/viewer/2022081421/56812d55550346895d925d41/html5/thumbnails/5.jpg)
First Program/** Hello World!* From www.ladyada.net * It shows how to send data to the computer */ void setup() // run once, when the sketch starts { Serial.begin(9600); // set up Serial library at 9600 bps Serial.println("Hello world!"); // prints hello with a line break } void loop() // run over and over again { // do nothing! }
![Page 6: Arduino Part 2](https://reader036.vdocuments.site/reader036/viewer/2022081421/56812d55550346895d925d41/html5/thumbnails/6.jpg)
Open the Serial Monitor and Upload the Program
![Page 7: Arduino Part 2](https://reader036.vdocuments.site/reader036/viewer/2022081421/56812d55550346895d925d41/html5/thumbnails/7.jpg)
Modify the Program (each bullet is a different mod)
• Move Serial.println("Hello world!"); to loop()• Add the following to setup():
int a = 5;int b = 10;Serial.print("a + b = "); Serial.println(a + b);
• Replace the code above with the following:
![Page 8: Arduino Part 2](https://reader036.vdocuments.site/reader036/viewer/2022081421/56812d55550346895d925d41/html5/thumbnails/8.jpg)
Conditional Statement
if (someCondition) { // do stuff if the condition is true} else { // do stuff if the condition is false}
modelect.wordpress.com
![Page 9: Arduino Part 2](https://reader036.vdocuments.site/reader036/viewer/2022081421/56812d55550346895d925d41/html5/thumbnails/9.jpg)
Conditional Statement
int printMessage = 1;void setup(){ Serial.begin(9600); }
void loop(){ if (printMessage == 1) { Serial.println("Message"); printMessage= 0; } }
int printMessage = 1;void setup(){ Serial.begin(9600); } void loop(){ if (printMessage == 1) { Serial.println("Message"); printMessage= 0; } else { Serial.println("NO Message"); printMessage= 1; }}
![Page 10: Arduino Part 2](https://reader036.vdocuments.site/reader036/viewer/2022081421/56812d55550346895d925d41/html5/thumbnails/10.jpg)
while Loop
while(expression){ statement(s); }
www.toves.org
Exampleint var = 0;while (var < 200) { // do something repetitive 200 times var = var + 1;}
![Page 11: Arduino Part 2](https://reader036.vdocuments.site/reader036/viewer/2022081421/56812d55550346895d925d41/html5/thumbnails/11.jpg)
while Loopvoid setup(){ Serial.begin(9600); int count = 0; while (count < 5) { Serial.println("Hello world!"); count = count +1; }}
void loop(){ }
![Page 12: Arduino Part 2](https://reader036.vdocuments.site/reader036/viewer/2022081421/56812d55550346895d925d41/html5/thumbnails/12.jpg)
for loop
martin-thoma.com
www.visualcplusdotnet.com
![Page 13: Arduino Part 2](https://reader036.vdocuments.site/reader036/viewer/2022081421/56812d55550346895d925d41/html5/thumbnails/13.jpg)
for Loopvoid setup(){ Serial.begin(9600); for (int count = 0; count < 5; count++) { Serial.println("Hello world!"); }}
void loop(){ }
![Page 14: Arduino Part 2](https://reader036.vdocuments.site/reader036/viewer/2022081421/56812d55550346895d925d41/html5/thumbnails/14.jpg)
Functions• loop() and setup() are procedures • You can create you own functions
arduino.cc
void setup() {}
void loop() {}
Both setup() and loop() have no parameters and return no values
![Page 15: Arduino Part 2](https://reader036.vdocuments.site/reader036/viewer/2022081421/56812d55550346895d925d41/html5/thumbnails/15.jpg)
Functions: Example 1
learn.parallax.com
![Page 16: Arduino Part 2](https://reader036.vdocuments.site/reader036/viewer/2022081421/56812d55550346895d925d41/html5/thumbnails/16.jpg)
Functions: Example 2
learn.parallax.com
![Page 17: Arduino Part 2](https://reader036.vdocuments.site/reader036/viewer/2022081421/56812d55550346895d925d41/html5/thumbnails/17.jpg)
Digital Input (introducing the switch)
Create the circuit above and then run File -> Examples -> Digital -> Button
push-button switch
www.ladyada.net/