Stap 8: Maken lawaai
Als u eenmaal de licht/donker sensor werken hoe je wilt kunt u twee functies voor het maken van het werk van de zoemer. De playTone() functie zorgt voor het geluid. De warningTone() functie roept de functie playTone() om uit te voeren van de specifieke geluiden voor onze schakeling. We dan om te testen het geluid functies kunt u de loop -functie om de output van verschillende tonen voor licht en donker voorwaarden wijzigen.
void playTone(uint8_t nPin, uint16_t nFreq, uint16_t nDuration) { // If frequency or duration is zero, stop any tones if ( (nFreq == 0) || (nDuration == 0) ) { noTone(nPin); } else { // Output the specified tone for the specified duration tone(nPin, nFreq, nDuration); // Since we need the tone to occur synchronously we wait for it to finish delay(nDuration); } }
void warningTone( uint16_t nFreq, uint16_t nDuration, uint8_t nCount) { // Setup a loop counter int n = 0; // While the loop counter is less than the specified count ... while (n++ < nCount) { // ... play the specified tone. playTone(BUZZER_PIN, nFreq, nDuration); // if we will repeat, pause briefly so the tones don't run together. if (n < nCount) { delay(250); } } }
void loop() { if (lightCheck() == "LIGHT") { warningTone( OPEN_WARNING_TONE, OPEN_WARNING_DURATION_MS, 1 ); } else { warningTone( TEMP_WARNING_TONE, TEMP_WARNING_DURATION_MS, TEMP_WARNING_REPEAT ); } Bean.sleep(500); }