Download presentation
Presentation is loading. Please wait.
Published byKristin Allen Modified over 8 years ago
1
Session 6 John Wolf (WolfDenElectronics.com)
2
Objectives – We’ll Learn Code to Voice PIR sensors – motion detection Quadrature Encoders Audio to RGB Display Use of Interrupts
3
Code to Voice Emic2 board from Parallax Uses the “SoftwareSerial” library to establish a UART serial port No special library for the board Commands are specific codes placed on the Serial Monitor send line Voice speaks via audio out Just type your message into sketch as a string within quotes
4
Emic 2 Text-to-Speech Module Arduino Rx Arduino Tx
5
#include #define rxPin 10 // Serial input (connects to Emic 2 SOUT) #define txPin 9 // Serial output (connects to Emic 2 SIN) #define ledPin 13 // on-board LED // set up a new serial port SoftwareSerial emicSerial = SoftwareSerial(rxPin, txPin); void setup() { Serial.begin(9600); pinMode(ledPin, OUTPUT); pinMode(rxPin, INPUT); pinMode(txPin, OUTPUT); emicSerial.begin(9600); digitalWrite(ledPin, LOW); Serial.print(“:”); // signals us to start }
6
// Serial Monitor: Newline, 9600 Baud. Type S first, then message void loop() { if (Serial.available()) emicSerial.write(Serial.read()); if (emicSerial.available()) Serial.write(emicSerial.read()); }
7
PIR (passive infrared sensor) Detects motion via a special lens over IR sensor array Monitors a single pin for a HIGH to do something in your sketch We’ll turn on a speaker and play a tune!
8
Passive Infrared Sensor – motion detector
9
ULN2003 Darrington Pair Driver – 500ma with flyback diodes GND connection for all ULN2003
10
const int speakerPin = 9; // choose the pin for the speaker const int inputPin = 2; // choose the input pin for the PIR sensor int length = 15; // the number of notes char notes[] = "ccggaagffeeddc "; // a space represents a rest int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 }; int tempo = 200; void playTone(int tone, int duration) { for (long i = 0; i < duration * 1000L; i += tone * 2) { digitalWrite(speakerPin, HIGH); delayMicroseconds(tone); digitalWrite(speakerPin, LOW); delayMicroseconds(tone); } } // end of playTone
11
void playNote(char note, int duration) { char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' }; int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 }; // play the tone corresponding to the note name for (int i = 0; i < 8; i++) { if (names[i] == note) { playTone(tones[i], duration); } }// end of playNote void setup() { pinMode(speakerPin, OUTPUT); pinMode(inputPin, INPUT); //delay(40000); // Move away and let detector go off }
12
void loop() { int val = digitalRead(inputPin); if (val == HIGH) { for (int i = 0; i < length; i++) { if (notes[i] == ' '){ delay(beats[i] * tempo); // rest } else { playNote(notes[i], beats[i] * tempo); }// end if-else statement delay(tempo / 2); }// end for statement }// if test for input }// end loop If movement is detected, the output pin goes HIGH. The software plays the song.
13
Quadrature Encoders Looks like a potentiometer Issues two square wave signal 90 degrees apart in phase called A and B If A rises first, the knob has been turned one way If B rises first, the know was turned the other way You count the pulse of one or the other to count how far the knob was turned
14
Quadrature Encoder
15
Quadrature – meaning 90 degrees out of phase 0 90 180 270 360
16
Interrupts – real time control Hardware sends signal asynchronously Sketch is set to react by halting code, jumping to handler function, then jumping back to next instruction in normal flow of code attachInterrupt(0,handler,RISING); See pin-out diagram for interrupt pins “handler” is a func you name RISING is one of 5 signal sensing’s
17
Arduino Uno int0 on pin 2, int1 on pin 3 Handler, or Interrupt Service Routine (ISR) Void function with no return value Can’t use delay() statement inside it All variables used have to be typed “volatile” at the top of sketch LOW,HIGH,RISING,FALLING,CHANGE Nothing in the sketch tells you about interrupt accept the attachInterrupt() in the set up function. When it comes, the ISR just runs.
18
Precautions The signal that triggers and interrupt must be stable – no bounce or false hits or the interrupt will trigger multiple times Best to use Real Time Clock or millis() method for time delays or you could miss an interrupt. Delay() blocks the code while timing out.
19
volatile int encoder_a = 2; // interrupt on pin 2 volatile int encoder_b = 3; // interrupt on pin 3 volatile long encoder; void setup() { Serial.begin(115200); // encoder pin on interrupt 0 (pin 2) attachInterrupt(0, encoderPinChangeA, CHANGE); // encoder pin on interrupt 1 (pin 3) attachInterrupt(1, encoderPinChangeB, CHANGE); encoder = 0; } void loop() { // provide Processing with data and \n Serial.print(encoder); Serial.print('\n'); delay(10); // if you run too fast, Processing won’t see data }
20
//######## Interrupt Handlers ############# void encoderPinChangeA() { if (digitalRead(encoder_a)==digitalRead(encoder_b)) { encoder--; } else{ encoder++; } void encoderPinChangeB() { if (digitalRead(encoder_a) != digitalRead(encoder_b)) { encoder--; } else { encoder++; }
21
Use Processing for cool interface
22
import processing.serial.*; Serial myPort; int colla=0, omy=2, colla2=215, colla3=255, omy2=2, omy3=2; float cosx, siny; void setup(){ size(1080,700); myPort=new Serial(this,"COM17", 115200); myPort.bufferUntil('\n'); } void draw(){ } void serialEvent(Serial myPort){ background(0); String pulses_raw=myPort.readStringUntil('\n'); if(pulses_raw != null){ pulses_raw=trim(pulses_raw); float pulses = float(pulses_raw); pulses = pulses*3.141592654/48; siny=sin(pulses); cosx=cos(pulses); stroke(colla,colla2,colla3); strokeWeight(10); fill(colla,colla2,colla3); // first two: center last two: end of line line(width/2, height/2, width/2+width/2*cosx, height/2+height/2*siny);
23
colla=colla+omy; if(colla>254) omy=omy* -1; if(colla<1) omy=omy* -1; colla2=colla2 + omy2; if(colla2>254) omy2=omy2* -1; if(colla2<1) omy2=omy2* -1; colla3=colla3 + omy3; if(colla3>254) omy3=omy3* -1; if(colla3<1) omy3=omy3* -1; } A bunch of color changes on the line
24
Audio to RGB MSGEQ7 is a small chip that divides the audio spectrum into seven band and issues a DC level change for each band with the amplitude of the audio signal in that band My board skips the lowest band and groups two together for the remainder to have three outputs to FET transitors The FETs are PWM driven with the amplitude changes so an LED can be varied in brightness on each channel
26
#define MSGEQ7_STROBE_PIN 7 #define MSGEQ7_RESET_PIN 8 #define MSGEQ7_ANALOG_PIN A0 #define RED_LED 9 #define GREEN_LED 10 #define BLUE_LED 11 #define NUM_FREQUENCY_BANDS 7 // Global variables int volumeInBand[NUM_FREQUENCY_BANDS]; int minimumVolume, maximumVolume; void setup() { // Set the LED pins as outputs pinMode(RED_LED, OUTPUT); pinMode(GREEN_LED, OUTPUT); pinMode(BLUE_LED, OUTPUT); // Set up the MSGEQ7 IC pinMode(MSGEQ7_ANALOG_PIN, INPUT); pinMode(MSGEQ7_STROBE_PIN, OUTPUT); pinMode(MSGEQ7_RESET_PIN, OUTPUT); digitalWrite(MSGEQ7_RESET_PIN, LOW); digitalWrite(MSGEQ7_STROBE_PIN, HIGH); // Initialize the minimum and maximum volume levels minimumVolume = 1023; maximumVolume = 0; }
27
// This loop executes around 100 times per second void loop() { static unsigned long nextLoopTime = 50; int cutoffVolume; // Toggle the RESET pin of the MSGEQ7 to start reading from the lowest frequency band digitalWrite(MSGEQ7_RESET_PIN, HIGH); digitalWrite(MSGEQ7_RESET_PIN, LOW); // Read the volume in every frequency band from the MSGEQ7 for (int i=0; i<NUM_FREQUENCY_BANDS; i++) { digitalWrite(MSGEQ7_STROBE_PIN, LOW); delayMicroseconds(30); // Allow the output to settle volumeInBand[i] = analogRead(MSGEQ7_ANALOG_PIN); autoAdjustMinMax(volumeInBand[i]); digitalWrite(MSGEQ7_STROBE_PIN, HIGH); }
28
// A volume greater than 35% should turn the LED on cutoffVolume = map(35, 0, 100, minimumVolume, maximumVolume); // Turn the RED LED on if the volume in the lower frequency bands is high enough if (volumeInBand[0] > cutoffVolume || volumeInBand[1] > cutoffVolume) analogWrite(RED_LED, map(max(volumeInBand[0], volumeInBand[1]), cutoffVolume, maximumVolume, 0, 255)); else analogWrite(RED_LED, 0); // Turn the GREEN LED on if the volume in the middle frequency bands is high enough if (volumeInBand[2] > cutoffVolume || volumeInBand[3] > cutoffVolume) analogWrite(GREEN_LED, map(max(volumeInBand[2], volumeInBand[3]), cutoffVolume, maximumVolume, 0, 255)); else analogWrite(GREEN_LED, 0); // Turn the BLUE LED on if the volume in the lower frequency bands is high enough if (volumeInBand[4] > cutoffVolume || volumeInBand[5] > cutoffVolume) analogWrite(BLUE_LED, map(max(volumeInBand[4], volumeInBand[5]), cutoffVolume, maximumVolume, 0, 255)); else analogWrite(BLUE_LED, 0); // Execute this loop 20 times per second (every 50ms) if (millis() < nextLoopTime) delay(nextLoopTime - millis()); nextLoopTime += 50; }
29
// Automatically adjust the high and low volume. This routine is called 20 times per // second for each band, or 20 * 7 bands = 140 times per second. void autoAdjustMinMax(int volume) { static int increaseMin = 0; static int decreaseMax = 0; // Adjust the minimum volume level if (volume < minimumVolume) minimumVolume = volume; else { // Move the minimum volume level upwards (1 per second) if (++increaseMin > 140) { increaseMin = 0; minimumVolume++; } // Adjust the maximum volume level if (volume > maximumVolume) maximumVolume = volume; else { // Move the maximum volume level downwards (5 per second) if (++decreaseMax > 28) { decreaseMax = 0; // Keep a reasonable level of separation between max and min if (maximumVolume - minimumVolume > 100) maximumVolume--; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.