We set ourselves goals to reach, so at each stage we would have something we could hand in, before we moved on to add the next part in. And today, finally, we reached our first goal, to read the sound data, and visualize it in a way to give instant feedback to the wearer.
The problem is, our sound sensor is still being a bit dodgy, so I’ve had to rig the code a little bit for testing purposes, but I still have the true science bit in (commented out) in case anybody asks!
Here’s the code:
int sensorPin = A0;
int ledPin[] = {3,4,5};
//int warningLevels[] = {200, 230, 270};
int warningLevels[] = {20, 50, 100};
int sensorValue = 0;
int accumSound = 0;
int readCount = 0;
int aveSound = 0;
int ledLit = 0;
void setup()
{
Serial.begin(9600);
//set up LEDs
for (int i = 0; i<(sizeof(ledPin)/2); i++){
pinMode(ledPin[i], OUTPUT);
}
}
void loop()
{
//turn the lights off
for (int i = 0; i<(sizeof(ledPin)/2); i++){
digitalWrite(ledPin[i], LOW);
}
ledLit = 0;
//read sensor
sensorValue = analogRead(sensorPin);
//remove errors
while(sensorValue == 0 || sensorValue > 350){
Serial.println("reread...");
sensorValue = analogRead(sensorPin);
}
//trace
Serial.print("sensorValue: ");
Serial.println(sensorValue);
//add to accumulative sound
accumSound += sensorValue;
//trace
Serial.print("Accumulative Sound: ");
Serial.println(accumSound);
//add to readCount
readCount++;
//work out average sound per minute
aveSound = accumSound/readCount;
//trace
Serial.print("Average Sound per min: ");
Serial.println(aveSound);
//check against our set levels
for (int i=0; i<(sizeof(warningLevels)/2); i++){
if (aveSound > warningLevels[i]){
ledLit = i+1;
Serial.print("Warning level breached: ");
Serial.println(i);
}
}
///light correct amount of LEDs
for (int i=0; i<ledLit; i++){
digitalWrite(ledPin[i], HIGH);
}
Serial.println("------------------------------------------");
//wait for 1 minute
delay(30000);
}
I’ve done my best to comment it well, but incase you can’t work out what it does, basically it reads the sound every 30 secs, adds this to a accumulative score, works out the average sound you’ve been subjected to per minute from that, and lights the LEDs if you’ve had over the recommended limit per minute.
I’ll let Karla explain the science behind the recommended limits she came up with, but they’re basically 80,85 and 90 decibels, which we think should correspond to 200, 230, 270 from our sensor. However, these values are never hit, even if the sound sensor on Karla’s phone says we’re above those decibels, so I’ve added random levels in for testing.
I’ve currently been running it, with the wifly board in but not working, on battery for seven minutes and it’s still going strong.
Next up, we have a break for a dissertation seminar, then we’ll get the wifly working. We have a vague idea of sending the data to twitter, and pulling the feed from it to visualize it, but we’ll see how we go. At least now we have something to show if all else fails.