1
0
Fork 0

Adds Arduino Intro - Whack-a-mole Class

This commit is contained in:
Shawn Nock 2016-07-08 20:47:25 -04:00
parent f999df9d55
commit ea9bb8099a
16 changed files with 449 additions and 147 deletions

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 340 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 38 KiB

After

Width:  |  Height:  |  Size: 340 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 174 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 211 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 836 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 738 KiB

View File

@ -7,7 +7,16 @@ lang: en-CA
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
@ -55,17 +64,39 @@ Hacker, Church of the Weird Machine, Odd Duck
\end{center}
\end{columns}
## Raphael: The Vital Info
## Raphael: Day Job
- Manufacturing Engineer
- Instructor at Fanshawe
- Maker
Mechanical Engineer
- 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?
## Kit Contents
- Arduino Uno R3 Clone
- Arduino Uno R3
- Solderless Breadboard
- Connecting wires
- 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
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*
@ -99,27 +122,48 @@ 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
<!-- ## Installation -->
\Large Get installing
<!-- \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 ($-$)
Current is the flow of charge through a circuit. Measured in Amperes
(\si{\ampere}).
## Voltage / Potential / Resistance
## Resistance / Impedance
Voltage is how fast the current can move in the circuit. River
metaphor:
Circuits have a resistance to current flow that depends on the parts
in the circuit.
- 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).
Measured in Ohms (\si{\ohm})
## Voltage
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
@ -159,8 +203,7 @@ call them resistance(s).
\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 Needed for LEDs: $\approx\SI{1000}{\ohm}$\\
\item Button Pull-up/down: $\ge\SI{10}{\kilo\ohm}$
\item Color coded, Google it
\end{itemize}
@ -170,32 +213,6 @@ call them resistance(s).
\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
@ -331,19 +348,21 @@ will not flow and the LED is off.
## Your first Program
~~~ C
#define LED 13
/* the setup function runs once on reset / power */
void setup() {
/* set pin 13 as an output */
pinMode(13, OUTPUT);
pinMode(LED, OUTPUT);
}
/* the loop function repeats forever */
/* loop() repeats until reset or power off */
void loop() {
digitalWrite(13, HIGH); // turn on LED
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the off LED
digitalWrite(LED, HIGH); // turn on LED
delay(1000); // wait for a second
digitalWrite(LED, LOW); // turn the off LED
delay(1000);
}
~~~
# 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)
<!-- ## 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
## Buzzer & Button: Hardware
\begin{center}
\includegraphics[width=0.98\textwidth]{images/bb+switch.png}
\includegraphics[width=0.98\textwidth]{images/buzzer-breadboard.png}
\end{center}
## Push Button: Hardware, Pt. 2
## Push Button: Zoom
\begin{center}
\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
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
#define BUTTON 2
#define LED 13
#define BUZZER 8
int button_state = 0;
int button_state;
void setup() {
pinMode(BUTTON, INPUT);
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW); /* Start w/ LED off */
pinMode(BUZZER, OUTPUT);
digitalWrite(BUZZER, LOW); /* Start w/ LED off */
}
~~~
## Programming Note: Variables
@ -430,7 +432,7 @@ void setup() {
Declare a variable:
~~~ C
int delay_ms = 1000;
int button_state = HIGH;
~~~
`<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
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
@ -469,16 +462,56 @@ In C-like languages, the `==` operator checks if two things
- It returns `1` if the items are equal, *or*
- 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)
~~~ C
void buzz(int ms) {
digitalWrite(BUZZER, HIGH);
delay(ms);
digitalWrite(BUZZER, LOW);
}
void loop() {
button_state = digitalRead(BUTTON);
if (button_state == HIGH) {
digitalWrite(LED, LOW);
} else {
digitalWrite(LED, HIGH);
if (button_state == LOW) {
buzz(100);
}
}
~~~
@ -514,21 +547,24 @@ 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.
`analogRead(`*pin*`)` returns the voltage at the pin (0--1023), it can
be used directly or via variable.
~~~ C
#define LED 13
void setup() {
pinMode(LED, OUTPUT);
}
void loop() {
delay_ms = analogRead(A0);
digitalWrite(LED, HIGH);
delay(delay_ms);
delay(analogRead(A0));
digitalWrite(LED, LOW);
delay(analogRead(A0));
}
~~~
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.
Since the delay() calls use the result of analogRead (0-1023), the
blink rate changes with knob position.
# Shall we play a game?
@ -538,15 +574,21 @@ knob position.
\includegraphics[width=0.65\textwidth]{images/bb+switch+pot+leds.png}
\end{center}
## LEDs, the first one
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.
\begin{center}
\includegraphics[width=0.55\textwidth]{images/one-led.png}
\end{center}
## Programming Note: `for` Loop
~~~ C
for ( initializer ; condition; increment ) {
// This body will repeat until condition != 0
// This body will repeat while condition != 0
}
~~~
@ -555,80 +597,117 @@ initializer
variable.
condition
: Loop will repeat until condition $\neq0$
: Loop will repeat while condition is true ($\neq0$)
increment
: Runs *after* each loop. Often used to increment variables.
\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
loop () {
for (int i = 4; i <= 7; i++) {
delay_ms = analogRead(A0);
analogWrite(i - 1, LOW);
analogWrite(i, HIGH);
digitalWrite(i - 1, LOW);
digitalWrite(i, HIGH);
delay(delay_ms);
}
for (int i = 6; i >= 3; i--) {
delay_ms = analogRead(A0);
analogWrite(i + 1, LOW);
analogWrite(i, HIGH);
digitalWrite(i + 1, LOW);
digitalWrite(i, HIGH);
delay(delay_ms);
}
}
~~~
## Programming Note: Functions
## Programming Note: `while` Loop
~~~ C
void my_function(int arg1, ...) {
// Do fun things
while ( statement ) {
// This body will repeat while condition is true
// True means statement != 0
}
~~~
void:
: Return type. Void means nothing returned. Can be any type.
initializer
: Executed once at beginning of loop. Often used to declare a local
variable.
my_function:
: A name for your function
condition
: Loop will repeat while condition is true ($\neq0$)
arguments:
: A type and name for any parameters you want to use in your function
from the outside.
increment
: Runs *after* each loop. Often used to increment variables.
Define a function once, you can use it again and again. Beats
copy/pasting.
\center{*All fields are optional*}
## Winner, Winner, Chicken Dinner
~~~ C
#define WINNER 5
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();
unsigned long 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
}
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++) {
digitalWrite(i, state);
}
}
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);
}
~~~
## Putting it Together
~~~ C
loop () {
for (int i = 4; i <= 7; i++) {
delay_ms = analogRead(A0);
analogWrite(i - 1, LOW);
analogWrite(i, HIGH);
digitalWrite(i - 1, LOW);
digitalWrite(i, HIGH);
check_delay(i, delay_ms);
}
for (int i = 6; i >= 3; i--) {
@ -638,8 +717,89 @@ loop () {
}
~~~
## The End?
# The End?
\begin{center}
\LARGE{Let's build some cool stuff!}
\end{center}
# Extra Credit
## 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{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.

View File

@ -10,4 +10,4 @@ Giant Trackball Katamari Damacy
Sphero Mini-Golf
Mind Controlled Robot Painting
### 13
Yarn Grid + Projector = Three Dimentional moving lights.
Yarn Grid + Projector = Three Dimentional moving lights.

View File

@ -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);
}

22
sketchbook/buzz/buzz.ino Normal file
View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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;
}

View File

@ -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);
}
}