Arduino 2 - Programming Basics: Mr. Mcbrien Tej2O

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 26

Arduino 2 - Programming

Basics
Mr. McBrien
TEJ2O
Yesterday

A first Arduino Sketch


Today

 Building our own devices


Arduino Programming Basics

 Single and Looped Commands


 Comments
 The Serial Monitor
 Inputs from the User
 Output Pins
Single vs. Looped Commands
 There are lots of commands that are done once
 Declared variables
 Opening communication
 Other…

 ManyArduino commands are done multiple times –


“Loops”
 “Keep doing this until we need to stop”
 Blinking LED
 Monitoring inputs
Individuals and Loops
 Identifywhat you need to
run once
 Identify
what must be
done over and over.
Communicating with the Programmer
who Comes Later
Comments

 Comments are ignored by the compiler - they


don’t change the way that the program executes
 All good programs are appropriately commented:
 Other programmers can follow the purpose of the code
 You can see why you did what you did at a later point.
 Any characters that come after // are comments.
Comments and TEJ
P4 Need!
 Allevaluated code must be appropriately
commented
 Header
 Name

 Date

 Name of Program
 Allmajor subroutines must be labeled with some
words about their purpose.
A Programmer Tip
Expert Tip!
 Sometimes you’ll want to disable some
code during testing/debugging.
 No need to delete
 Rightclick, “comment”
 Whenyou’re ready to reinsert, rightclick,
“uncomment”
Using the Computer Screen
Why Work with the Screen?
 The basic Arduino has a hard time communicating with users
 You need to know what’s going on with your Arduino,
especially while you’re coding.
 Many (most?) Arduino applications involve it being detached
from the computer, but you’ll usually have it plugged in
while you’re coding.
 The serial print command allows you to communicate to the
user using the Serial Monitor

While you’re coding, incorporate print commands to help you


to determine what the Arduino is doing at a given time.
Serial Monitor Commands


Serial.begin()
Core
- e.g., Serial.begin(9600) //opens communication
Skill!
• Serial.print() or Serial.println() //sends info to the screen
 - e.g., Serial.print(value)
• Serial.read() //accepts info from the window
• Serial.available()
• Serial.write()
• Serial.parseInt()

Play with the commands, and see what you can do.
Viewing the Serial Monitor
Accepting User Input

 Serial.read() is the command to accept from the “send”


window in the serial monitor
 Checking to see if there is anything available, and the
action of reading the value need to be looped, because we
don’t know when the values will become available.

Later we will use this same idea to monitor for button pushes,
etc.
Activity 1

 Experimentwith the Serial


Monitor Output:
 Modifyyour blink code to output
the text “blink” to the monitor each
time your LED lights up.
Part 2 – Input and Output Pins
Arduino Pins
About Output

 TheArduino has two kinds of pins – digital


and analog
 Output (on) is +5V
 (Off) is 0V
Configuration
Core Skill!
 Tomake pins work, we have
to declare them in the setup
part of the sketch.
 Reviewyour code from
yesterday. Can you find that
part?
Activity 2 – Connecting the Breadboard
 Modify Key Pitfall!
your blink sketch from yesterday
 Configure a breadboard with a 1000 Ω
resistor and LED
 Connectyour breadboard’s ground to
Arduino GND
 Connect your LED to pin 9
 Program your LED with your Arduino.
https://
learn.adafruit.com/adafruit-arduino-lesson-5-the-serial-moni
Shared Grounds
 The Arduino and the breadboard have
to share the “reference” potential
Key Pitfall!
Extension – Time Permitting

 Write a Sketch that accepts some kind of input from the


user and changes the LED pattern in response.
Key Points
 Declare Pins
 Serial Monitor for troubleshooting
 Ground the Arduino and the breadboard!
 //Commenting is an important part of programming,
and will be part of every evaluation in the unit.
Homework

 Finishthe serial monitor and


breadboard activities
References

 https://www.arduino.cc/en/Reference/pinMode
 https://www.arduino.cc/en/reference/serial
 https://
learn.adafruit.com/adafruit-arduino-lesson-5-the-serial-m
onitor/overview

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy