// The flame sensor's DO output: LOW when // it sees a flame const int FLAME_PIN = 11; // The buzzer's and the two LEDs' pins const int BUZZER_PIN = 8; const int BLUE_LED = 9; const int RED_LED = 10; // A flame flickers, so DO does too: seen // again within this, it is still there const unsigned long FLICKER_HOLD = 300; // A flame lasting this long sounds the // alarm (a spark or a glint does not) const unsigned long CONFIRM_TIME = 500; // It keeps sounding this long after the // flame is out const unsigned long CLEAR_TIME = 3000; // The siren's lowest and highest tones // (Hz) const int SIREN_LOW = 800; const int SIREN_HIGH = 2000; unsigned long lastFlame = 0; unsigned long flameSince = 0; bool flameSeen = false; bool alarmOn = false; unsigned long alarmStart = 0; int playing = 0; void setup() { // A loose wire reads HIGH: no alarm pinMode(FLAME_PIN, INPUT_PULLUP); pinMode(BUZZER_PIN, OUTPUT); pinMode(BLUE_LED, OUTPUT); pinMode(RED_LED, OUTPUT); Serial.begin(9600); // Listen for 2 s: always seeing a flame // means the threshold may be too keen Serial.println("Checking the sensor..."); int seen = 0; for (int i = 0; i < 100; i++) { if (digitalRead(FLAME_PIN) == LOW) seen++; digitalWrite(BLUE_LED, (i / 10) % 2 ? HIGH : LOW); delay(20); } digitalWrite(BLUE_LED, LOW); if (seen > 90) { Serial.println("The sensor sees a flame all the time."); Serial.println("With no flame around, turn the screw on"); Serial.println("the board until its DO light goes out."); } Serial.println("Alarm set. To test it, light a lighter"); Serial.println("30-50 cm away from the sensor."); } // Siren: rises from low to high every // second, the LEDs take turns like a // fire engine's void updateOutputs(unsigned long now) { int hz = 0; if (alarmOn) { unsigned long t = (now - alarmStart) % 1000; hz = SIREN_LOW + (long)(SIREN_HIGH - SIREN_LOW) * t / 1000; // 20 Hz steps: fewer tone() calls hz = hz / 20 * 20; bool redTurn = (now - alarmStart) / 150 % 2 == 0; digitalWrite(RED_LED, redTurn ? HIGH : LOW); digitalWrite(BLUE_LED, redTurn ? LOW : HIGH); } else { digitalWrite(RED_LED, LOW); // On watch: blue blinks every 2 s, // and stays on while it sees a flame bool blink = now % 2000 < 100; digitalWrite(BLUE_LED, flameSeen || blink ? HIGH : LOW); } if (hz != playing) { playing = hz; if (hz > 0) { tone(BUZZER_PIN, hz); } else { noTone(BUZZER_PIN); } } } void loop() { unsigned long now = millis(); if (digitalRead(FLAME_PIN) == LOW) lastFlame = now; bool flame = lastFlame > 0 && now - lastFlame < FLICKER_HOLD; if (flame && !flameSeen) { flameSince = now; if (!alarmOn) Serial.println("Flame seen..."); } if (!flame && flameSeen && !alarmOn) { Serial.println("Too short, no alarm."); } flameSeen = flame; // Timed up to when it was last seen if (!alarmOn && flame && lastFlame - flameSince >= CONFIRM_TIME) { alarmOn = true; alarmStart = now; Serial.println("FIRE ALARM! Flame detected."); } if (alarmOn && now - lastFlame >= CLEAR_TIME) { alarmOn = false; Serial.print("Flame out, alarm stopped (it lasted "); Serial.print((now - alarmStart) / 1000); Serial.println(" s)."); } updateOutputs(now); }