Paul, Rebecca, and I teamed up once again to work on a project, deemed Project Audio. This was a smaller scale project for practice in preparation for the big final project. For this one, we needed to incorporate a speaker and a microphone. I was gone for a lot of the week in which this project started, so Paul and Rebecca came up with the idea to make something that detects temperature, and when you speak to it, it will talk back and light up either green (room temperature), red (warm), or blue (cold).


Paul and Rebecca worked on the project for most of the time without me; I came in handy moreso at the end. They put together the board and wired everything up. When I came in, they were using a capacitor instead of a thermistor (some of them do look very much alike). We fixed this issue, but we were struggling to get the temperature reading just right. We tried changing resistors, thermistors, code…but with no difference in results. Finally, we went to the professor for help. She showed us a diagram of how to wire a thermistor–turns out, we were wiring it wrong. After fixing this, the thermistor was working splendidly.
Now we just needed to fix the microphone. Paul and Rebecca had tested some mic code earlier, so we altered that for the rest of the project. The mic was tricky about detecting sound. We never did get it to work perfectly, but it works well enough. After adjusting the mic and the range for the temperature, our project was done. When we made noise or tapped the mic, the speaker would make a sound depending on the temperature; the higher the temperature, the higher the pitch. Originally we wanted the speaker to output something like a .wav with custom commentary, but we didn’t get that implemented in time. Regardless, it’ still cool.
Project Audio – Complete.
Link to video: https://vimeo.com/94467334



Rebecca: https://arduinohackers.wordpress.com/
Paul: http://paulsaidi.blogspot.com/
And here’s the code!
/* Lozano's code adapted by W. M. Harris 4.5.14
*Code for the electret microphone
*Based on Knock and Calibrate from Arduino examples
*by David Mellis,Tom Igoe and David Cuartielles
*
*
*More info:
*http://www.dtic.upf.edu/~jlozano/interfaces/microphone.html
*Jose Lozano
*
*
*/
#include "pitches.h"
// these constants won’t change:
const int microphonePin = 2; // the amplifier output is connected to analog pin 2
const int triColorBlueLed = 12;
const int triColorGreenLed = 10;
const int triColorRedLed = 8;
const int thermisterPin = A5;
const int speakerPin = 3;
const int hotTemperature = 60;
const int coldTemperature = 50;
// these variables will change:
int sensorReading = 0; // variable to store the value read from the sensor pin
int sensorMax = 0;
int sensorMin = 1023;
int threshold = 0;
int difference;
int melodyCold[] =
{
NOTE_G3
};
int melodyHot[] =
{
NOTE_G5
};
int melodyMeh[] =
{
NOTE_G4
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] =
{
//4, 8, 8, 4, 4, 4, 4, 4
1//4, 4, 4, 3, 4, 2, 4, 8, 8, 2, 8, 8, 2, 4, 8, 8, 4, 4, 4, 2
};
void setup() {
pinMode(triColorRedLed, OUTPUT);
pinMode(triColorGreenLed, OUTPUT);
pinMode(triColorBlueLed, OUTPUT);
Serial.begin(9600); // use the serial port
while (millis() < 3000) {
threshold = analogRead(microphonePin);
// record the maximum sensor value
if (threshold > sensorMax) {
sensorMax = threshold;
}
}
//playSoundEffect(1);
// signal the end of the calibration period
threshold = sensorMax;
}
void loop() {
int number = isThereNoise();
if(number == 1)
{
figureOutTheTemperature();
}
}
int isThereNoise()
{
// read the sensor and store it in the variable sensorReading:
sensorReading = analogRead(microphonePin);
sensorMax = max(sensorMax, sensorReading);
difference = 0;
// if the sensor reading is greater than the threshold:
if ((sensorReading >= threshold)) {
difference = sensorReading-threshold;
Serial.print(difference); //Will send only positive and absolute values of waveform
}
//make LED brightness proportional to range of sensor values
//Still send only positive vals
difference = max(0, map(sensorReading, threshold, sensorMax, 0, 255));
//Serial.print(” “);
Serial.println(difference);
if(difference > 0)
{
return 1;
}
else
{
return 0;
}
}
void figureOutTheTemperature()
{
float thermisterReading = readingFromThermister(analogRead(thermisterPin));
if(thermisterReading >= hotTemperature)
{
digitalWrite(triColorRedLed, HIGH);
//analogWrite(triColorRedLed, thermisterReading);
playSoundEffect(1);
delay(50);
digitalWrite(triColorRedLed, LOW);
}
else if(thermisterReading <= coldTemperature)
{
digitalWrite(triColorBlueLed, HIGH);
//analogWrite(triColorBlueLed, thermisterReading);
playSoundEffect(3);
delay(50);
digitalWrite(triColorBlueLed, LOW);
}
else
{
digitalWrite(triColorGreenLed, HIGH);
//analogWrite(triColorGreenLed, thermisterReading);
playSoundEffect(2);
delay(50);
digitalWrite(triColorGreenLed, LOW);
}
// Serial.println(analogRead(thermisterPin));
// Serial.println(thermisterReading);
//delay(4); // Better for Processing showing data
}
float readingFromThermister(int RawADC)
{
float Temp;
// See http://en.wikipedia.org/wiki/Thermistor for explanation of formula
Temp = log(((10240000/RawADC) – 10000));
Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
Temp = Temp – 238.00;//273.15; // Convert Kelvin to Celcius
Serial.print(“Celcius: “); Serial.print(Temp);
Temp *= 1.8;
Temp += 32;
Serial.print(” Fahrenheit: “); Serial.println( Temp);
return Temp;
}
void playSoundEffect(int temp)
{
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < /*20*/1; thisNote++)
{
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000/noteDurations[thisNote];
if (temp == 1) //hot
{
tone(speakerPin, melodyHot[thisNote],noteDuration);
}
else if (temp == 2) //meh
{
tone(speakerPin, melodyMeh[thisNote],noteDuration);
}
else if (temp == 3) //cold
{
tone(speakerPin, melodyCold[thisNote],noteDuration);
}
// to distinguish the notes, set a minimum time between them.
// the note’s duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(speakerPin);
}
}
/* Processing code
/**
* from http://www.dtic.upf.edu/~jlozano/interfaces/microphone.html
* code –http://www.dtic.upf.edu/~jlozano/interfaces/piezo_sopla/singme.pde
*
*Based on Tickle example.
*
* The word “tickle” jitters when serial data arrives from serial port
*
*
import processing.serial.*;
String portname = “COM1”;
Serial port;
PFont font;
int micro = 0;
float x = (500); // X-coordinate of text
float y = (350); // Y-coordinate of text
void setup()
{
size(1200, 700);
font = createFont(“Courier”, 24);
textFont(font);
noStroke();
// #1 in Serial.list() usually FTDI adaptor
println(Serial.list());
portname = Serial.list()[2];
port = new Serial(this, portname, 9600);
}
void draw()
{
fill(247,236,7);
rect(0, 0, width, height);
fill(0);
if (port.available() > 0) {
micro = port.read();
port.clear();
x = x + random(-12, 12) + random(-2,2)*micro/2 ;
y = y + random(-12, 12) + random(-2,2)*micro/2;
}
text(“Sing Me”, constrain(x,0,width-30), constrain(y,0,height));
if (x>width || y > height || y < 0 || x < 0 ){
x = (500); // X-coordinate of text
y = (350); // Y-coordinate of text
}
}
void mousePressed() {
x = (500); // X-coordinate of text
y = (350); // Y-coordinate of text
}
*/
Reblogged from Amanda Imperials website: http://amandaimperial.wordpress.com/2014/04/18/project-audio-1/