Étape 2: Arduino Code
Voici le code. L’interruption définit un indicateur (pbPress) et le compteur (menuState) entre 0 - 4. À l’aide tandis que (pbPress == 0) {} ; Je reste dans l’un des éléments de menu suivants ; Au loin, Audio, Flash rapide, lent, Flash et de suite. Chaque fois que vous appuyez sur l’interrupteur momentané une interruption est déclenchée, le menu est avancé d’un pas et l’indicateur pbPress est défini sur 1 Quelles pauses la routine de l’existant en boucle et redémarre l’instruction switch avançant vers l’affaire suivante avec le tout prochain en boucle. Lorsque le menu est sur l’Audio, le microcontrôleur échantillonne le signal audio sur la broche A0 et calcule si le signal est supérieur à une des trois valeurs contenues dans le tableau de coupure []. Ces valeurs ont été déterminées empiriquement par essais et erreurs, avec différents niveaux de bruit ambiant. Le point de coupure [] qui est utilisée dépend de la position du commutateur S2_AUDIO_LEVEL correspondant à 0 - basse, 1 haut - MED et 2. Confusion, la dépression correspond à haute sensibilité (sons graves le fil el sur le déclencheur) et ainsi de suite.
int triacGatePin = 5; // drive el inverter thru optoisolator controlling triacint monitorPin = 0; //from microphone //for audio processing int digInputA = 3; //pins to check for audio switch position int digInputB = 4; //pins to check for audio switch position int cutOff[] = {70, 110, 260}; //value to compare peaktopeak with const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz) unsigned int sample; unsigned int signalMax, peakToPeak = 0; unsigned int signalMin = 1024; int menuState = 0; // variable to be updated by the interrupt int pbPress = 0; int triacState = LOW; long previousMillis = 0; long flashInterval = 0; //variables to keep track of the timing of recent interrupts volatile unsigned long button_time = 0; volatile unsigned long last_button_time = 0; void setup() { pinMode(triacGatePin, OUTPUT); pinMode(digInputA, INPUT); pinMode(digInputB, INPUT); //enable interrupt 0 (pin 2) which is connected to a button //jump to the increment function on falling edge attachInterrupt(0, increment, FALLING); //turn on interrupt for pin 2 } void loop() { if (pbPress == 1){ pbPress = 0; switch (menuState){ case 0: //off while(pbPress == 0){ //wait for next pbPress delay(10); } break; case 1: //audio while(pbPress == 0){ int audioLevel = getSwitchState(); // collect data for 50 mS unsigned long startMillis= millis(); // Start of sample window while (millis() - startMillis < sampleWindow) { sample = analogRead(monitorPin); if (sample < 1024) // toss out spurious readings { if (sample > signalMax) { signalMax = sample; // save just the max levels } else if (sample < signalMin) { signalMin = sample; // save just the min levels } } } peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude if (peakToPeak > cutOff[audioLevel]){ //turn on led digitalWrite(triacGatePin, HIGH); }else{ digitalWrite(triacGatePin, LOW); } resetValues(); } break; case 2: //switch fast bink flashInterval = 120; while(pbPress == 0){ unsigned long currentMillis = millis(); if(currentMillis - previousMillis > flashInterval) { // save the last time you blinked the LED previousMillis = currentMillis; // if the LED is off turn it on and vice-versa: if (triacState == LOW){ triacState = HIGH; }else{ triacState = LOW; } // set the LED with the triacState of the variable: digitalWrite(triacGatePin, triacState); } } break; case 3: //switch slow blink flashInterval = 700; while(pbPress == 0){ unsigned long currentMillis = millis(); if(currentMillis - previousMillis > flashInterval) { // save the last time you blinked the LED previousMillis = currentMillis; // if the LED is off turn it on and vice-versa: if (triacState == LOW){ triacState = HIGH; }else{ triacState = LOW; } // set the LED with the triacState of the variable: digitalWrite(triacGatePin, triacState); } } break; case 4: //switch on triacState = HIGH; digitalWrite(triacGatePin, triacState); while(pbPress == 0){ //wait for pbPress delay(10); } triacState = LOW; digitalWrite(triacGatePin, triacState); break; } } delay(100); } // Interrupt service routine for interrupt 0 void increment() { button_time = millis(); //check to see if increment() was called in the last 250 milliseconds if (button_time - last_button_time > 250) { pbPress = 1; if (menuState == 4){ menuState = 0; }else{ menuState += 1; } last_button_time = button_time; } } //for audio processing void resetValues(){ signalMax = 0; signalMin = 1024; peakToPeak = 0; } int getSwitchState(){ int pin2 = digitalRead(digInputA); int pin3 = digitalRead(digInputB); if (pin2 == 0){ return 1; //Medium volume } if(pin3 == 0){ return 2; //High volume; }else{ return 0; //Low volume; } }