Achievement reached!

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()
  //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){
   sensorValue = analogRead(sensorPin); 
  Serial.print("sensorValue: ");

  //add to accumulative sound
  accumSound += sensorValue;
  Serial.print("Accumulative Sound: ");

  //add to readCount

  //work out average sound per minute
  aveSound = accumSound/readCount;
  Serial.print("Average Sound per min: ");

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

  ///light correct amount of LEDs
  for (int i=0; i<ledLit; i++){
   digitalWrite(ledPin[i], HIGH); 


  //wait for 1 minute

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.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s