In this session IOT expert Bob Gallup walked us through the basics of electronics and taught us how to do some basic wiring and programming on an Adafruit Trinket micro processor.
16. Download Software >
http://www.robertgallup.com/download/planningness2014-mac.zip
Unzip Arduino Software
Note: There are also downloads for “-windows”, and “-linux”
30. Sketch Walkthrough (See the Comments) E
//
Setup
runs
once
at
the
beginning
void
setup()
{
pinMode(1,
OUTPUT);
//
Pin
#1
is
output
}
!
//
Loop
runs
over
and
over
again
forever
void
loop()
{
digitalWrite(1,
HIGH);
//
turn
the
LED
on
delay(1000);
//
wait
for
a
second
digitalWrite(1,
LOW);
//
turn
the
LED
off
delay(1000);
//
wait
for
a
second
}
31. Hack 01: Change the Blinking Speed (
1. Change the blink speed
Hint: edit the blink sketch to change the blink delay
from 1 second to 1/4 second (note: 1 second =
1000 milliseconds). Then run the sketch again.
2. Stretch: Make a short on, longer off blink
Hint: the delay times don’t have to be the same.
33. Wire Up an LED >
USB
#0
#1
#2
5V
BAT
GND
#4
#3
RST
JUMPER WIRE
34. A Note about Breadboards E
All of the contact points
in the same row are
connected
The “rift” separates the two sides
35. Hack 02: An LED on Another Pin (
1. Rewire your hack to move the LED to Trinket pin #2.
2. In the “blink” sketch, change the LED pin to match.
Hint: You may have to change more than one line
36. Hack 02: A Solution (
//
Setup
runs
once
void
setup()
{
pinMode(2,
OUTPUT);
//
Pin
#2
is
output
}
!
//
Loop
repeats
forever
void
loop()
{
digitalWrite(2,
HIGH);
//
turn
the
LED
on
delay(1000);
//
wait
for
a
second
digitalWrite(2,
LOW);
//
turn
the
LED
off
delay(1000);
//
wait
for
a
second
}
37. Hack 02: Another Way, “Variable” (
//
Declarations
int
pinLED
=
2;
!
//
Setup
runs
once
at
the
beginning
void
setup()
{
pinMode(pinLED,
OUTPUT);
//
Pin
is
output
}
!
//
Loop
runs
over
and
over
again
forever
void
loop()
{
digitalWrite(pinLED,
HIGH);
//
turn
the
LED
on
delay(1000);
//
wait
for
a
second
digitalWrite(pinLED,
LOW);
//
turn
the
LED
off
delay(1000);
//
wait
for
a
second
}
Declare Variable
pinLED = 2
42. What’s Different About This Sketch? E
//
Include
the
library
#include
<BobaBlox.h>
!
//
Declarations
LED
boardLED(1);
//
Declare
an
LED
on
pin
#1
!
//
Setup
runs
once
at
the
beginning
void
setup()
{
}
!
//
Loop
repeats
forever.
Use
the
“blink”
method
to
blink
boardLED
void
loop()
{
boardLED.blink();
}
45. Another Version of the Blink Method E
boardLED.blink(1, 100);
# TIMES
DELAY
46. Hack 03: An LED on Another Pin (
1. Change the library Blink sketch to use an LED on pin #3
Hint: You can add a 2nd LED, or move the LED you
connected in the previous hack.
47. Hack 04: Fast Blink (
1. Change the library sketch to blink faster
Hint: look at the example of the LED blink method that
uses two parameters, times and delay.
48. Hack 05: Blink Morse Code (
1. Load and run the SOS sketch from planningness2014
examples.
Note: In Morse Code, “S” is dot-dot-dot, “O” is dash-dash-
dash.
2. Change the sketch so it blinks “CQ”.
Hint: In Morse Code, “C” is dot-dash-dot-dash, “Q” is
dash-dash-dot-dash. Note: “CQ” is a little like “YO” in
the ham radio world.
53. A Microcontroller E
!
!
SENSORS
microcontroller Input: temperature, motion, touch, …
ACTUATORS
Output: light, motor, sound, heat, …
PROGRAM
54. E
The Trinket Basic Connections (pinout)
0
1
2
+5V (Regulated)
Digital I/O
Ground
4
3
LED (Pin 1)
USB
Reset Button
ADAFRUIT Power LED
TRINKET
I/O Pins connect the
microcontroller to
power, sensors, and
actuators.
73. Wiring Up a Single Button >
USB
#0
#1
#2
5V
BAT
GND
#4
#3
RST
JUMPER WIRE
JUMPER WIRE
74. Load / Run the ButtonLED Example E
//
Declarations
LED
boardLED(1);
Button
pushButton(2);
int
buttonState;
!
//
Setup
runs
once
at
the
beginning
void
setup()
{
}
!
//
Loop
repeats
forever
void
loop()
{
//
Read
the
switch.
Set
LED
on/off
according
to
button
state
buttonState
=
pushButton.isDown();
if
(buttonState
==
1)
{
boardLED.on();
}
else
{
boardLED.off();
}
}
77. Other Pushbutton Methods E
pushButton.isDown();
pushButton.isUp();
pushButton.isPressed();
pushButton.isReleased();
78. Branching (One Thing or Another) E
if
(buttonState
==
1)
{
boardLED.on();
}
else
{
boardLED.off();
}
79. The If statement (if/then/else) E
if
(condition)
{
•••
}
else
{
•••
}
if condition is true
if condition is false
conditions:
(a == b)
(a < b)
(a > b)
(a <= b)
(a >= b)
(a != b)
80. Hack 06: Reverse the Logic (
1. Reverse the logic in the buttonLED sketch. I.e. make
the LED go OFF when the button is pressed.
Hint: there are two ways to do this
2. Stretch: make the LED blink when the button is
pressed.
Hint: what other LED functions do you have to work
with?
83. Analog to Digital Converter (ADC) E
ADC
0, 25, 100, 300, 100, 1023
Voltage Values (0-1032)
84. E
The Trinket: A More Complete Picture
+5V (USB)
0
1
2
0
1
1
Battery
Ground
4
2
3 3
Digital I/O
Analog Output
Analog Input Reset
+5V (Regulated)
LED (Pin 1)
USB
Reset Button
ADAFRUIT Power LED
TRINKET
85. Wiring Up a Knob >
USB
#0
#1
#2
5V
BAT
GND
#4
#3
RST
86. Load / Run the knobDimLED Example E
LED
boardLED(1);
Knob
bluKnob(1);
!
int
knobValue;
int
LEDBrightness;
!
void
setup()
{
}
!
void
loop()
{
//
Check
the
knob
value
//
Convert
the
knob
value
(0-‐1023)
to
a
brightness
(0-‐255).
//
Note:
see
the
Map
command
in
the
reference
knobValue
=
bluKnob.value();
LEDBrightness
=
map(knobValue,
0,
1023,
0,
255);
boardLED.setBrightness(LEDBrightness);
}
.setBrightness()
(see reference)
89. Hack 07: Change Knob Direction (
1. Change the direction of the know. I.e. So the
brightness goes down when the knob it turned up.
2. Stretch: Change the blink speed with the knob
position.
90. The Smart LED (Digital LED) E
Unlike a standard LED, the Smart LED has a small processor inside that
controls the light and communicates digitally with your microprocessor. They
are also called NeoPixels. Generically, they are sometimes referred to by their
“controller”, the WS2812 or WS2811.
91. Component: Smart LED (NeoPixel) E
Digital In
+5V
GND
Digital Out
0 1 2 3 4 5 6 7
Digital In
Multiple of these LEDs can be “cascaded” from a
singe pin on your micro process (but, you may
need outside power).
92. Wiring Up a Smart LED (NeoPixel) >
USB
#0
#1
#2
5V
BAT
GND
#4
#3
RST
93. Hack 08: Hack the SmartPixel Example (
1. After wiring the SmartPixel up, load and run the
SmartPixel sketch.
2. Change the rate the colors change in the sketch
3. The sketch currently uses red, green, and blue as it’s
colors. Change to three different colors
Hint: look at the comments to figure out the parameters
for the setPixelColor() method.
94. Hack 09: Hack the SmartPixelRainbow Sketch (
1. Load and run the SmartPixelRainbow sketch.
2. Change the rate the colors change in the sketch.
Hint: You can guess what to do (perfectly respectable), or
look at the complicated code in the NeoPixelFunctions tab
for a clue.
95. Hack 10: More Smart LED Hacks (
1. BIG STRETCH: See if you can figure out how to control
the rainbow color with a knob.
Hint: look at the setPixelColor() and wheel() methods.
2. GIANT STRETCH: See you can cooperate with some of
your table-mates and connect several smart LEDs
together. Hint: one of the parameters when you declare
the LED variable, is the number of LEDs in the strip.
103. Hack 99: Hack Digital Personality (
1. Team up and use everything you’ve learned to design a
switch with a personality. Hint: use two LEDs and a switch.
pressing the switch turns one of the LEDs on/off. The other
LED flashes in some way with personality to indicate
whether you’ve been successful. Note: could you use a knob
as the switch?
2. Do anything else you want to try as an individual. Or, as a
team.