Session 6 John Wolf (WolfDenElectronics.com). Objectives – We’ll Learn  Code to Voice  PIR sensors – motion detection  Quadrature Encoders  Audio.

Slides:



Advertisements
Similar presentations
EMS1EP Lecture 6 Digital Inputs
Advertisements

EMS1EP Lecture 4 Intro to Programming Dr. Robert Ross.
JED Microprocessors Pty Ltd Presenting the JED T430 low-cost Projector Controllers Nov 22nd, 2009.
Lab7: Introduction to Arduino
What is Arduino?  Arduino is a ATMEL 168 micro-controller kit designed specially for small projects  User friendly IDE(Integrated Development Environment)
How to use Arduino By: Andrew Hoffmaster.
Encoders, Motors, Power, Mini Project #1 10/24/2014.
Panasonic EVE-KC2F2024B 24 pulses per revolution 6mm diameter flattened output shaft output type: quadrature (incremental) minimum life: 15,000 rotations.
Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.
Digital & Analog Inputs. Review Fundamental parts of an Arduino program are … Setting output types using pinMode. Declaring variables Can write a digital.
Introduction to Arduino Programming January MER-421:Mechatronic System Design.
Intro to Programming and Microcontrollers. Activity Group into pairs and sit back-to-back. Pick one person who is going to draw. The other person will.
Beginning Arduino Session 2 AN INTRODUCTION TO HACKING THE WORLD’S MOST POPULAR MICROCONTROLLER PLATFORM
PING))) Ultrasonic Distance Sensor living with the lab ultrasonic pressure waves from PING))) speaker The PING))) sensor emits short bursts of sound and.
Embedded Programming and Robotics Lesson 2 C Programming Refresher C Programming1.
1 Ultrasonic Distance Sensor. 2 How it Works The distance sensor emits short bursts of sound and listens for this sound to echo off of nearby objects.
Basic Circuits – Lab 2 Arduino and Sensors Xmedia Spring 2011.
PROGRAMMING WITH ARDUINO. Arduino An open-source hardware platform based on an Atmel AVR 8-bit microcontroller and a C++ based IDE Over boards.
Arduino John Marcoux Christopher Lesch Thomas Dodge Unless otherwise noted, all information and pictures came from:
Tele-presence – Connecting Two Processing Platforms via Internet Controlling the Screen based on Distant User Input ServerClient 1.
Microcontroller Hands-on Workshop #3 Ahmad Manshad New Mexico State University Institute of Electrical and Electronics Engineers November 7, 2009.
ProtoSnap Introduction to Arduino Casey Haskell, Pete Lewis, David Stillman, Jim Lindblom, Pete Dokter, Lindsay Levkoff, Trevor Zylstra.
Arduino. What is it? A open-source software suite and single-board microcontroller. Allows easy and affordable prototyping of microcontroller applications.
Introduction to Sensor Technology Week Five Adam Taylor
Sparkfun Electronics ATtiny85 Arduino Quick Reference Sheet
LECTURE 2 – INPUTS THIS LECTURE WILL INTRODUCE HOW TO INPUT SIGNALS INTO AN ARDUINO.
Code The Arduino Environment.
Serial Communication. mouseX from computer to arduino processing sends a single byte of data arduino reads each byte arduino uses value to set light brightness.
ARDUINO 1. Basics  Comments  /* * Blink * * The basic Arduino example. Turns on an LED on for one second, * then off for one second, and so on... We.
Arduino Circuits and Code. int ledPin = 9; void setup() { pinMode(ledPin, OUTPUT); } void loop() { digitalWrite(ledPin, LOW); delay(1000); digitalWrite(ledPin,
1 - Remove LED from 13 and GND - Bring out your breadboard from HW#4 Arduino Overview:
INTERNET OF EVERYTHING SDU 2016 Week 4. Simple Digital and Analog Inputs  The Arduino’s ability to sense digital and analog inputs allows it to respond.
Embedded Programming and Robotics Lesson 11 Arduino Interrupts 1.
INTERNET OF EVERYTHING SDU 2016 Week 6. Getting Input from Sensors  Sensors give report on the world around it  Sensors convert physical input an electrical.
Embedded systems and sensors 1 Part 2 Interaction technology Lennart Herlaar.
Microcontroller basics Embedded systems for mortals.
Programming in Arduino Materials:Arduino Board Casperelectronics Pre Pres. Notes Photos from workshop?
INTERNET OF EVERYTHING SDU 2016 Week 12. Remotely Controlling Devices  interact with almost any device that uses some form of remote control  TVs, audio.
Arduino + Bluetooth TYWu. Connection Arduino + Bluetooth Module.
Electronic instrumentation Digitization of Analog Signal in TD
Pulse-Width Modulation: Simulating variable DC output
ME 120: Arduino PWM Breathing LED Code: Implementation on Arduino ME 120 Mechanical and Materials Engineering Portland State University
Microcontroller basics Embedded systems for mortals.
Pulse Width Modulation Instructor Dr Matthew Khi Yi Kyaw.
physical computing .2 – Day 3
Application Case Study Christmas Lights Controller
Lab 7 Basic 1: Game of Memory
Sparkfun Electronics ATtiny85 Arduino Quick Reference Sheet
Assist. Prof. Rassim Suliyev - SDU 2017
Microcontroller basics
Microcontroller basics
REMOTE JAMMING DEVICE.
Manual for Arduino Kit v1.0
Microcontroller basics
An Arduino Workshop A Microcontroller.
Get Your Project Started with Arduino
Arduino Part 1 Topics: Microcontrollers Programming Basics
Control the color and brightness of an RGB LED with a Potentiometer
Arduino Uno and sensors
Control a motors angular position with a flex sensor
Ultrasonic Distance Sensor
1 Code
Implementing Switches Using Interrupts
Interfacing a Rotary Encoder with an Arduino
Arduino : Introduction & Programming
Programming 2: The Arduino IDE & First Sketches
Sensors and actuators Sensors Resistive sensors
frclabviewtutorials.com/workshop
Interrupts.
Pulse-Width Modulation: Simulating variable DC output
Presentation transcript:

Session 6 John Wolf (WolfDenElectronics.com)

Objectives – We’ll Learn  Code to Voice  PIR sensors – motion detection  Quadrature Encoders  Audio to RGB Display  Use of Interrupts

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

Emic 2 Text-to-Speech Module Arduino Rx Arduino Tx

#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 }

// 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()); }

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!

Passive Infrared Sensor – motion detector

ULN2003 Darrington Pair Driver – 500ma with flyback diodes GND connection for all ULN2003

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

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 }

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.

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

Quadrature Encoder

Quadrature – meaning 90 degrees out of phase

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

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.

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.

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 }

//######## 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++; }

Use Processing for cool interface

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", ); 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* /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);

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

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

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

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

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

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