Session 2 Slides

Download Report

Transcript Session 2 Slides

Beginning
Arduino
Session 2
http://scn.sap.com/community/developer-center/hana/blog/2014/12/08/iot-with-sap-hana-dev
AN INTRODUCTION TO HACKING
THE WORLD’S MOST POPULAR
MICROCONTROLLER PLATFORM
Today’s Agenda
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
2.3.1 – Blink Without Delay
2.4 – Exploring the 4 main uses for a resistor
2.4.1 – Current Limiting
2.4.2 – Voltage Division
2.4.3 – Pull Down
2.4.4 – Pull Up
Today’s Agenda (cont.)
2.5 – Exploring Speakers and Piezo Buzzers
2.5.1 – How a speaker works vs a Piezo speaker
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
2.13 - Shields
Components used in this Project.
TAC Switch x 4
Peizo Buzzer x 1
RGB Breakout x 1
Circuit:
Bouncing
When a switch is pressed in a
circuit it casues the line
voltage to “bounce”
Sample of a switch press
Blink
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
Modify the Blink Sketch setup
void setup()
{
pinMode(13,OUTPUT);
pinMode(9,OUTPUT);
pinMode(2,INPUT);
}
Modify the Blink Sketch loop
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);
}
Blink Without delay()
Open sketch examples > Digital >
Blink without delay
Modify the sketch
Modify the sketch in the same way,
what happens differently?
Four Main uses for a Resistor
Current Limiting
Voltage Division
Pull Down
Pull Up
Current Limiting Resistor
Voltage Divider
Pull-UP
Example of INPUT_PULLUP
void setup()
{
pinMode(6,OUTPUT);
}
void loop()
{
if(digitalRead(2) == LOW)
digitalWrite(6,HIGH);
else
digitalWrite(6,LOW);
}
The Piezo Buzzer
PWM
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
Low Pass Filter
A Low pass Filter will smooth a PWM signal back to a DC voltage.
Types of LEDs
•
•
•
•
Single Color
Bi-Color
Tri-Color
RGB
The RGB LED with your Kit
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.
RGB LED test
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);
}
Libraries
Libraries are chunks of code writen for
specific, and often repetitive task.
# include <Lib_Name>
or
# include “Lib_Name”
Download and install Libraries.
Go to –
https://github.com/shirriff/Arduino-IRremote/releases
Download the IRremote.zip file
Two ways to install a Library
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.
Create a Library
// Our Lib of Pitches
#define NOTE_G1 49
#define NOTE_B2 123
#define NOTE_F3 175
#define NOTE_G4 392
Arrays
An array is a collection of variables that
are accessed with an index number.
Arrays are Zero indexed.
Bringing it all together
// 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;
}
Name that tune
Press keys in this order
1–2–3–4–2
Serial Feedback
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
Add to the script
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) ;
There’s a Shield for that…
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.
Shields
http://shieldlist.org/
https://learn.sparkfun.com/tutorials/arduino-shields
Challenge
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.