Adds Arduino Intro - Whack-a-mole Class
After Width: | Height: | Size: 340 KiB |
Before Width: | Height: | Size: 38 KiB After Width: | Height: | Size: 340 KiB |
After Width: | Height: | Size: 174 KiB |
After Width: | Height: | Size: 211 KiB |
After Width: | Height: | Size: 836 KiB |
After Width: | Height: | Size: 738 KiB |
|
@ -7,7 +7,16 @@ lang: en-CA
|
||||||
colorlinks: true
|
colorlinks: true
|
||||||
...
|
...
|
||||||
|
|
||||||
# Goals
|
## Goals
|
||||||
|
|
||||||
|
By the end of this class, you'll:
|
||||||
|
|
||||||
|
- Know how to create programs for Arduino and run them.
|
||||||
|
- Have learned about digital input and output, reading switches and
|
||||||
|
lighting LEDs
|
||||||
|
- Have created a *Whack-a-Mole* type game.
|
||||||
|
- Be prepared to follow Arduino tutorials online and continue
|
||||||
|
exploring.
|
||||||
|
|
||||||
## Unlondon
|
## Unlondon
|
||||||
|
|
||||||
|
@ -55,17 +64,39 @@ Hacker, Church of the Weird Machine, Odd Duck
|
||||||
\end{center}
|
\end{center}
|
||||||
\end{columns}
|
\end{columns}
|
||||||
|
|
||||||
## Raphael: The Vital Info
|
## Raphael: Day Job
|
||||||
|
|
||||||
- Manufacturing Engineer
|
Mechanical Engineer
|
||||||
- Instructor at Fanshawe
|
|
||||||
- Maker
|
- Working in the medical device industry
|
||||||
|
- Experience in medical device R&D and Manufacturing
|
||||||
|
- Teaching SolidWorks CAD at Fanshawe
|
||||||
|
|
||||||
|
## Raphael: The Fun Stuff
|
||||||
|
|
||||||
|
Thinker, Jack of all Trades - Master of None
|
||||||
|
|
||||||
|
\begin{columns}[c]
|
||||||
|
\column{0.50\textwidth}
|
||||||
|
\begin{itemize}
|
||||||
|
\item Arduino for Fun, and Odd Jobs
|
||||||
|
\item 3D Printer Hobbyist
|
||||||
|
\item PC Builder \& Gamer
|
||||||
|
\item Fish keeper
|
||||||
|
\end{itemize}
|
||||||
|
\column{0.50\textwidth}
|
||||||
|
\begin{center}
|
||||||
|
\includegraphics[width=0.80\textwidth]{images/rapha1.png}
|
||||||
|
\vspace{5mm}
|
||||||
|
\includegraphics[width=0.80\textwidth]{images/rapha2.png}
|
||||||
|
\end{center}
|
||||||
|
\end{columns}
|
||||||
|
|
||||||
# What's in your kit?
|
# What's in your kit?
|
||||||
|
|
||||||
## Kit Contents
|
## Kit Contents
|
||||||
|
|
||||||
- Arduino Uno R3 Clone
|
- Arduino Uno R3
|
||||||
- Solderless Breadboard
|
- Solderless Breadboard
|
||||||
- Connecting wires
|
- Connecting wires
|
||||||
- LEDs
|
- LEDs
|
||||||
|
@ -82,14 +113,6 @@ It's a kit (on a board) with the bare minimum components to easily use the $\mu$
|
||||||
hardware. They do the basic, boring design needed for any board, so users only
|
hardware. They do the basic, boring design needed for any board, so users only
|
||||||
need to add the neat stuff.
|
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
|
## Arduino Software
|
||||||
|
|
||||||
The Arduino folks also adapted an *Integrated Development Environment*
|
The Arduino folks also adapted an *Integrated Development Environment*
|
||||||
|
@ -99,27 +122,48 @@ their boards and then write the programs to the $\mu$C.
|
||||||
\Large Get the Arduino IDE:
|
\Large Get the Arduino IDE:
|
||||||
[https://www.arduino.cc/en/Main/Software](https://www.arduino.cc/en/Main/Software)
|
[https://www.arduino.cc/en/Main/Software](https://www.arduino.cc/en/Main/Software)
|
||||||
|
|
||||||
## Installation
|
<!-- ## Installation -->
|
||||||
|
|
||||||
\Large Get installing
|
<!-- \Large Get installing -->
|
||||||
|
|
||||||
# Circuit Basics
|
# Circuit Basics
|
||||||
|
|
||||||
## Current
|
## Current
|
||||||
|
|
||||||
Current is the flow of charge through a circuit. Conventionally we
|
Current is the flow of charge through a circuit. Measured in Amperes
|
||||||
think of this as happening from HIGH ($+$) to LOW ($-$)
|
(\si{\ampere}).
|
||||||
|
|
||||||
## Voltage / Potential / Resistance
|
## Resistance / Impedance
|
||||||
|
|
||||||
Voltage is how fast the current can move in the circuit. River
|
Circuits have a resistance to current flow that depends on the parts
|
||||||
metaphor:
|
in the circuit.
|
||||||
|
|
||||||
- current = flow rate: ($\si{\liter\per\second}$)
|
Measured in Ohms (\si{\ohm})
|
||||||
- voltage = change in height: ($\si{\meter}$)
|
|
||||||
|
|
||||||
Other devices in a circuit can impede / effect current flow. We'll
|
## Voltage
|
||||||
call them resistance(s).
|
|
||||||
|
Voltage is a potential (akin to a pressure) pushing the current
|
||||||
|
through a circuit. Current is said to flow from higher (+) voltage
|
||||||
|
to lower (-) voltage.
|
||||||
|
|
||||||
|
Measured in Volts (\si{\volt})
|
||||||
|
|
||||||
|
## Ohm's Law; Light
|
||||||
|
|
||||||
|
Voltage, Current and Resistance are related to each other.
|
||||||
|
|
||||||
|
- As voltage increases, current increases
|
||||||
|
- As voltage decreases, current decreases
|
||||||
|
- As resistance increases, current decreases
|
||||||
|
- As resistance decreases, current increases
|
||||||
|
|
||||||
|
## \si{\volt}, \si{\ohm}, \si{\ampere}: The Water Analogy
|
||||||
|
|
||||||
|
If charge were water, then:
|
||||||
|
|
||||||
|
- resistance = obstacles blocking flow
|
||||||
|
- current = flow rate
|
||||||
|
- voltage = change in height *or* pressure.
|
||||||
|
|
||||||
## Diode
|
## Diode
|
||||||
|
|
||||||
|
@ -159,8 +203,7 @@ call them resistance(s).
|
||||||
|
|
||||||
\begin{itemize}
|
\begin{itemize}
|
||||||
\item \emph{Resists}/limits the flow of current
|
\item \emph{Resists}/limits the flow of current
|
||||||
\item Needed for LEDs: $\approx\SI{400}{\ohm}$\\
|
\item Needed for LEDs: $\approx\SI{1000}{\ohm}$\\
|
||||||
(safe for $\le\SI{6}{\volt}$)
|
|
||||||
\item Button Pull-up/down: $\ge\SI{10}{\kilo\ohm}$
|
\item Button Pull-up/down: $\ge\SI{10}{\kilo\ohm}$
|
||||||
\item Color coded, Google it
|
\item Color coded, Google it
|
||||||
\end{itemize}
|
\end{itemize}
|
||||||
|
@ -170,32 +213,6 @@ call them resistance(s).
|
||||||
|
|
||||||
\end{columns}
|
\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
|
||||||
|
|
||||||
- Buttons connect _or_ disconnect two wires/parts
|
- Buttons connect _or_ disconnect two wires/parts
|
||||||
|
@ -331,19 +348,21 @@ will not flow and the LED is off.
|
||||||
## Your first Program
|
## Your first Program
|
||||||
|
|
||||||
~~~ C
|
~~~ C
|
||||||
|
#define LED 13
|
||||||
|
|
||||||
/* the setup function runs once on reset / power */
|
/* the setup function runs once on reset / power */
|
||||||
void setup() {
|
void setup() {
|
||||||
/* set pin 13 as an output */
|
pinMode(LED, OUTPUT);
|
||||||
pinMode(13, OUTPUT);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* the loop function repeats forever */
|
/* loop() repeats until reset or power off */
|
||||||
void loop() {
|
void loop() {
|
||||||
digitalWrite(13, HIGH); // turn on LED
|
digitalWrite(LED, HIGH); // turn on LED
|
||||||
delay(1000); // wait for a second
|
|
||||||
digitalWrite(13, LOW); // turn the off LED
|
|
||||||
delay(1000); // wait for a second
|
delay(1000); // wait for a second
|
||||||
|
digitalWrite(LED, LOW); // turn the off LED
|
||||||
|
delay(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
~~~
|
~~~
|
||||||
|
|
||||||
# Add Some Parts
|
# Add Some Parts
|
||||||
|
@ -363,36 +382,13 @@ $\SI{5}{\volt}$ on the pin marked `5V`, the reference (GND) is marked
|
||||||
|
|
||||||
![Arduino + Breadboard](images/bb+uno.png)
|
![Arduino + Breadboard](images/bb+uno.png)
|
||||||
|
|
||||||
<!-- ## Buzzer: Hardware -->
|
## Buzzer & Button: 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}
|
\begin{center}
|
||||||
\includegraphics[width=0.98\textwidth]{images/bb+switch.png}
|
\includegraphics[width=0.98\textwidth]{images/buzzer-breadboard.png}
|
||||||
\end{center}
|
\end{center}
|
||||||
|
|
||||||
## Push Button: Hardware, Pt. 2
|
## Push Button: Zoom
|
||||||
|
|
||||||
\begin{center}
|
\begin{center}
|
||||||
\includegraphics[width=0.98\textwidth]{images/bb+switch+zoom.png}
|
\includegraphics[width=0.98\textwidth]{images/bb+switch+zoom.png}
|
||||||
|
@ -411,18 +407,24 @@ is floating!
|
||||||
Connect the pin to Vcc so that it reads High; use a
|
Connect the pin to Vcc so that it reads High; use a
|
||||||
resistor to prevent short circuit (limit current).
|
resistor to prevent short circuit (limit current).
|
||||||
|
|
||||||
## Push Button: Software (Part 1)
|
## Buzzer: Zoom
|
||||||
|
|
||||||
|
\begin{center}
|
||||||
|
\includegraphics[width=0.98\textwidth]{images/buzzer-zoom.png}
|
||||||
|
\end{center}
|
||||||
|
|
||||||
|
## Buzzer / Button: Software (Part 1)
|
||||||
|
|
||||||
~~~ C
|
~~~ C
|
||||||
#define BUTTON 2
|
#define BUTTON 2
|
||||||
#define LED 13
|
#define BUZZER 8
|
||||||
|
|
||||||
int button_state = 0;
|
int button_state;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
pinMode(BUTTON, INPUT);
|
pinMode(BUTTON, INPUT);
|
||||||
pinMode(LED, OUTPUT);
|
pinMode(BUZZER, OUTPUT);
|
||||||
digitalWrite(LED, LOW); /* Start w/ LED off */
|
digitalWrite(BUZZER, LOW); /* Start w/ LED off */
|
||||||
}
|
}
|
||||||
~~~
|
~~~
|
||||||
## Programming Note: Variables
|
## Programming Note: Variables
|
||||||
|
@ -430,7 +432,7 @@ void setup() {
|
||||||
Declare a variable:
|
Declare a variable:
|
||||||
|
|
||||||
~~~ C
|
~~~ C
|
||||||
int delay_ms = 1000;
|
int button_state = HIGH;
|
||||||
~~~
|
~~~
|
||||||
|
|
||||||
`<type> <name> [= <initial value>];` (value optional)
|
`<type> <name> [= <initial value>];` (value optional)
|
||||||
|
@ -438,15 +440,6 @@ int delay_ms = 1000;
|
||||||
It's a name, like a preprocessor `#define`, but the value can change
|
It's a name, like a preprocessor `#define`, but the value can change
|
||||||
at *runtime*
|
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
|
## Programming Note: *If* Statement
|
||||||
|
|
||||||
~~~ C
|
~~~ C
|
||||||
|
@ -470,15 +463,55 @@ In C-like languages, the `==` operator checks if two things
|
||||||
- It returns `1` if the items are equal, *or*
|
- It returns `1` if the items are equal, *or*
|
||||||
- It returns `0` if the items are not equal
|
- It returns `0` if the items are not equal
|
||||||
|
|
||||||
|
## Programming Note: Functions
|
||||||
|
|
||||||
|
Functions make it easy to reuse code. You already know / use several
|
||||||
|
functions:
|
||||||
|
|
||||||
|
- pinMode
|
||||||
|
- digitalWrite
|
||||||
|
- delay
|
||||||
|
|
||||||
|
digitalRead(pin number) returns HIGH or LOW depending on current state
|
||||||
|
of any **INPUT** pin.
|
||||||
|
|
||||||
|
You can write your own functions!
|
||||||
|
|
||||||
|
## Programming Note: Writing 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. Better than
|
||||||
|
copy/pasting.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Push Button: Software (Part 2)
|
## Push Button: Software (Part 2)
|
||||||
|
|
||||||
~~~ C
|
~~~ C
|
||||||
|
void buzz(int ms) {
|
||||||
|
digitalWrite(BUZZER, HIGH);
|
||||||
|
delay(ms);
|
||||||
|
digitalWrite(BUZZER, LOW);
|
||||||
|
}
|
||||||
void loop() {
|
void loop() {
|
||||||
button_state = digitalRead(BUTTON);
|
button_state = digitalRead(BUTTON);
|
||||||
if (button_state == HIGH) {
|
if (button_state == LOW) {
|
||||||
digitalWrite(LED, LOW);
|
buzz(100);
|
||||||
} else {
|
|
||||||
digitalWrite(LED, HIGH);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
~~~
|
~~~
|
||||||
|
@ -514,21 +547,24 @@ Connect center pin to `A0`, outer pins to ($+$) and ($-$) rails
|
||||||
|
|
||||||
## Pot Code
|
## Pot Code
|
||||||
|
|
||||||
`analogRead(`*pin*`)` returns the current state of the pin (0--1023),
|
`analogRead(`*pin*`)` returns the voltage at the pin (0--1023), it can
|
||||||
it can be assigned to a variable.
|
be used directly or via variable.
|
||||||
|
|
||||||
~~~ C
|
~~~ C
|
||||||
|
#define LED 13
|
||||||
|
void setup() {
|
||||||
|
pinMode(LED, OUTPUT);
|
||||||
|
}
|
||||||
void loop() {
|
void loop() {
|
||||||
delay_ms = analogRead(A0);
|
|
||||||
digitalWrite(LED, HIGH);
|
digitalWrite(LED, HIGH);
|
||||||
delay(delay_ms);
|
delay(analogRead(A0));
|
||||||
digitalWrite(LED, LOW);
|
digitalWrite(LED, LOW);
|
||||||
|
delay(analogRead(A0));
|
||||||
}
|
}
|
||||||
~~~
|
~~~
|
||||||
|
|
||||||
Each time through the loop, a new `delay_ms` value is read. Since the
|
Since the delay() calls use the result of analogRead (0-1023), the
|
||||||
subsequent delay calls use `delay_ms`, the blink rate changes with
|
blink rate changes with knob position.
|
||||||
knob position.
|
|
||||||
|
|
||||||
# Shall we play a game?
|
# Shall we play a game?
|
||||||
|
|
||||||
|
@ -538,15 +574,21 @@ knob position.
|
||||||
\includegraphics[width=0.65\textwidth]{images/bb+switch+pot+leds.png}
|
\includegraphics[width=0.65\textwidth]{images/bb+switch+pot+leds.png}
|
||||||
\end{center}
|
\end{center}
|
||||||
|
|
||||||
|
## LEDs, the first one
|
||||||
|
|
||||||
Looks complicated, but for each LED: The short leg goes to ground, the
|
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
|
long leg goes to one end of a resistor, and the other end of the
|
||||||
resistor goes to the arduino pin.
|
resistor goes to the arduino pin.
|
||||||
|
|
||||||
|
\begin{center}
|
||||||
|
\includegraphics[width=0.55\textwidth]{images/one-led.png}
|
||||||
|
\end{center}
|
||||||
|
|
||||||
## Programming Note: `for` Loop
|
## Programming Note: `for` Loop
|
||||||
|
|
||||||
~~~ C
|
~~~ C
|
||||||
for ( initializer ; condition; increment ) {
|
for ( initializer ; condition; increment ) {
|
||||||
// This body will repeat until condition != 0
|
// This body will repeat while condition != 0
|
||||||
}
|
}
|
||||||
~~~
|
~~~
|
||||||
|
|
||||||
|
@ -555,69 +597,106 @@ initializer
|
||||||
variable.
|
variable.
|
||||||
|
|
||||||
condition
|
condition
|
||||||
: Loop will repeat until condition $\neq0$
|
: Loop will repeat while condition is true ($\neq0$)
|
||||||
|
|
||||||
increment
|
increment
|
||||||
: Runs *after* each loop. Often used to increment variables.
|
: Runs *after* each loop. Often used to increment variables.
|
||||||
|
|
||||||
\center{*All fields are optional*}
|
\center{*All fields are optional*}
|
||||||
|
|
||||||
## Game Code: Part. 1; Cylon Attack
|
## Cylon Simulator: Part. 1; Pin Setup
|
||||||
|
|
||||||
|
~~~ C
|
||||||
|
setup() {
|
||||||
|
pinMode(3, OUTPUT);
|
||||||
|
pinMode(4, OUTPUT);
|
||||||
|
pinMode(5, OUTPUT);
|
||||||
|
pinMode(6, OUTPUT);
|
||||||
|
pinMode(7, OUTPUT);
|
||||||
|
}
|
||||||
|
~~~
|
||||||
|
|
||||||
|
## Cylon Simulator: Part. 2
|
||||||
|
|
||||||
~~~ C
|
~~~ C
|
||||||
loop () {
|
loop () {
|
||||||
for (int i = 4; i <= 7; i++) {
|
for (int i = 4; i <= 7; i++) {
|
||||||
delay_ms = analogRead(A0);
|
delay_ms = analogRead(A0);
|
||||||
analogWrite(i - 1, LOW);
|
digitalWrite(i - 1, LOW);
|
||||||
analogWrite(i, HIGH);
|
digitalWrite(i, HIGH);
|
||||||
delay(delay_ms);
|
delay(delay_ms);
|
||||||
}
|
}
|
||||||
for (int i = 6; i >= 3; i--) {
|
for (int i = 6; i >= 3; i--) {
|
||||||
delay_ms = analogRead(A0);
|
delay_ms = analogRead(A0);
|
||||||
analogWrite(i + 1, LOW);
|
digitalWrite(i + 1, LOW);
|
||||||
analogWrite(i, HIGH);
|
digitalWrite(i, HIGH);
|
||||||
delay(delay_ms);
|
delay(delay_ms);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
~~~
|
~~~
|
||||||
|
|
||||||
## Programming Note: Functions
|
## Programming Note: `while` Loop
|
||||||
|
|
||||||
~~~ C
|
~~~ C
|
||||||
void my_function(int arg1, ...) {
|
while ( statement ) {
|
||||||
// Do fun things
|
// This body will repeat while condition is true
|
||||||
|
// True means statement != 0
|
||||||
}
|
}
|
||||||
~~~
|
~~~
|
||||||
|
|
||||||
void:
|
initializer
|
||||||
: Return type. Void means nothing returned. Can be any type.
|
: Executed once at beginning of loop. Often used to declare a local
|
||||||
|
variable.
|
||||||
|
|
||||||
my_function:
|
condition
|
||||||
: A name for your function
|
: Loop will repeat while condition is true ($\neq0$)
|
||||||
|
|
||||||
arguments:
|
increment
|
||||||
: A type and name for any parameters you want to use in your function
|
: Runs *after* each loop. Often used to increment variables.
|
||||||
from the outside.
|
|
||||||
|
|
||||||
Define a function once, you can use it again and again. Beats
|
\center{*All fields are optional*}
|
||||||
copy/pasting.
|
|
||||||
|
|
||||||
## Winner, Winner, Chicken Dinner
|
## Winner, Winner, Chicken Dinner
|
||||||
|
|
||||||
~~~ C
|
~~~ C
|
||||||
|
#define WINNER 5
|
||||||
void check_delay(int cur_led, int delay_ms) {
|
void check_delay(int cur_led, int delay_ms) {
|
||||||
if (cur_led != 5) { // No chance of winner
|
unsigned long start = millis();
|
||||||
delay(delay_ms); // Do a normal delay
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
int start = millis();
|
|
||||||
while (millis() < start+delay_ms) {
|
while (millis() < start+delay_ms) {
|
||||||
if (digitalRead(BUTTON) == LOW) {
|
if (digitalRead(BUTTON) == LOW) {
|
||||||
|
if (cur_led == WINNER) {
|
||||||
|
do_winner();
|
||||||
|
} else {
|
||||||
|
while (digitalRead(BUTTON) == LOW) {
|
||||||
|
do_loser();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
~~~
|
||||||
|
|
||||||
|
## More functions, pt. 1
|
||||||
|
~~~ C
|
||||||
|
void set_all_leds(int state) {
|
||||||
for (int i = 3; i <= 7; i++) {
|
for (int i = 3; i <= 7; i++) {
|
||||||
digitalWrite(i, HIGH);
|
digitalWrite(i, state);
|
||||||
}; for (;;); // Loop until reset
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void do_loser(void) {
|
||||||
|
buzz(500);
|
||||||
|
}
|
||||||
|
~~~
|
||||||
|
|
||||||
|
## More functions, pt. 2
|
||||||
|
~~~ C
|
||||||
|
void do_winner(void) {
|
||||||
|
set_all_leds(HIGH);
|
||||||
|
buzz(100);
|
||||||
|
delay(100);
|
||||||
|
buzz(100);
|
||||||
|
set_all_leds(LOW);
|
||||||
}
|
}
|
||||||
~~~
|
~~~
|
||||||
|
|
||||||
|
@ -627,8 +706,8 @@ void check_delay(int cur_led, int delay_ms) {
|
||||||
loop () {
|
loop () {
|
||||||
for (int i = 4; i <= 7; i++) {
|
for (int i = 4; i <= 7; i++) {
|
||||||
delay_ms = analogRead(A0);
|
delay_ms = analogRead(A0);
|
||||||
analogWrite(i - 1, LOW);
|
digitalWrite(i - 1, LOW);
|
||||||
analogWrite(i, HIGH);
|
digitalWrite(i, HIGH);
|
||||||
check_delay(i, delay_ms);
|
check_delay(i, delay_ms);
|
||||||
}
|
}
|
||||||
for (int i = 6; i >= 3; i--) {
|
for (int i = 6; i >= 3; i--) {
|
||||||
|
@ -638,8 +717,89 @@ loop () {
|
||||||
}
|
}
|
||||||
~~~
|
~~~
|
||||||
|
|
||||||
## The End?
|
# The End?
|
||||||
|
|
||||||
\begin{center}
|
# Extra Credit
|
||||||
\LARGE{Let's build some cool stuff!}
|
## Ohm's Law
|
||||||
\end{center}
|
|
||||||
|
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{1}{\kilo\ohm}} = \SI{5}{\milli\ampere} $$
|
||||||
|
|
||||||
|
\footnotetext[1]{\tiny Actually, this calculation is inaccurate. LEDs
|
||||||
|
will have a *forward voltage drop* of between \SI{1.8}{\volt} and
|
||||||
|
\SI{3.3}{\volt} this should be subtracted from \si{\volt} above... but
|
||||||
|
it's not critical.}
|
||||||
|
|
||||||
|
## Current Limits, Arduino
|
||||||
|
|
||||||
|
- No single pin should source more that \SI{20}{\milli\ampere} (\SI{40}{\milli\ampere} is absolute max)
|
||||||
|
- Pins are ganged together in groups of 8, no group should source more
|
||||||
|
than \SI{150}{\milli\ampere} total
|
||||||
|
- The whole board cannot source more than \SI{200}{\milli\ampere} total
|
||||||
|
|
||||||
|
Practically speaking, this means that the Arduino cannot drive
|
||||||
|
speakers, most motors, or anything normally mains powered.
|
||||||
|
|
||||||
|
## So\ldots no Arduino smart blender?
|
||||||
|
|
||||||
|
You can control almost anything with an arduino, you just can't power
|
||||||
|
it with the Arduino. There are various devices that let you switch
|
||||||
|
highier powered devices:
|
||||||
|
|
||||||
|
- Transistors
|
||||||
|
- Relays
|
||||||
|
- Solid State Relays
|
||||||
|
- Triac
|
||||||
|
|
||||||
|
## HIGHs and LOWs
|
||||||
|
|
||||||
|
Many different logic levels are in common use: \SI{1.2}{\volt},
|
||||||
|
\SI{1.8}{\volt}, \SI{2.5}{\volt}, \SI{3.3}{\volt}, and
|
||||||
|
\SI{5}{\volt}. The voltage cited is the *nominal* Vcc of the system.
|
||||||
|
|
||||||
|
A HIGH signal is generally any voltage $\geq \frac{2}{3}V_{cc}$.
|
||||||
|
|
||||||
|
A LOW signal is generally any voltage $\leq \frac{1}{3}V_{cc}$.
|
||||||
|
|
||||||
|
## HIGHs and LOWs, pt. 2
|
||||||
|
|
||||||
|
In your travels, you're likely to see both \SI{5}{\volt} and
|
||||||
|
\SI{3.3}{\volt} sensors and peripherals.
|
||||||
|
|
||||||
|
Since $\SI{3.3}{\volt}\geq\frac{2}{3}V_cc$ your Arduino will accept
|
||||||
|
input from a \SI{3.3}{\volt} peripheral without issue.
|
||||||
|
|
||||||
|
If you drive an output to \SI{5}{\volt} while it's connected to a
|
||||||
|
\SI{3.3}{\volt} peripheral with an Arduino **it will blow up your
|
||||||
|
peripheral**.^[In the datasheet for the sensor, it'll have a
|
||||||
|
section called *Absolute Maximums*. Generally \SI{3.3}{\volt} parts
|
||||||
|
won't accept more that $\approx\SI{3.6}{\volt}$, but some will.]
|
||||||
|
|
||||||
|
## HIGHs and LOWs, pt. 3
|
||||||
|
|
||||||
|
Solutions:
|
||||||
|
|
||||||
|
- Level Shifter: A dedicated chip that translates between
|
||||||
|
voltages. Available as uni or bidirectional.
|
||||||
|
|
||||||
|
- Buy a 3.3V Arduino Compatible. Arduinos are available that operate
|
||||||
|
at the lower voltage.
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
#define LED 13
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
pinMode(LED, OUTPUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
digitalWrite(LED, HIGH); // turn on LED
|
||||||
|
delay(1000); // wait for a second
|
||||||
|
digitalWrite(LED, LOW); // turn the off LED
|
||||||
|
delay(1000);
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
#define BUZZER 8
|
||||||
|
#define BUTTON 2
|
||||||
|
|
||||||
|
int button_state;
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
pinMode(BUTTON, INPUT);
|
||||||
|
pinMode(BUZZER, OUTPUT);
|
||||||
|
}
|
||||||
|
void buzz(int ms) {
|
||||||
|
digitalWrite(BUZZER, HIGH);
|
||||||
|
delay(ms);
|
||||||
|
digitalWrite(BUZZER, LOW);
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
button_state = digitalRead(BUTTON);
|
||||||
|
if (button_state == LOW) {
|
||||||
|
buzz(100);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
int delay_ms;
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
pinMode(3, OUTPUT);
|
||||||
|
pinMode(4, OUTPUT);
|
||||||
|
pinMode(5, OUTPUT);
|
||||||
|
pinMode(6, OUTPUT);
|
||||||
|
pinMode(7, OUTPUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
for (int i = 4; i <= 7; i++) {
|
||||||
|
delay_ms = analogRead(A0);
|
||||||
|
digitalWrite(i - 1, LOW);
|
||||||
|
digitalWrite(i, HIGH);
|
||||||
|
delay(delay_ms);
|
||||||
|
}
|
||||||
|
for (int i = 6; i >= 3; i--) {
|
||||||
|
delay_ms = analogRead(A0);
|
||||||
|
digitalWrite(i + 1, LOW);
|
||||||
|
digitalWrite(i, HIGH);
|
||||||
|
delay(delay_ms);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
#define LED 13
|
||||||
|
void setup() {
|
||||||
|
pinMode(LED, OUTPUT);
|
||||||
|
}
|
||||||
|
void loop() {
|
||||||
|
digitalWrite(LED, HIGH);
|
||||||
|
delay(analogRead(A0));
|
||||||
|
digitalWrite(LED, LOW);
|
||||||
|
delay(analogRead(A0));
|
||||||
|
}
|
||||||
|
|
||||||
|
void buzz(int ms) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,69 @@
|
||||||
|
#define BUTTON 2
|
||||||
|
#define BUZZER 8
|
||||||
|
#define WINNER 5
|
||||||
|
|
||||||
|
int delay_ms;
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
pinMode(BUTTON, INPUT);
|
||||||
|
pinMode(3, OUTPUT);
|
||||||
|
pinMode(4, OUTPUT);
|
||||||
|
pinMode(5, OUTPUT);
|
||||||
|
pinMode(6, OUTPUT);
|
||||||
|
pinMode(7, OUTPUT);
|
||||||
|
pinMode(BUZZER, OUTPUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
void buzz(int ms) {
|
||||||
|
digitalWrite(BUZZER, HIGH);
|
||||||
|
delay(ms);
|
||||||
|
digitalWrite(BUZZER, LOW);
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_all_leds(int state) {
|
||||||
|
for (int i = 3; i <= 7; i++) {
|
||||||
|
digitalWrite(i, state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void do_winner(void) {
|
||||||
|
set_all_leds(HIGH);
|
||||||
|
buzz(100);
|
||||||
|
delay(100);
|
||||||
|
buzz(100);
|
||||||
|
set_all_leds(LOW);
|
||||||
|
}
|
||||||
|
|
||||||
|
void do_loser(void) {
|
||||||
|
buzz(500);
|
||||||
|
}
|
||||||
|
|
||||||
|
void check_delay(int cur_led, int delay_ms) {
|
||||||
|
unsigned long start = millis();
|
||||||
|
while (millis() < start+delay_ms) {
|
||||||
|
if (digitalRead(BUTTON) == LOW) {
|
||||||
|
if (cur_led == WINNER) {
|
||||||
|
do_winner();
|
||||||
|
} else {
|
||||||
|
while (digitalRead(BUTTON) == LOW) {
|
||||||
|
do_loser();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
for (int i = 4; i <= 7; i++) {
|
||||||
|
delay_ms = analogRead(A0);
|
||||||
|
digitalWrite(i - 1, LOW);
|
||||||
|
digitalWrite(i, HIGH);
|
||||||
|
check_delay(i, delay_ms);
|
||||||
|
}
|
||||||
|
for (int i = 6; i >= 3; i--) {
|
||||||
|
delay_ms = analogRead(A0);
|
||||||
|
digitalWrite(i + 1, LOW);
|
||||||
|
digitalWrite(i, HIGH);
|
||||||
|
check_delay(i, delay_ms);
|
||||||
|
}
|
||||||
|
}
|