2. what is a microchip? tiny, inexpensive, computer which can
interact with the physical world
feedback
sensors
microphone
infrared sensor
C02 sensor
compass
motor
LED
valve
speaker
actuators
μ chip
brain
=
programmed
behavior
7. 1. download arduino software (https://www.arduino.cc/en/Main/Software)
blink: software
2. plug in arduino to laptop with USB cable
5. select board > Arduino Genuino / UNO
3. run Arduino software
6. select COM Port > COMXX (windows) or dev/...Arduino Uno (Mac)
*Windows Device Driver install (Device Manager > Update Driver >
select Arduino/Drivers folder)
4. file > examples > basic > blink
8. syntax
functionName (argument 1, argument 2)
{
do something
between curly
brackets
}
semicolons for most line endings;
// comments after double slashes
/* comments
between
star slashes */
9. asked to choose a sketch folder name: this is where your Arduino sketch
will be stored. Name it Blinking_LED and click OK. Then, type the following
text (Example 01) into the Arduino sketch editor (the main window of
the Arduino IDE). You can also download it from www.makezine.com/
getstartedarduino. It should appear as shown in Figure 4-3.
// Example 01 : Blinking LED
#define LED 13 // LED connected to
// digital pin 13
void setup()
{
pinMode(LED, OUTPUT); // sets the digital
// pin as output
}
void loop()
{
digitalWrite(LED, HIGH); // turns the LED on
delay(1000); // waits for a second
digitalWrite(LED, LOW); // turns the LED off
delay(1000); // waits for a second
}
blink: software
comment
(for humans)
part of code
where we define
input/output pins
Required in every
Arduino sketch
main code which
will be repeated
over and over.
Required in every
Arduino sketch
do nothing for
1000 milliseconds
(one second)
do nothing for
1000 milliseconds
replace “LED”
with “13”
when compiled
set pin 13 to
OUTPUT
sets pin 13
to 5V
sets pin 13
to GND
*HIGH = 5V
LOW = 0V
14. specialized pins
needs power (+)
can convert
analog to digital
can compare
voltages
Can talk to computers
has a clock
has reconfigurable
sensing and
actuating pins
can “interrupt”
itself
15. blink: hardware Arduino must be plugged in...
...to your computer,
in order to download
the code.
after being programmed:
(USB cable also supplies 5V)
can be battery powered
or plugged into wall
adapter (no longer needs
computer).
23. microchips operate
faster than human
sensory apparatuses
human eyes can detect:
<60-90 Hz
human ears can detect:
20 - 20,000Hz
our microchip
runs at
16 MHz!!
blink: Thresholds of human perception
26. Debugging Tips:
-the compile button
-display line numbers (File > Preferences)
-the serial monitor and serial printing (make sure same baud rate)
-isolate, test one thing at a time.
-metal connections are exposed on the bottom of
the board, beware of stray bits of metal or metal
tables.
-if your arduino is turning off right after turning
on, it’s probably shorting (ground and power are
connected).
27. diagnostic tools: mulitmeter & oscilloscope
measures voltage and current
at single location and time
multimeter oscilloscope
measures voltage and current
at multiple locations and times
30. // Example 03A: Turn on LED when the button is pressed
// and keep it on after it is released
#define LED 13 // the pin for the LED
#define BUTTON 7 // the input pin where the
// pushbutton is connected
int val = 0; // val will be used to store the state
// of the input pin
int state = 0; // 0 = LED off while 1 = LED on
void setup() {
pinMode(LED, OUTPUT); // tell Arduino LED is an output
pinMode(BUTTON, INPUT); // and BUTTON is an input
}
void loop() {
val = digitalRead(BUTTON); // read input value and store it
// check if the input is HIGH (button pressed)
// and change the state
if (val == HIGH) {
state = 1 - state;
}
if (state == 1) {
digitalWrite(LED, HIGH); // turn LED ON
} else {
digitalWrite(LED, LOW);
}
}
Now go test this code. You will notice that it works . . . somewhat. You’ll
declare an
INPUT too
if statement
checks a
condition and
executes the
proceeding
statement if the
condition is
“true”
*1 = TRUE
0 = FALSE
button: software
variable =
box for storage
32. introduce
multiple readings
taken at different
times to solve
the bouncing
probem
debounce: software
// Example 03B: Turn on LED when the button is pressed
// and keep it on after it is released
// Now with a new and improved formula!
#define LED 13 // the pin for the LED
#define BUTTON 7 // the input pin where the
// pushbutton is connected
int val = 0; // val will be used to store the state
// of the input pin
int old_val = 0; // this variable stores the previous
// value of "val"
int state = 0; // 0 = LED off and 1 = LED on
void setup() {
pinMode(LED, OUTPUT); // tell Arduino LED is an output
pinMode(BUTTON, INPUT); // and BUTTON is an input
}
void loop(){
val = digitalRead(BUTTON); // read input value and store it
// yum, fresh
// check if there was a transition
if ((val == HIGH) && (old_val == LOW)){
state = 1 - state;
}
old_val = val; // val is now old, let's store it
if (state == 1) {
digitalWrite(LED, HIGH); // turn LED ON
} else {
digitalWrite(LED, LOW);
}
}
36. reuse existing code!
find code online and use it
if you’re a beginner, don’t bother
making anything from scratch just yet.
(The more popular the sensor/actuator
the easier this will be to find online)
37. Libraries
premade packages of software
procedures which save you time.
libraries provide extra
functionality for use in sketches,
e.g. working with hardware or
manipulating data.
38. 1. Find thing you want to talk to
2. Find and download the library
3. Get experimenting!
eg. radio transmitter/reciever
Libraries