Fabwear (6 / 7 steg)

Steg 6: Programmet Contoller



Använd den under kod programmera din controller. Se till att ställa in rätt antal mikrofoner och ändra stiften till beroende på vilket som du brukade göra anslutningar.

CONST int numReadings = 10;
CONST int numberOfMics = 3;
CONST int omgivande = 600;

heltal index = 0;
int maxMic = 0;
booleska soundDetected = false;

int soundSensorPins [numberOfMics] = {A0, A1, A2};
int soundLEDPins [numberOfMics] = {3, 5, 7};

int soundArray [numberOfMics] [numReadings];
int micTotals [numberOfMics];
booleska lightState [numberOfMics];

void setup() {
Serial.BEGIN(9600);
ställa in LED stift till utgång
för (int thisLED = 0; thisLED < numberOfMics; thisLED ++) {
pinMode (soundLEDPins [thisLED], produktionen);
}
init soundArray
för (int thisReading = 0; thisReading < numReadings; thisReading ++) {
för (int thisMic = 0; thisMic < numberOfMics; thisMic ++) {
soundArray [thisMic] [thisReading] = 0;
}
}
init micTotals
för (int thisTotal = 0; thisTotal < numberOfMics; thisTotal ++) {
micTotals [thisTotal] = 0;
}
init lightState
för (int thisState = 0; thisState < numberOfMics; thisState ++) {
lightState [thisState] = false;
}
}

void loop() {
smidig läsning
för (int thisMic = 0; thisMic < numberOfMics; thisMic ++) {
ta bort sista värdet
micTotals [thisMic] = micTotals [thisMic] - soundArray [thisMic] [index];
Läs nya värdet
soundArray [thisMic] [index] = analogRead(soundSensorPins[thisMic]);
få nya totalt
micTotals [thisMic] = micTotals [thisMic] + soundArray [thisMic] [index];
}

Leta efter en mikrofon över tröskeln
soundDetected = false;
för (int thisMic = 0; thisMic < numberOfMics; thisMic ++) {
om (micTotals [thisMic] / numReadings > omgivande) {
soundDetected = sant;
Serial.println ("ljud upptäcks");
}
}
jämföra mikrofoner
om (soundDetected == true) {
maxMic = 0;
för (int thisMic = 0; thisMic < numberOfMics; thisMic ++) {
om (micTotals [thisMic] > micTotals[maxMic]) {
maxMic = thisMic;
Serial.println(maxMic);
} annat {
Serial.println(maxMic);
}
}
för (int thisMic = 0; thisMic < numberOfMics; thisMic ++) {
om (thisMic == maxMic) {
lightState [thisMic] = true;
Serial.println(thisMic);
} annat {
lightState [thisMic] = false;
}
}
} annat {
för (int thisMic = 0; thisMic < numberOfMics; thisMic ++) {
lightState [thisMic] = false;
}
}
avancera till nästa position i matrisen
index = index + 1.
återställa array
om (index > = numReadings) {
index = 0;
}
ställa in lysdioder
för (int thisLight = 0; thisLight < numberOfMics; thisLight ++) {
om (lightState [thisLight] == true) {
digitalWrite (soundLEDPins [thisLight], hög);
} annat {
digitalWrite (soundLEDPins [thisLight], låg);
}
}
Delay(1);
}

Se Steg
Relaterade Ämnen