How to hack electronics
-
Upload
planning-ness -
Category
Devices & Hardware
-
view
3.083 -
download
2
Transcript of How to hack electronics
Agenda
Mad Cap First Hacks Electricity + Electronics
Hacking Switches + Knobs + Smart LEDs Project? Wrap Up
>Download Software
http://www.robertgallup.com/download/planningness2014-mac.zip
Unzip Arduino Software
Note: There are also downloads for “-windows”, and “-linux”
>Mac OS X: Security Settings
Open Preferences Unlock, and change “Allow apps …” to Anywhere
Run IMPORTANT: Reset security settings
1 2 3 4
>Add the Planning-ness Examples
1
2 Select planningness2014 in the Libraries folder from the class software download
ESketch Structure (Recipe)
// Svoid setup() { pinMode(1, OUTPUT); } !// Loopvoid loop() { digitalWrite(1, HIGH); delay(500); digitalWrite(1, LOW); delay(500); }
Statements
Comment
ESketch Walkthrough (See the Comments)
// 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 }
(Change the Blinking SpeedHack 01:
1. Change the blink speedHint: 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.
EA Note about Breadboards
The “rift” separates the two sides
All of the contact points in the same row are connected
(An LED on Another PinHack 02:
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
(A SolutionHack 02:
// 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 }
(Another Way, “Variable”Hack 02:
// 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 }
pinLED = 2
Declare Variable
EWhat’s Different About This Sketch?
// 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(); }
(An LED on Another PinHack 03:
1. Change the library Blink sketch to use an LED on pin #3Hint: You can add a 2nd LED, or move the LED you connected in the previous hack.
(Fast BlinkHack 04:
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.
(Blink Morse CodeHack 05:
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.
EA Microcontroller
!
!
microcontroller Input: temperature, motion, touch, …
ACTUATORS
SENSORS
PROGRAM
Output: light, motor, sound, heat, …
E
+5V (Regulated)
0
1
2
Digital I/O
Ground
4
3
LED (Pin 1)
USB
Reset Button
Power LEDADAFRUITTRINKET
The Trinket Basic Connections (pinout)
I/O Pins connect the microcontroller to power, sensors, and actuators.
EMix in Some Universal Forces
NegativePositive
+-
Like Yin+Yang, or Good+Evil, one comes with the other
EActually, Current is a Little Twisted
+-
++
+
++
++
+ ++
+
++
+
+
Current flows positive to negative
ELoad / Run the ButtonLED Example
// 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(); } }
EOther Pushbutton Methods
pushButton.isDown();
pushButton.isUp();
pushButton.isPressed();
pushButton.isReleased();
EThe If statement (if/then/else)
if (condition) {
•••
} else { •••
}
if condition is true
if condition is false
conditions:(a == b)(a < b)(a > b)(a <= b)(a >= b)(a != b)
(Reverse the LogicHack 06:
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?
E
+5V (USB)
0
1
2
0
1
1
Digital I/O
Analog Output
Analog Input +5V (Regulated)
Battery
Ground
4
3 3
2
Reset
LED (Pin 1)
USB
Reset Button
Power LEDADAFRUITTRINKET
The Trinket: A More Complete Picture
ELoad / Run the knobDimLED Example
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)
(Change Knob DirectionHack 07:
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.
EThe Smart LED (Digital LED)
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.
EComponent: Smart LED (NeoPixel)
Dig
ital In+
5V
GN
DD
igital O
ut
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).
(Hack the SmartPixel ExampleHack 08:
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.
(Hack the SmartPixelRainbow SketchHack 09:
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.
(More Smart LED HacksHack 10:
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.
(Hack Digital PersonalityHack 99:
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.
ENext Steps: Online Tutorials
http://arduino.cc/en/Tutorial http://playground.arduino.cc https://learn.sparkfun.com !
http://learn.adafruit.com http://learn.adafruit.com/category/learn-arduino !
!
google “arduino tutorials”
ENext Steps: Arduino Comic
http://www.jodyculkin.com/wp-content/uploads/2011/09/arduino-comic-latest3.pdf
EOnline Resources
ARDUINO: www.arduino.cc, reference, tutorials SPARKFUN: www.sparkfun.com, components, devices, tutorials ADAFRUIT: www.adafruit.com, components, devices, tutorials MAKE MAGAZINE: www.makezine.com, magazine, books, stuff MAKER SHED: www.makershed.com, components, kits, stuff INSTRUCTABLES: www.instructables.com, tutorials RADIO SHACK: www.radioshack.com, components, devices, tutorials AMAZON: www.amazon.com, books, components, devices, tutorials O’REILLY: www.oreilly.com, books, tutorials, etc.
ELECTRONIC GOLDMINE: http://www.goldmine-elec-products.com, components JAMECO: www.jameco.com, components MOUSER: www.mouser.com, components DIGIKEY: www.digikey.com, components !
Robert Gallup [email protected] @robertgallup