Download presentation
Presentation is loading. Please wait.
Published byJeffery Laurence Barrett Modified over 8 years ago
1
Harpeth Hall Jan 2016 Introduction to Arduino Prepared for Harpeth Hall Winterim January 2016
2
Harpeth Hall Jan 2016 https://www.arduino.cc/en/uploads/ Main/ArduinoUno_R3_Front_450px. jpg The Arduino Uno Basic, easy-to-use micro-controller Basically a simple computer In addition to a processor has a variety of inputs/outputs USB connection to computer Analog input and digital input/output “extras” – reset button, led’s, power management… Has a specific programming language used to create programs with the board You will use this on your laptops
3
Harpeth Hall Jan 2016 What can you do with Arduino A computer with inputs and outputs that does simple operations Many kinds of sensors can be used as inputs Usually motors are outputs, can also talk to a computer or another microcontroller Make a wheeled robot that follows a line Make an automatic light switch http://g01.a.alicdn.com/kf/HTB1PL3dJVXXXXaRXXXXq6xXFXXXw/Makebl ock-mBot-Educational-font-b-Robot-b-font-font-b-Kit-b-font-for-font-b.jpg https://atmelcorporation.files.wordpress.com/2014/10/839x40 0xposterimageresized2-pagespeed-ic-u1fkcea2d1.jpg
4
Harpeth Hall Jan 2016 Other options Arduino is very well-known and widely used, but not only option Teensy microcontroller PIC microcontrollers Raspberry pi is basically a full computer in a mini package We are using Arduino UNO, there are other designs Have to match your board with your background and project
5
Harpeth Hall Jan 2016 Digital vs. Analog Digital clockAnalog clock http://sapling-inc.com/wp-content/gallery/sbp-series/Red-6-Digit-Digital-Clock-With-Logo.png http://sapling-inc.com/wp-content/gallery/analog/round-analog-clock-single_v02_large.jpg
6
Harpeth Hall Jan 2016 Arduino Tutorial I – “Blink” Our goal is to blink an LED Materials: Arduino UNO LED 1K resistor Breadboard Computer Power/USB connections Before running the code, must set up the physical connections https://www.arduino.cc/en/Tutorial/Blink
7
Harpeth Hall Jan 2016 Blink Code Located in: File->Examples->01. Basics ->Blink Read through the code, start understanding how it works Run! Verify and upload
8
Harpeth Hall Jan 2016 Blink Code – What is the meaning? Line 1/* …. */ Line 18void setup() Line 20pinMode(…) Line 24 void loop() Line 25digitalWrite(…) Line 26 delay(…) https://www.arduino.cc/ is your friend!https://www.arduino.cc/
9
Harpeth Hall Jan 2016 Programming basics Variables store information Have a certain “data type” integer – you should know what an integer is double or float – has a decimal boolean – true or false Data types can be complicated, but we will deal with that later, for now think of these 3 types
10
Harpeth Hall Jan 2016 Altering Blink We want to use variables so that we can edit the code more easily “Initialize” variables before setup() Would like to change the output pin easily as well as how long the LED is on and off (using variables) Let’s alter the code to make these changes
11
Harpeth Hall Jan 2016 Code Structure Initialization setup() loop()
12
Harpeth Hall Jan 2016 Serial communication There is a specific way the Arduino talks to the PC – uses a “serial protocol” We can send messages back and forth – the “how” isn’t needed now In setup() routine Serial.begin(9600) Within the loop() routine Serial.println(…) Serial.print(…) Serial.read(…) https://www.arduino.cc/en/Reference/Serial has more documentationhttps://www.arduino.cc/en/Reference/Serial
13
Harpeth Hall Jan 2016 Arduino Tutorial II – “DigitalReadSerial” Our goal is to read a pushbutton and display its status to the PC Materials: Same Arduino things Pushbutton Resistor Breadboard https://www.arduino.cc/en/Tutorial/Di gitalReadSerialhttps://www.arduino.cc/en/Tutorial/Di gitalReadSerial Will need the “serial monitor” Test what happens adding a println in both setup() and loop()
14
Harpeth Hall Jan 2016 Arduino Tutorial III – “ReadAnalogVoltage” Our goal is to read an analog voltage from a potentiometer https://www.arduino.cc/en/Tutorial/Re adAnalogVoltagehttps://www.arduino.cc/en/Tutorial/Re adAnalogVoltage You will learn more about “pots” later, for now it’s a way to get a varying signal between 0 and 5 volts
15
Harpeth Hall Jan 2016 Binary In the “ReadAnalogVoltage” code, look at line 21: float voltage = sensorValue * (5.0 / 1023.0); Analog to digital conversion on Arduino uses a 10-big converter Where does that come from? Binary!
16
Harpeth Hall Jan 2016 Arduino Tutorial IV – Your own project In this project, we want to use the sensor input to blink the LED from the first tutorial at different speeds according to how the pot is turned You have all the tools to make this happen Up to you how to do this Go!
17
Harpeth Hall Jan 2016 Other Programming Structures Constants Logic If… else if … else Loops For While 17
18
Harpeth Hall Jan 2016 Constants Syntax: #define someName value Example #define pi 3.1416 Used for things that don’t change Some built-in keywords are also constants true, false HIGH, LOW, INPUT, OUTPUT Used for input/output pins 18
19
Harpeth Hall Jan 2016 Logic Sometime you want to only sometimes do something Example from “Blink” code Maybe if the pot is at one end or another, we want to keep the LED completely on or completely off Syntax: if (logicCondition) { …//lots of code } else if () …{…} … else {…} 19
20
Harpeth Hall Jan 2016 Logic Syntax Main logic operators are > < or == if (myStomach < full) { eatMore(); } else { sleep(); } 20
21
Harpeth Hall Jan 2016 Logic Syntax For a more practical example: if (robotPosition >= maxPosition) { stopRobot(); } else { //do whatever else you wanted to do } 21
22
Harpeth Hall Jan 2016 Logic Syntax For a more robust example: if (robotPosition >= maxPosition || robotPosition <= minPosition) { stopRobot(); } else { //do whatever else you wanted to do } 22
23
Harpeth Hall Jan 2016 Logical AND and Logical OR Simple overview: If I want to see if either of two conditions is met and possibly both of them, use OR If I want to see if exactly both of two conditions are met, use AND In Arduino AND is written as && OR is written as || For programming almost anything, this is extremely useful 23
24
Harpeth Hall Jan 2016 For Loop When you want to do something a fixed number of times Syntax: for (int i=0;i<5;i++) { … many lines of code } Create an integer called i and assign it the value zero Execute the following code When finished increment i by 1 If i<5 repeat execution Else continue 24
25
Harpeth Hall Jan 2016 Practice using if and for structures Try to make these examples work using “if” and “for” structures Both of these are based on the “blink” code If the pot is near one end of its travel, turn the LED all the way on or off – otherwise, blink it as in the example In the setup portion of the code, use a for loop to create an “opening” sequence of blinks so that you know the program has turned on 25
26
Harpeth Hall Jan 2016 Super-Challenge Create a code that reads in button presses and counts every time you press a button You can use digitalReadSerial as a base Display the counter on the serial monitor Once you have done that, get a second button and make it decrement the counter every time you press the button If you press both buttons, clear the counter You don’t have time to do all this, but it’s very possible! 26
27
Harpeth Hall Jan 2016 References http://www.instructables.com/id/Intro-to-Arduino/ https://www.arduino.cc/
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.