/* Sound Locator This sound locator has a pac-man like ghost that runs towards the loudest sound detected! This Sound Locator uses three microphones and an Arduino UNO to plot the direction of sound and also makes for a fun, interactive effect when using with music. Row-Column Scanning an 8x8 LED matrix with X-Y input Microphone lauyout below ________________________ |Mic3 (Top View) Mic1| | Mic2 | |_______________________| Version Control V21 - Final version for instructables */ // 2-dimensional array of row pin numbers: const int row[8] = { 1,2,3,4,5,6,7,8 }; // Equates to D0-D8 on Arduino UNO // 2-dimensional array of column pin numbers: const int col[8] = { 9,10,11,12,13,17,18,19 }; // Equates to D9-D13, A3-A5 on Arduino UNO // 2-dimensional array of pixels: int pixels[8][8]; // Mic iput variables int mic1 = 0; // Microphone input on A1 int mic2 = 0; // Microphone input on A0 int mic3 = 0; // Microphone input on A2 // Noise threshold before changing position int threshold = 50; // Brightness delay determines how many Milliseconds each LED is allowed to be lit int brightness = 15; // Delay between samples in terms of Milliseconds int sampledelay = 1; // Startup flag int startup = 0; // cursor position: int x = 4; int y = 4; void setup() { // initialize the I/O pins as outputs // iterate over the pins: for (int thisPin = 0; thisPin < 8; thisPin++) { // initialize the output pins: pinMode(col[thisPin], OUTPUT); pinMode(row[thisPin], OUTPUT); // take the col pins (i.e. the cathodes) high to ensure that // the LEDS are off: digitalWrite(col[thisPin], HIGH); } // initialize the pixel matrix: for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { pixels[x][y] = HIGH; } } } void loop() { // read input: readMics(); // draw the screen: refreshScreen(); delay(sampledelay); } void readMics() { // turn off the last position: pixels[x][y] = HIGH; // read the Mics Values mic1 = analogRead(A1); mic2 = analogRead(A0); mic3 = analogRead(A2); //Test is threshold hurdle met before proceeding if (mic1-mic2>threshold || mic2-mic1>threshold|| mic2-mic3>threshold || mic3-mic2>threshold || mic3-mic1>threshold || mic1-mic3>threshold) { // Sound Direction Algorithm // X-Axis Positive movement if (mic1>mic3) { x= x + 1; } // X-Axis negative movement if (mic3>mic2 ) { x= x - 1; } // Keep X Axis coordinates within boundaries of 8x8 grid display if (x <= 0) { x=0; } if (x >= 7) { x=7; } // Y-Axis Positive movement if (mic1>mic2 ) { y= y + 1; } // Y-Axis negative movement if (mic2>mic3 || mic2>mic3) { y= y - 1; } // Keep Y Axis coordinates within boundaries of 8x8 grid display if (y <= 0) { y=0; } if (y >= 7) { y=7; } } // set the new pixel position low so that the LED will turn on // in the next screen refresh: pixels[x][y] = LOW; } void refreshScreen() { // This routine writes the Array Data in Pixels[x],[y] to the Digital outputs. When the row is HIGH and the col is LOW the LED where they meet turns on: // iterate over the rows (anodes): for (int thisRow = 0; thisRow < 8; thisRow++) { // take the row pin (anode) high: digitalWrite(row[thisRow], HIGH); // iterate over the cols (cathodes): for (int thisCol = 0; thisCol < 8; thisCol++) { // get the state of the current pixel; int thisPixel = pixels[thisRow][thisCol]; // when the row is HIGH and the col is LOW, // the LED where they meet turns on: digitalWrite(col[thisCol], thisPixel); // turn the pixel off: if (thisPixel == LOW) { // Delay to make LEDs Brighter delay(brightness); // Delay to make LEDs Brighter digitalWrite(col[thisCol], HIGH); } } // take the row pin low to turn off the whole row: digitalWrite(row[thisRow], LOW); } }