// code for ATtiny to read analog values from a mic and // translate these into light patterns // charlieplexing possible for more lights const int numReadings = 1; int readings[numReadings]; // the readings from the analog input int readIndex = 0; // the index of the current reading int total = 0; // the running total int average = 0; // the average const int numReadings1 = 2; int readings1[numReadings1]; // the readings from the analog input int readIndex1 = 0; // the index of the current reading int total1 = 0; // the running total int average1 = 0; // the average const int numReadings2 = 3; int readings2[numReadings2]; // the readings from the analog input int readIndex2 = 0; // the index of the current reading int total2 = 0; // the running total int average2 = 0; // the average int value; int value1; #define sensitivity 250 //231 void setup() { pinMode(0, OUTPUT); pinMode(1, OUTPUT); pinMode(2, OUTPUT); // initialize all the readings to 0: for (int thisReading = 0; thisReading < numReadings; thisReading++) { readings[thisReading] = 0; } for (int thisReading1 = 0; thisReading1 < numReadings1; thisReading1++) { readings1[thisReading1] = 0; } for (int thisReading2 = 0; thisReading2 < numReadings2; thisReading2++) { readings2[thisReading2] = 0; } } void loop() { value = analogRead(A2); // subtract the last reading: total = total - readings[readIndex]; // read from the sensor: readings[readIndex] = analogRead(A2); // add the reading to the total: total = total + readings[readIndex]; // advance to the next position in the array: readIndex = readIndex + 1; //--------------- total1 = total1 - readings1[readIndex1]; // read from the sensor: readings1[readIndex1] = analogRead(A2); // add the reading to the total: total1 = total1 + readings1[readIndex1]; // advance to the next position in the array: readIndex1 = readIndex1 + 1; //----------------- value1= analogRead(A3); total2 = total2 - readings2[readIndex2]; // read from the sensor: readings2[readIndex2] = analogRead(A3); // add the reading to the total: total2 = total2 + readings2[readIndex2]; // advance to the next position in the array: readIndex2 = readIndex2 + 1; //------------------------ // if we're at the end of the array... if (readIndex >= numReadings) { // ...wrap around to the beginning: readIndex = 0; } // if we're at the end of the array... if (readIndex1 >= numReadings1) { // ...wrap around to the beginning: readIndex1 = 0; } // if we're at the end of the array... if (readIndex2 >= numReadings2) { // ...wrap around to the beginning: readIndex2 = 0; } // calculate the average: average = total / numReadings; average1 = total1 / numReadings1; average2 = total2 / numReadings2; // send it to the computer as ASCII digits if((abs(value - average)) > 3*sensitivity) //3 digitalWrite(0, LOW); else digitalWrite(0, HIGH); if((abs(value - average2)) > 3*sensitivity) //2 digitalWrite(1, LOW); else digitalWrite(1, HIGH); if((abs(value - average1)) > 3*sensitivity)//4 digitalWrite(2, LOW); else digitalWrite(2, HIGH); }