646 lines
15 KiB
Markdown
646 lines
15 KiB
Markdown
|
---
|
||
|
author: Raphael Kopala, Shawn Nock
|
||
|
institute: Unlondon Digital Media Assoc.
|
||
|
title: Introduction to Arduino
|
||
|
subtitle: Maker Workshop
|
||
|
lang: en-CA
|
||
|
colorlinks: true
|
||
|
...
|
||
|
|
||
|
# Goals
|
||
|
|
||
|
## Unlondon
|
||
|
|
||
|
\begin{center}
|
||
|
{\huge Enabling Exploration, Creativity, and Excellence In Art+Make+Tech}
|
||
|
\end{center}
|
||
|
|
||
|
Challenging and embracing ideas related to new technologies and social
|
||
|
platforms through the education, entertainment and engagement of our
|
||
|
membership and the community-at-large.
|
||
|
|
||
|
- 121Studios: Coworking for Creatives
|
||
|
- Unlab: Hackerspace
|
||
|
- Events: STEAM Outreach & Edu., ExplodeConf, Nuit Blanche
|
||
|
|
||
|
## Shawn: Day Job
|
||
|
|
||
|
Freelance Embedded Systems Engineer
|
||
|
|
||
|
- Indoor location tracking w/ Bluetooth
|
||
|
- Keychain / Fitness Band Widgets
|
||
|
- Joystick for VR
|
||
|
- Remote Controls
|
||
|
- Internet of S*#t
|
||
|
|
||
|
## Shawn: The Fun Stuff
|
||
|
|
||
|
Hacker, Church of the Weird Machine, Odd Duck
|
||
|
|
||
|
\begin{columns}[c]
|
||
|
\column{0.50\textwidth}
|
||
|
|
||
|
\begin{itemize}
|
||
|
\item Arduino compatible implant
|
||
|
\item EEG Games / Toy Hacking
|
||
|
\item Brain Stimulation
|
||
|
\item Be Weird, Make Weird, Have Fun!
|
||
|
\end{itemize}
|
||
|
|
||
|
\column{0.50\textwidth}
|
||
|
\begin{center}
|
||
|
\includegraphics[width=0.95\textwidth]{images/timbo.jpg}
|
||
|
\vspace{5mm}
|
||
|
\includegraphics[width=0.95\textwidth]{images/circadia.jpg}
|
||
|
\end{center}
|
||
|
\end{columns}
|
||
|
|
||
|
## Raphael: The Vital Info
|
||
|
|
||
|
- Manufacturing Engineer
|
||
|
- Instructor at Fanshawe
|
||
|
- Maker
|
||
|
|
||
|
# What's in your kit?
|
||
|
|
||
|
## Kit Contents
|
||
|
|
||
|
- Arduino Uno R3 Clone
|
||
|
- Solderless Breadboard
|
||
|
- Connecting wires
|
||
|
- LEDs
|
||
|
- Resistors, Potentiometer
|
||
|
- Buzzer
|
||
|
|
||
|
## What is Arduino?
|
||
|
|
||
|
\begin{center}
|
||
|
$\mu$C + reset button + led + USB
|
||
|
\end{center}
|
||
|
|
||
|
It's a kit (on a board) with the bare minimum components to easily use the $\mu$C
|
||
|
hardware. They do the basic, boring design needed for any board, so users only
|
||
|
need to add the neat stuff.
|
||
|
|
||
|
<!-- ## Arduino UNO -->
|
||
|
|
||
|
<!-- The Arduino variety that we are using is the Arduino UNO. -->
|
||
|
|
||
|
<!-- - Processor: Atmel Atmega328p -->
|
||
|
<!-- - Memory: 2K RAM + 32K Flash -->
|
||
|
<!-- - FT232RL Logic-level Serial$\leftrightarrow$USB Chip -->
|
||
|
|
||
|
## Arduino Software
|
||
|
|
||
|
The Arduino folks also adapted an *Integrated Development Environment*
|
||
|
(IDE) to their boards. This IDE allows us to easily write programs for
|
||
|
their boards and then write the programs to the $\mu$C.
|
||
|
|
||
|
\Large Get the Arduino IDE:
|
||
|
[https://www.arduino.cc/en/Main/Software](https://www.arduino.cc/en/Main/Software)
|
||
|
|
||
|
## Installation
|
||
|
|
||
|
\Large Get installing
|
||
|
|
||
|
# Circuit Basics
|
||
|
|
||
|
## Current
|
||
|
|
||
|
Current is the flow of charge through a circuit. Conventionally we
|
||
|
think of this as happening from HIGH ($+$) to LOW ($-$)
|
||
|
|
||
|
## Voltage / Potential / Resistance
|
||
|
|
||
|
Voltage is how fast the current can move in the circuit. River
|
||
|
metaphor:
|
||
|
|
||
|
- current = flow rate: ($\si{\liter\per\second}$)
|
||
|
- voltage = change in height: ($\si{\meter}$)
|
||
|
|
||
|
Other devices in a circuit can impede / effect current flow. We'll
|
||
|
call them resistance(s).
|
||
|
|
||
|
## Diode
|
||
|
|
||
|
\begin{columns}[c]
|
||
|
\column{0.50\textwidth}
|
||
|
|
||
|
\begin{itemize}
|
||
|
\item One way value for current\footnotemark[1]
|
||
|
\item LED $\equiv$ Light Emitting Diode
|
||
|
\item Band marks (-)\footnotemark[2]
|
||
|
\item Longer leg marks (+)
|
||
|
\end{itemize}
|
||
|
|
||
|
\column{0.50\textwidth}
|
||
|
\begin{center}
|
||
|
\includegraphics[width=0.75\textwidth]{images/diode.png}
|
||
|
\vspace{5mm}
|
||
|
\includegraphics[width=0.50\textwidth]{images/led.jpg}
|
||
|
\end{center}
|
||
|
\end{columns}
|
||
|
|
||
|
\footnotetext[1]{\tiny \url{https://learn.sparkfun.com/tutorials/diodes}}
|
||
|
\footnotetext[2]{\tiny \url{https://learn.sparkfun.com/tutorials/polarity/diode-and-led-polarity}}
|
||
|
|
||
|
## Diode Problems
|
||
|
|
||
|
* Diodes don't limit current
|
||
|
* Diodes aren't perfect (some current turned to heat)
|
||
|
* Too much current $\rightarrow$ Too much heat $\rightarrow$ \
|
||
|
\
|
||
|
\center{ \huge{\emph{What's that smell?} } }
|
||
|
|
||
|
## Resistor
|
||
|
|
||
|
\begin{columns}[c]
|
||
|
\column{0.50\textwidth}
|
||
|
|
||
|
\begin{itemize}
|
||
|
\item \emph{Resists}/limits the flow of current
|
||
|
\item Needed for LEDs: $\approx\SI{400}{\ohm}$\\
|
||
|
(safe for $\le\SI{6}{\volt}$)
|
||
|
\item Button Pull-up/down: $\ge\SI{10}{\kilo\ohm}$
|
||
|
\item Color coded, Google it
|
||
|
\end{itemize}
|
||
|
\column{0.50\textwidth}
|
||
|
|
||
|
\includegraphics[width=0.98\textwidth]{images/resistor.png}
|
||
|
|
||
|
\end{columns}
|
||
|
|
||
|
## Ohm's Law
|
||
|
|
||
|
Ohm's Law relates current to potential and resistance.
|
||
|
|
||
|
$$ V = IR $$
|
||
|
$$ I=\frac{V}{R} $$
|
||
|
$$ R = \frac{V}{I} $$
|
||
|
|
||
|
* V = Potential in Volts (\si{\volt})
|
||
|
* I = Current in Amperes (\si{\ampere})
|
||
|
* R = Resistance in Ohms (\si{\ohm})
|
||
|
|
||
|
## Ohm's Law: Example
|
||
|
|
||
|
The datasheet for an LED says that the maximum continuous current is
|
||
|
\SI{15}{\milli\ampere}. Your circuit operates at \SI{5}{\volt}\footnotemark[1]. How
|
||
|
big should your resistor be?
|
||
|
|
||
|
$$ \si{\ohm} = \frac{\SI{5}{\volt}}{\SI{0.015}{\ampere}} = 333.\overline{3}\si{\ohm} $$
|
||
|
|
||
|
How much current for our *cheet sheet* value?
|
||
|
|
||
|
$$ \si{\ampere} = \frac{\SI{5}{\volt}}{\SI{400}{\ohm}} = \SI{12.5}{\milli\ampere} $$
|
||
|
|
||
|
\footnotetext[1]{\tiny Actually, this calculation is inaccurate. LEDs will have a *forward voltage drop* of between \SI{300}{\milli\volt} and \SI{700}{\milli\volt} this should be subtracted from \si{\volt} above... but it's not critical.}
|
||
|
|
||
|
## Buttons
|
||
|
|
||
|
- Buttons connect _or_ disconnect two wires/parts
|
||
|
- Momentary Switch: Normally Closed (NC), Normally Open (NO)
|
||
|
- Toggle Switch
|
||
|
|
||
|
## Circuits
|
||
|
|
||
|
A circuit is a completed loop from HIGH potential (voltage) to LOW,
|
||
|
which causes current to flow through some other components along the
|
||
|
way.
|
||
|
|
||
|
## Transducers {.fragile}
|
||
|
|
||
|
Often these *other* components are *transducers*, which convert
|
||
|
electrical energy into another sort of energy:
|
||
|
|
||
|
| |
|
||
|
-------|--------:
|
||
|
Speaker|Electrical $\rightarrow$ Sound
|
||
|
Microphone|Sound $\rightarrow$ Electrical
|
||
|
LED|Electrical $\rightarrow$ Light
|
||
|
LED|Light $\rightarrow$ Electrical
|
||
|
Piezoelectric|Electrical $\rightarrow$ Motion
|
||
|
|
||
|
<!-- ## Piezo Buzzer -->
|
||
|
|
||
|
<!-- - Piezoelectric elements change shape when voltage is applied -->
|
||
|
<!-- - Thin discs can be made to oscillate and create sound. -->
|
||
|
<!-- - Contains oscillator circuit -->
|
||
|
<!-- - Two connections: Vcc, GND -->
|
||
|
<!-- - Use a switch; connected = annoying tone, disconnected = glorious silence -->
|
||
|
|
||
|
## Power
|
||
|
|
||
|
The power supply provides the energy to drive the system.
|
||
|
|
||
|
Can be a:
|
||
|
|
||
|
* Voltage Regulator (converts one potential to another)
|
||
|
* Batteries
|
||
|
* Solar Panel
|
||
|
|
||
|
In our circuits, your laptop is converting it's power source to $\SI{5}{\volt}$ and
|
||
|
delivering power to our circuit via USB.
|
||
|
|
||
|
## $\mu$Controller
|
||
|
|
||
|
Microcontroller ($\mu$C) is a *processor*, *memory* and a few *peripherals* on a standalone
|
||
|
chip.
|
||
|
|
||
|
Processor
|
||
|
: is a group of transistors that understands a dozen or so
|
||
|
commands (ADD, SUB, JUMP..)
|
||
|
|
||
|
Memory
|
||
|
: a circuit that can hold values.
|
||
|
|
||
|
Peripherals
|
||
|
: Vary chip to chip, but often include timers, communications and
|
||
|
ADC, DAC.
|
||
|
|
||
|
Seems complicated, but really simple. They read a command from the
|
||
|
start of memory, then execute the command. At the end of the command,
|
||
|
read the next command from the next memory cell and
|
||
|
repeat^[some commands change the address of the next fetched command]
|
||
|
|
||
|
## Digital Signals
|
||
|
|
||
|
- Vcc: The power supply of the circuit elements
|
||
|
- GND: The reference voltage (usually \SI{0}{\volt})
|
||
|
- Connecting a part to Vcc = Logical 1 or High
|
||
|
- Connecting to GND = Logical 0 or Low
|
||
|
- Connecting various pins to Vcc or Ground is all the $\mu$C can do to
|
||
|
talk to the world [^4]
|
||
|
|
||
|
[^4]: w/o fancy peripherals or dirty tricks
|
||
|
|
||
|
## $\mu$Controller INPUT and OUTPUT
|
||
|
|
||
|
Most of the pins on the Arduino can be set for INPUT or OUTPUT mode.
|
||
|
|
||
|
- INPUT mode pins listen for a signal ($0$ or $1$) from another device
|
||
|
- OUTPUT mode pins drive the pin High or Low
|
||
|
|
||
|
## Floating Pins
|
||
|
|
||
|
What's happens if an INPUT mode pin tries to read the value of a pin
|
||
|
that is connected to nothing? Is that a $1$ or $0$?
|
||
|
|
||
|
\center{\huge {No one knows!}}
|
||
|
|
||
|
It's dependant of transient charges, static, nearby electric fields,
|
||
|
the phase of the moon, \ldots Whenever you want to check a digital
|
||
|
signal, make sure that something is *driving* it (ensuring Vcc or GND).
|
||
|
|
||
|
## $\mu$C + Digital Signals as Switches
|
||
|
|
||
|
If one end of an LED is connected to ground, and the other end is
|
||
|
connected to an OUTPUT pin on a $\mu$Controller, then:
|
||
|
|
||
|
- If the $\mu$C sets the pin High (Vcc, $\SI{5}{\volt}$) then current
|
||
|
will flow from the pin through the LED and turn it on.
|
||
|
- If $\mu$C sets the pin Low (GND, $\SI{0}{\volt}$) then the current
|
||
|
will not flow and the LED is off.
|
||
|
|
||
|
# Let's start programming
|
||
|
|
||
|
## Configure Arduino
|
||
|
|
||
|
\begin{center}
|
||
|
\includegraphics[width=0.98\textwidth]{images/arduino-board.png}
|
||
|
\end{center}
|
||
|
|
||
|
* Board: Arduino/Genuino UNO
|
||
|
* Port: \ldots
|
||
|
|
||
|
<!-- ## Fetch the Class Code -->
|
||
|
|
||
|
<!-- - Download and extract: [https://nocko.se/assets/arduino-medway.zip](https://nocko.se/assets/arduino-medway.zip) -->
|
||
|
<!-- - File$\rightarrow$Preferences -->
|
||
|
<!-- - Browse for sketchbook -->
|
||
|
<!-- - Point it at the `sketchbook` subfolder of the extracted download -->
|
||
|
<!-- - You should now see a list of projects in the -->
|
||
|
<!-- File$\rightarrow$Sketchbook menu. -->
|
||
|
|
||
|
## The Code Environment
|
||
|
|
||
|
\begin{center}
|
||
|
\includegraphics[width=0.95\textwidth]{images/arduino-toolbar.png}
|
||
|
\end{center}
|
||
|
|
||
|
## Your first Program
|
||
|
|
||
|
~~~ C
|
||
|
/* the setup function runs once on reset / power */
|
||
|
void setup() {
|
||
|
/* set pin 13 as an output */
|
||
|
pinMode(13, OUTPUT);
|
||
|
}
|
||
|
|
||
|
/* the loop function repeats forever */
|
||
|
void loop() {
|
||
|
digitalWrite(13, HIGH); // turn on LED
|
||
|
delay(1000); // wait for a second
|
||
|
digitalWrite(13, LOW); // turn the off LED
|
||
|
delay(1000); // wait for a second
|
||
|
}
|
||
|
~~~
|
||
|
|
||
|
# Add Some Parts
|
||
|
|
||
|
## Breadboard
|
||
|
|
||
|
![Breadboard](images/breadboard.png)\
|
||
|
|
||
|
- Connectors gently pinch component leads, wires.
|
||
|
- Have internal connections
|
||
|
|
||
|
## Power Up the Rails
|
||
|
|
||
|
We use the long rows to distribute power. The Arduino outputs
|
||
|
$\SI{5}{\volt}$ on the pin marked `5V`, the reference (GND) is marked
|
||
|
`GND`.
|
||
|
|
||
|
![Arduino + Breadboard](images/bb+uno.png)
|
||
|
|
||
|
<!-- ## Buzzer: Hardware -->
|
||
|
|
||
|
<!-- \begin{center} -->
|
||
|
<!-- \includegraphics[width=0.98\textwidth]{images/buzzer-breadboard.png} -->
|
||
|
<!-- \end{center} -->
|
||
|
|
||
|
<!-- ## Buzzer: Software -->
|
||
|
|
||
|
<!-- ~~~ C -->
|
||
|
<!-- #define BUZZER 8 /* Make BUZZER same as pin 8 */ -->
|
||
|
|
||
|
<!-- void setup() { -->
|
||
|
<!-- pinMode(BUZZER, OUTPUT); -->
|
||
|
<!-- digitalWrite(BUZZER, HIGH); /* Turn off buzzer */ -->
|
||
|
<!-- } -->
|
||
|
<!-- void loop() { -->
|
||
|
<!-- digitalWrite(BUZZER, LOW); /* Turn on buzzer */ -->
|
||
|
<!-- delay(100); /* wait for 100ms */ -->
|
||
|
<!-- digitalWrite(BUZZER, HIGH); /* Turn off buzzer */ -->
|
||
|
<!-- delay(900); /* wait 900ms */ -->
|
||
|
<!-- } -->
|
||
|
<!-- ~~~ -->
|
||
|
|
||
|
## Push Button: Hardware
|
||
|
|
||
|
\begin{center}
|
||
|
\includegraphics[width=0.98\textwidth]{images/bb+switch.png}
|
||
|
\end{center}
|
||
|
|
||
|
## Push Button: Hardware, Pt. 2
|
||
|
|
||
|
\begin{center}
|
||
|
\includegraphics[width=0.98\textwidth]{images/bb+switch+zoom.png}
|
||
|
\end{center}
|
||
|
|
||
|
## Pullup / Pulldown Resistors
|
||
|
|
||
|
Reading a floating pin is **bad**. A switch only connects
|
||
|
and disconnects a wire. When the wire is disconnected... the INPUT pin
|
||
|
is floating!
|
||
|
|
||
|
\vfill
|
||
|
|
||
|
**Solution:**
|
||
|
|
||
|
Connect the pin to Vcc so that it reads High; use a
|
||
|
resistor to prevent short circuit (limit current).
|
||
|
|
||
|
## Push Button: Software (Part 1)
|
||
|
|
||
|
~~~ C
|
||
|
#define BUTTON 2
|
||
|
#define LED 13
|
||
|
|
||
|
int button_state = 0;
|
||
|
|
||
|
void setup() {
|
||
|
pinMode(BUTTON, INPUT);
|
||
|
pinMode(LED, OUTPUT);
|
||
|
digitalWrite(LED, LOW); /* Start w/ LED off */
|
||
|
}
|
||
|
~~~
|
||
|
## Programming Note: Variables
|
||
|
|
||
|
Declare a variable:
|
||
|
|
||
|
~~~ C
|
||
|
int delay_ms = 1000;
|
||
|
~~~
|
||
|
|
||
|
`<type> <name> [= <initial value>];` (value optional)
|
||
|
|
||
|
It's a name, like a preprocessor `#define`, but the value can change
|
||
|
at *runtime*
|
||
|
|
||
|
<!-- ## RGB LED -->
|
||
|
|
||
|
<!-- - Three LEDs in the same package. -->
|
||
|
<!-- - LEDs share the same `GND` ($-$) pin, one ($+$) side of each LED -->
|
||
|
<!-- - Connect `-` to negative rail, R, G, & B to pins 3,5, & 6 on Arduino -->
|
||
|
|
||
|
<!-- \centering -->
|
||
|
<!-- \includegraphics[width=0.60\textwidth]{images/bb+uno+led.png} -->
|
||
|
|
||
|
## Programming Note: *If* Statement
|
||
|
|
||
|
~~~ C
|
||
|
if (condition) {
|
||
|
// body: Runs if condition true ( != 0)
|
||
|
} else {
|
||
|
// Runs if condition false ( == 0 )
|
||
|
}
|
||
|
~~~
|
||
|
|
||
|
- body code inside curly braces: `{` `}`
|
||
|
- **condition** evaluates to 0 $\rightarrow$ body code skipped
|
||
|
- else section is optional, runs if **condition** evaluates to 0
|
||
|
- **condition** evaluates to *not* 0 $\rightarrow$ body code runs
|
||
|
|
||
|
## Programming Note: `==`
|
||
|
|
||
|
In C-like languages, the `==` operator checks if two things
|
||
|
(statements, variables, \ldots) are equal to each other.
|
||
|
|
||
|
- It returns `1` if the items are equal, *or*
|
||
|
- It returns `0` if the items are not equal
|
||
|
|
||
|
## Push Button: Software (Part 2)
|
||
|
|
||
|
~~~ C
|
||
|
void loop() {
|
||
|
button_state = digitalRead(BUTTON);
|
||
|
if (button_state == HIGH) {
|
||
|
digitalWrite(LED, LOW);
|
||
|
} else {
|
||
|
digitalWrite(LED, HIGH);
|
||
|
}
|
||
|
}
|
||
|
~~~
|
||
|
|
||
|
# More Parts
|
||
|
|
||
|
## Potentiometer
|
||
|
|
||
|
*Puh - ten - she - ometer*
|
||
|
|
||
|
- *Pot* for short
|
||
|
- A Voltage Divider
|
||
|
- Voltage at *Wiper* is somewhere between potential at the two
|
||
|
terminals.
|
||
|
- The exact wiper potential depends on the position of the knob/lever.
|
||
|
|
||
|
## ADC: Analog to Digital Converter
|
||
|
|
||
|
- A peripheral of the $\mu$Controller
|
||
|
- Measures Potential, outputs a number
|
||
|
- In our case, $\SI{0}{\volt}\rightarrow0$ and
|
||
|
$\SI{5}{\volt}\rightarrow1023$
|
||
|
- A0-A5 pins on Arduino can be used
|
||
|
- Fun uses: Reading pot position, sampling audio, reading from sensors
|
||
|
|
||
|
## The Pot Hookup
|
||
|
|
||
|
\begin{center}
|
||
|
\includegraphics[width=0.98\textwidth]{images/bb+switch+pot.png}
|
||
|
\end{center}
|
||
|
|
||
|
Connect center pin to `A0`, outer pins to ($+$) and ($-$) rails
|
||
|
|
||
|
## Pot Code
|
||
|
|
||
|
`analogRead(`*pin*`)` returns the current state of the pin (0--1023),
|
||
|
it can be assigned to a variable.
|
||
|
|
||
|
~~~ C
|
||
|
void loop() {
|
||
|
delay_ms = analogRead(A0);
|
||
|
digitalWrite(LED, HIGH);
|
||
|
delay(delay_ms);
|
||
|
digitalWrite(LED, LOW);
|
||
|
}
|
||
|
~~~
|
||
|
|
||
|
Each time through the loop, a new `delay_ms` value is read. Since the
|
||
|
subsequent delay calls use `delay_ms`, the blink rate changes with
|
||
|
knob position.
|
||
|
|
||
|
# Shall we play a game?
|
||
|
|
||
|
## Hooking up a bunch of LEDs
|
||
|
|
||
|
\begin{center}
|
||
|
\includegraphics[width=0.65\textwidth]{images/bb+switch+pot+leds.png}
|
||
|
\end{center}
|
||
|
|
||
|
Looks complicated, but for each LED: The short leg goes to ground, the
|
||
|
long leg goes to one end of a resistor, and the other end of the
|
||
|
resistor goes to the arduino pin.
|
||
|
|
||
|
## Programming Note: `for` Loop
|
||
|
|
||
|
~~~ C
|
||
|
for ( initializer ; condition; increment ) {
|
||
|
// This body will repeat until condition != 0
|
||
|
}
|
||
|
~~~
|
||
|
|
||
|
initializer
|
||
|
: Executed once at beginning of loop. Often used to declare a local
|
||
|
variable.
|
||
|
|
||
|
condition
|
||
|
: Loop will repeat until condition $\neq0$
|
||
|
|
||
|
increment
|
||
|
: Runs *after* each loop. Often used to increment variables.
|
||
|
|
||
|
\center{*All fields are optional*}
|
||
|
|
||
|
## Game Code: Part. 1; Cylon Attack
|
||
|
|
||
|
~~~ C
|
||
|
loop () {
|
||
|
for (int i = 4; i <= 7; i++) {
|
||
|
delay_ms = analogRead(A0);
|
||
|
analogWrite(i - 1, LOW);
|
||
|
analogWrite(i, HIGH);
|
||
|
delay(delay_ms);
|
||
|
}
|
||
|
for (int i = 6; i >= 3; i--) {
|
||
|
delay_ms = analogRead(A0);
|
||
|
analogWrite(i + 1, LOW);
|
||
|
analogWrite(i, HIGH);
|
||
|
delay(delay_ms);
|
||
|
}
|
||
|
}
|
||
|
~~~
|
||
|
|
||
|
## Programming Note: Functions
|
||
|
|
||
|
~~~ C
|
||
|
void my_function(int arg1, ...) {
|
||
|
// Do fun things
|
||
|
}
|
||
|
~~~
|
||
|
|
||
|
void:
|
||
|
: Return type. Void means nothing returned. Can be any type.
|
||
|
|
||
|
my_function:
|
||
|
: A name for your function
|
||
|
|
||
|
arguments:
|
||
|
: A type and name for any parameters you want to use in your function
|
||
|
from the outside.
|
||
|
|
||
|
Define a function once, you can use it again and again. Beats
|
||
|
copy/pasting.
|
||
|
|
||
|
## Winner, Winner, Chicken Dinner
|
||
|
|
||
|
~~~ C
|
||
|
void check_delay(int cur_led, int delay_ms) {
|
||
|
if (cur_led != 5) { // No chance of winner
|
||
|
delay(delay_ms); // Do a normal delay
|
||
|
return;
|
||
|
}
|
||
|
int start = millis();
|
||
|
while (millis() < start+delay_ms) {
|
||
|
if (digitalRead(BUTTON) == LOW) {
|
||
|
for (int i = 3; i <= 7; i++) {
|
||
|
digitalWrite(i, HIGH);
|
||
|
}; for (;;); // Loop until reset
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
~~~
|
||
|
|
||
|
## Putting it Together
|
||
|
|
||
|
~~~ C
|
||
|
loop () {
|
||
|
for (int i = 4; i <= 7; i++) {
|
||
|
delay_ms = analogRead(A0);
|
||
|
analogWrite(i - 1, LOW);
|
||
|
analogWrite(i, HIGH);
|
||
|
check_delay(i, delay_ms);
|
||
|
}
|
||
|
for (int i = 6; i >= 3; i--) {
|
||
|
...
|
||
|
check_delay(i, delay_ms);
|
||
|
}
|
||
|
}
|
||
|
~~~
|
||
|
|
||
|
## The End?
|
||
|
|
||
|
\begin{center}
|
||
|
\LARGE{Let's build some cool stuff!}
|
||
|
\end{center}
|