Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM
2.0 – Welcome and Introduction 2.1 – Components used in this Project. 2.2 – Exploring a Switch and its effects in a digital system. 2.3 – De-bouncing a switch – Blink Without Delay 2.4 – Exploring the 4 main uses for a resistor – Current Limiting – Voltage Division – Pull Down – Pull Up Today’s Agenda
2.5 – Exploring Speakers and Piezo Buzzers – How a speaker works vs a Piezospeaker 2.6 – PWM outputs, what are they? A detailed look into Pulse width Modulation 2.7 – The RGB LED 2.8 – Exploring Libraries. 2.9 – Using Tabs, creating our own Lib 2.10 – Using integer Arrays 2.11 – Bringing it all together: Name That Tune 2.12 – Serial Feedback Shields Today’s Agenda (cont.)
TAC Switch x 4 Peizo Buzzer x 1 RGB Breakout x 1 Components used in this Project.
Circuit:
When a switch is pressed in a circuit it casues the line voltage to “bounce” Bouncing
Sample of a switch press
When timing is crucial or multiple things need to happen during the void loop(), the delay() command can become a big problem. Open example > Basic > Blink Blink
void setup() { pinMode(13,OUTPUT); pinMode(9,OUTPUT); pinMode(2,INPUT); } Modify the Blink Sketch setup
void loop() { digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(13, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second if(digitalRead(2) == LOW) digitalWrite(9,HIGH); else digitalWrite(9,LOW); } Modify the Blink Sketch loop
Open sketch examples > Digital > Blink without delay Blink Without delay()
Modify the sketch in the same way, what happens differently? Modify the sketch
Current Limiting Voltage Division Pull Down Pull Up Four Main uses for a Resistor
Current Limiting Resistor
Voltage Divider
Pull-UP
void setup() { pinMode(6,OUTPUT); } void loop() { if(digitalRead(2) == LOW) digitalWrite(6,HIGH); else digitalWrite(6,LOW); } Example of INPUT_PULLUP
The Piezo Buzzer
Pulse Width Modulation Is a DC voltage that is turned ON and OFF at different rates. The voltage has 2 states, 0 and Vcc PWM
A Low pass Filter will smooth a PWM signal back to a DC voltage. Low Pass Filter
Single Color Bi-Color Tri-Color RGB Types of LEDs
The RGB LED provided in your kit is mounted on a Breakout board. A breakout board provides all the nessesary supporting electronics so it can be used easily and quickly for proto typeing. The RGB LED with your Kit
void setup() { pinMode(6,OUTPUT); pinMode(9,OUTPUT); pinMode(10,OUTPUT); } void loop() { digitalWrite(9,HIGH); delay(300); digitalWrite(9,LOW); digitalWrite(10,HIGH); delay(300); digitalWrite(10,LOW); digitalWrite(6,HIGH); delay(300); digitalWrite(6,LOW); } RGB LED test
Libraries are chunks of code writen for specific, and often repetitive task. # include or # include “Lib_Name” Libraries
Go to – Download the IRremote.zip file Download and install Libraries.
Method 1 Automatic – Go to Sketch > Input Library > Add Library. Navigate to the file just downloaded and select it. Method 2 Manually extract the Library and copy the folder to the Lib folder within the Arduino sketch folder. Two ways to install a Library
// Our Lib of Pitches #define NOTE_G1 49 #define NOTE_B2 123 #define NOTE_F3 175 #define NOTE_G4 392 Create a Library
An array is a collection of variables that are accessed with an index number. Arrays are Zero indexed. Arrays
// Call my Lib #include "pitches.h" // Define my Array int BP[] = {NOTE_G4, NOTE_B2, NOTE_F3, NOTE_G1}; Bringing it all together
// Declare my variabls int INDEX = 2; // Start button int redLEDPin = 9; int greenLEDPin = 10; int blueLEDPin = 6; Bringing it all together
// Set my pin modes void setup() { pinMode(2,INPUT_PULLUP); pinMode(3,INPUT_PULLUP); pinMode(4,INPUT_PULLUP); pinMode(5,INPUT_PULLUP); pinMode(13,OUTPUT); pinMode(redLEDPin,OUTPUT); pinMode(greenLEDPin,OUTPUT); pinMode(blueLEDPin,OUTPUT); } Bringing it all together
// My loop void loop() { if(digitalRead(INDEX) == LOW) { tone(8,BP[INDEX-2]); RGBWrite(random(100),random(100),random(100)) ; while(digitalRead(INDEX) == LOW) { } RGBWrite(0,0,0); noTone(8); } INDEX=INDEX+1; if(INDEX > 5) INDEX = 2; } Bringing it all together
Press keys in this order 1 – 2 – 3 – 4 – 2 Name that tune
There is 1 UART (universal asynchronous receiver/transmitter) on the Arduino Uno The Serial port uses Pin 0 and 1 Pin 0 = RX or Data Receive Pin 1 = TX or Data Transmit The USB connection uses the same UART Serial Feedback
int R = random(100); int G = random(100); int B = random(100); if(digitalRead(INDEX) == LOW) { Serial.print(INDEX); Serial.print("|"); Serial.print(R); Serial.print("|"); Serial.print(G); Serial.print("|"); Serial.println(B); // Serial.println() adds a line feed to the end tone(8,BP[INDEX-2]); RGBWrite(R,G,B) ; Add to the script
A Shield is a supportive board that plugs into the Arduino to extend the Arduino’s ability and reduces the amount of bread boarding needed. There’s a Shield for that…
Shields
Challenge #1 – Use a switch-case and make specific colors light up based on the tone being played. Challenge #2 – Team up and use 8 buttons to make a 8 Key Keyboard Challenge #3 Use the Ir Lib to play sounds from your Remote. Challenge