As computing becomes more ubiquitous in our objects, designers need to be more aware of how to design meaningful interactions into electronically enhanced objects. At the University of Washington, a class of junior Interaction Design majors is exploring this question. These pages chronicle their efforts.

Tuesday, May 28, 2013

Randy & Charlie: It's All Coming Together

Wiring all the microphones together to later put into a wearable device. Code works pretty well.


/*
  New code for the microphones. 
  Uses four arrays to keep running averages for each microphone for the last ten seconds
  and sets the average of the ten as the threshold.
  Sound below the threshold will not register in the vibration motors.
*/

// The analogue input pins each microphone is plugged into.
const int electret1 = 0;  
const int electret2 = 1;
const int electret3 = 2;
const int electret4 = 3;

// The analogue output pins each vibration motor.
const int vibration1 = 11;  
const int vibration2 = 10;
const int vibration3 = 9;
const int vibration4 = 6;

// Variable to store the value read from the sensor pins.
int sensorReading1 = 0;     
int sensorReading2 = 0;
int sensorReading3 = 0;
int sensorReading4 = 0;

// The queue of running averages for each microphone to set threshold.
int sensorMax1[10];
int sensorMax2[10];
int sensorMax3[10];
int sensorMax4[10];

// Queue to temporarily store data.
int temp[10];

// The minimum.
int sensorMin = 1023;

// The threshold that needs to be surpassed to register.
int threshold1;
int threshold2;
int threshold3;
int threshold4;

// Keep track of time to take samples every instance.
int time;

// SETUP
void setup() {
  
  // Use the serial port 57600 to read faster than 9600.
  Serial.begin(57600);  

  // Set the vibration motors as output.
  pinMode(vibration1, OUTPUT);
  pinMode(vibration2, OUTPUT);
  pinMode(vibration3, OUTPUT);
  pinMode(vibration4, OUTPUT);
  
  delay(2000);
  
  // Record time it took to get here.
  time = millis();
  
  Serial.print("Time: ");
  Serial.println(time);
  // For ten seconds...
  for (int i = 0; i < 10; i++) {
    // Every 5 seconds, add a microphone reading to each microphone reading queue.
    Serial.print("Passed: ");
    Serial.println(millis() - time);
    
    sensorReading1 = analogRead(electret1);   
    push(sensorMax1, sensorReading1);
    
    sensorReading2 = analogRead(electret2); 
    push(sensorMax2, sensorReading2);
    
    sensorReading3 = analogRead(electret3); 
    push(sensorMax3, sensorReading3);;
    
    sensorReading4 = analogRead(electret4); 
    push(sensorMax4, sensorReading4);
    delay(1000);
  }
  
  Serial.print("Setup sensor1:");
  Serial.println(sizeof(sensorMax1));
  
  Serial.print("Setup sensor2:");
  Serial.println(sizeof(sensorMax2));
  
  Serial.print("Setup sensor3:");
  Serial.println(sizeof(sensorMax3));
  
  Serial.print("Setup sensor4:");
  Serial.println(sizeof(sensorMax4));
  
  // Set the threshold for each microphone as the average of the queue.
  Serial.println("Lets begin.");
  averageMicrophones();
}


// THE LOOP
void loop() {
  // For stability, start out with low on all vibration motors.
  digitalWrite(vibration1, LOW);
  digitalWrite(vibration2, LOW);
  digitalWrite(vibration3, LOW);
  digitalWrite(vibration4, LOW);
  
  // Read the sensor and store it in a variable.
  sensorReading1 = analogRead(electret1);
  sensorReading2 = analogRead(electret2);
  sensorReading3 = analogRead(electret3);
  sensorReading4 = analogRead(electret4); 

  // if the sensor reading is greater than the threshold:
  if (sensorReading1 >= threshold1) {
    Serial.print("Mic1: ");
    Serial.print(sensorReading1-threshold1);
    Serial.print("    ");
    if (sensorReading1-threshold1 > 10) {
        digitalWrite(vibration1, HIGH);
    }
  } else {
    Serial.print("Mic1: 0    ");
  }
  
  if (sensorReading2 >= threshold2) {
    Serial.print("Mic2: ");
    Serial.print(sensorReading2-threshold2);
    Serial.print("    ");
    if (sensorReading2-threshold2 > 10) {
        digitalWrite(vibration2, HIGH);
    }
  } else {
    Serial.print("Mic2: 0    ");
  }
  
  if (sensorReading3 >= threshold3) {
    Serial.print("Mic3: ");
    Serial.print(sensorReading3-threshold3);
    Serial.print("    ");
    if (sensorReading3-threshold3 > 10) {
        digitalWrite(vibration3, HIGH);
    }
  } else {
    Serial.print("Mic3: 0    ");
  }
  
  if (sensorReading4 >= threshold4) {
    Serial.print("Mic4: ");
    Serial.print(sensorReading4-threshold4);
    Serial.print("    ");
    Serial.print("\n");  
    if (sensorReading4-threshold4 > 10) {
        digitalWrite(vibration4, HIGH);
    }
  } else {
    Serial.print("Mic4: 0    ");
    Serial.print("\n");  
  }

  Serial.print("Mic1 Average: ");
  Serial.print(threshold1);
  Serial.print("   ");
  Serial.print("Mic2 Average: ");
  Serial.print(threshold2);
  Serial.print("   ");
  Serial.print("Mic3 Average: ");
  Serial.print(threshold3);
  Serial.print("   ");
  Serial.print("Mic4 Average: ");
  Serial.println(threshold4);
  Serial.print("\n");
  
  // Every second, take a reading and use it to adjust the average.
  if (millis() % 1000 == 0) {
    // Put in new data.
    push(sensorMax1, sensorReading1);
    push(sensorMax2, sensorReading2);
    push(sensorMax3, sensorReading3);
    push(sensorMax4, sensorReading4);
    // Average the data.
    averageMicrophones();
  }
  
  delay(2);  // Better for Processing showing data
}


// Averages all the microphone readings.
void averageMicrophones() {
  
  // Average the recordings of micrphone 1.
  int sum = 0;
  for (int i = 0; i < 10; i++) {
     sum = sum + sensorMax1[i];
  }
  threshold1 = sum / 10;
  
  // Average the recordings of micrphone 2.
  sum = 0;
  for (int i = 0; i < 10; i++) {
     sum = sum + sensorMax2[i];
  }
  threshold2 = sum / 10;
  
  // Average the recordings of micrphone 1.
  sum = 0;
  for (int i = 0; i < 10; i++) {
     sum = sum + sensorMax3[i];
  }
  threshold3 = sum / 10;
  
  // Average the recordings of micrphone 1.
  sum = 0;
  for (int i = 0; i < 10; i++) {
     sum = sum + sensorMax4[i];
  }
  threshold4 = sum / 10;
}

void push(int data[], int sensorReading) {
  // Shift all items one slot.
  for (int i = 9; i > 0; i--) {
    data[i] = data[i-1];
  }
  // Add new data to the beginning.
  data[0] = sensorReading;
}

No comments:

Post a Comment