Top Banner
Re-programming the Simon Says with Arduino Linz Craig, Brian Huang
31

Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.

Dec 17, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.

Re-programming the Simon Sayswith Arduino

Linz Craig, Brian Huang

Page 2: Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.
Page 3: Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.

Agenda

• About us / Introductions• Software Installation• What can it do? Who cares?• Blink Sketch Disco Lights• Using Variables• If() statement reading buttonPress• Analog Sensors Fading• Making Sound

Page 4: Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.

About Us

SparkFun Electronics is all about creation, innovation and sharing information. We want to get you excited about cutting edge electronics technology with our hands on educational kits.

Page 5: Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.

Arduino Board“Strong Friend” Created in Ivrea, Italy

in 2005 by Massimo Banzi & David Cuartielles

Open Source Hardware

Atmel Processor

Coding is accessible (C++, Processing, ModKit and MiniBloq)

Page 6: Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.

Arduino Software Installation

Open SourceFreeAvailable on-line with resources at:

www.arduino.cc

Page 7: Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.

What can it do?

•Great for prototyping ideas

•Access to multiple I/O

•Drive motors, turn on lights, trigger controls.

•Low Power requirements

•Flexible / Open-source

Page 8: Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.

Who cares?

Hackers / Makers

Engineers

Artists

Musicians

Kids!

Teachers!!

You!!!

Page 9: Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.

Setup Board TypeTools →  Board →  Arduino Uno

Page 10: Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.

Setup Serial COM PortTools →  Serial Port →  

Notes:

PC – Highest COM #Mac – /dev/tty.usbserial-A####xxx

Page 11: Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.

Analog and Digital

• All Arduino signals are either Analog or Digital

• All computers including Arduino, only understand Digital

• It is important to understand the difference between Analog and Digital signals since Analog signals require an Analog to Digital conversion

Page 12: Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.

Input vs. Output

Everything is referenced from the perspective of the microcontroller.

Inputs is a signal going into the board.

Output is any signal exiting an electrical system

•Almost all systems that use physical computing will have some form of output

•Often – Outputs include LEDs, a motor, a servo, a piezo element, a relay and an RGB LED

Page 13: Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.

Basic Program

Two required routines / methods / functions:

void setup()

{

// runs once

}

void loop()

{

// repeats forever!!!

}

upload

Page 14: Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.

Let’s get to hacking…

Project #1 – Blink“Hello World” of Physical Computing

Psuedo-code – how should this work?

Page 15: Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.

Three commands to know…

pinMode(pin, INPUT/OUTPUT);

ex: pinMode(13, OUTPUT);

digitalWrite(pin, HIGH/LOW);

ex: digitalWrite(13, HIGH);

delay(time_ms);

ex: delay(2500);

Page 16: Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.

LED1 = ~3;LED2 = ~5;LED3 = ~10;LED4 = 13;

Can you figure out which LED is tied to which pin? Write down a few notes in your notebook!

3

513

LED Pin Configurations

10

Page 17: Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.

Output is always Digital

To output a signal that pretends to be Analog use this code:

analogWrite (pinNumber, value );

Where pin is one of the analog output pins: 3, 5, 6, 9, 10, 11

Where value is a number ranging from: 0 – 255.

Page 18: Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.

Output is always Digital (ON or OFF)

Using a Digital signal that pretends to be an Analog signal is called Pulse Width Modulation (PWM)

By varying the duty cycle, we can “fake” an analog signal output.

PWM is available on Arduino pins # 3, 5, 6, 9, 10, and 11

P.W.M. Signal @ 25% P.W.M. Signal @ 75% P.W.M. Signal rising

SIMON_2b_BLINK

Page 19: Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.

Using VariablesTo clean-up code, for read-ability, and flexibility – we

can create placeholders in code.

Example:int ledPin = 3;

void setup(){

pinMode(ledPin, OUTPUT);

}

void loop(){

digitalWrite(ledPin, HIGH);

}

Page 20: Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.

Digital Input

int button_state = digitalRead(ButtonPin);

Value will be either: HIGH or LOW

Page 21: Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.

Reading a button press

Button Input is normally HIGH – when you press it, you pull it LOW.

The Code:

int buttonPress = digitalRead(2);

Page 22: Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.

Activating the Internal Pull-up Resistor

pinMode(pin, INPUT_PULLUP);

ex: pinMode(2, INPUT_PULLUP);

Notes:BUTTON1 = 2;BUTTON2 = 6;BUTTON3 = 9;BUTTON4 = 12;

Page 23: Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.

BUTTON1 = 2;BUTTON2 = 6;BUTTON3 = 9;BUTTON4 = 12;

Can you figure out which Button is tied to which pin? Write down a few notes in your notebook!

2

612

Button Pin Configurations

9

Page 24: Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.

Conditional StatementsIf…

General Use

if(condition)

{

// do this

}

Example

if(button_State==HIGH)

{

digitalWrite(ledPin, HIGH);

delay(300);

digitalWrite(ledPin, LOW);

delay(300);

}

Page 25: Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.

Digital Input• To connect digital input to your Arduino use Digital

Pins # 0 – 13 (Although pins # 0 & 1 are also used for serial)

• Digital Input needs a pinMode command:

pinMode ( pinNumber, INPUT );

Make sure to use caps for INPUT

• To get a digital reading: digitalRead ( pinNumber );

• Digital Input values are only HIGH (On) or LOW (Off)

Page 26: Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.

Last bit… the buzzerSIMON_3_BUZZER

Final command to know:

tone(pin, freq, duration);

pin – the OUTPUT pin the buzzer is connected to.

freq – unsigned int (0 … 65,535)

duration – unsigned long (0 … 2^32 - 1)

Page 27: Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.

Buzzer Pins

The Buzzer is connected between pins D4 and D7.

You must set both pins as OUTPUTs – pinMode(4, OUTPUT);

pinMode(7, OUTPUT);

Use tone(4, 440); to generate a 440 Hz sound.

Page 28: Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.

Musical Notes / Frequencies

Note  Frequency (Hz)C4 261

C#4/Db

4 277D4 293

D#4/Eb

4 311E4 329F4 349

F#4/Gb

4 369G4 392

G#4/Ab

4 415A4 440

A#4/Bb

4 466B4 493

Note  Frequency (Hz)C5 523

C#5/Db

5 554D5 587

D#5/Eb

5 622E5 659F5 698

F#5/Gb

5 739G5 783

G#5/Ab

5 830A5 880

A#5/Bb

5 932B5 987

Page 29: Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.

Simon DiscoMode

• Array variables• custom functions

• buzz(tone_id);• change_led();

Page 30: Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.

Questions?

Page 31: Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.

www.sparkfun.com6175 Longbow Drive, Suite 200

Boulder, Colorado 80301