// Install from the Library Manager: // "DallasTemperature" (Miles Burton; // it brings OneWire too) and "TM1637" // (Avishay Orpaz) #include #include #include // The probe's yellow (DATA) wire: analog // pins work as digital pins too const int PROBE_PIN = A0; // The display module's CLK and DIO pins const int DISPLAY_CLK = 7; const int DISPLAY_DIO = 6; const int BUZZER_PIN = 5; // The allowed range (°C). Aquarium: // 23-28. To beep once tea has cooled: // TOO_COLD 60, TOO_HOT 110 (never passed) const float TOO_COLD = 23.0; const float TOO_HOT = 28.0; // true: the alarm is set once it has been // inside the range (for tea the probe // starts cold, in the air). Aquarium: // leave it false const bool WAIT_UNTIL_IN_RANGE = false; // To go quiet it must come this far back // inside: no flicker at the limit const float MARGIN = 0.5; // Time between readings (ms) const unsigned long MEASURE_EVERY = 1000; // How long a 12-bit reading takes (ms) const unsigned long CONVERSION_TIME = 750; // The degree sign and the letter C const uint8_t SEG_DEGREE = SEG_A | SEG_B | SEG_F | SEG_G; const uint8_t SEG_LETTER_C = SEG_A | SEG_D | SEG_E | SEG_F; const uint8_t SEG_DASH = SEG_G; OneWire oneWire(PROBE_PIN); DallasTemperature probe(&oneWire); TM1637Display display(DISPLAY_CLK, DISPLAY_DIO); // No alarm, too cold or too hot enum AlarmState { NONE, COLD, HOT }; AlarmState alarmState = NONE; unsigned long alarmStart = 0; bool beeping = false; bool armed = !WAIT_UNTIL_IN_RANGE; // The last reading; probeOk false if // there was none float temperature = 0; bool probeOk = false; bool measured = false; unsigned long requestedAt = 0; bool waiting = false; // What the display shows: a temperature, // BLANK or DASHES; written on a change const int BLANK = -1000; const int DASHES = -2000; int shown = BLANK; // Shows the temperature: "24°C", "-5°C", // and from 100 up "100C" void showTemperature(unsigned long now) { if (!measured) return; int next = round(temperature); if (!probeOk) next = DASHES; // Blinks once a second during an alarm bool off = (now - alarmStart) % 1000 >= 600; if (probeOk && alarmState != NONE && off) next = BLANK; if (next == shown) return; shown = next; if (shown == BLANK) { display.clear(); return; } if (shown == DASHES) { uint8_t dashes[] = { SEG_DASH, SEG_DASH, SEG_DASH, SEG_DASH }; display.setSegments(dashes); return; } int whole = shown; if (whole >= -9 && whole <= 99) { display.showNumberDec(whole, false, 2, 0); uint8_t unit[] = { SEG_DEGREE, SEG_LETTER_C }; display.setSegments(unit, 2, 2); } else { display.showNumberDec(whole, false, 3, 0); uint8_t unit[] = { SEG_LETTER_C }; display.setSegments(unit, 1, 3); } } // Fast and high when hot, slow and low // when cold: the sound tells which void updateBuzzer(unsigned long now) { bool beep = false; int hz = 0; unsigned long t = now - alarmStart; if (alarmState == HOT) { // 3 short beeps, then 1 s of quiet t %= 2000; beep = t < 900 && (t / 150) % 2 == 0; hz = 2500; } else if (alarmState == COLD) { // One long beep every 1.5 s beep = t % 1500 < 400; hz = 700; } // tone() only when the sound changes if (beep != beeping) { beeping = beep; if (beep) { tone(BUZZER_PIN, hz); } else { noTone(BUZZER_PIN); } } } void checkLimits(unsigned long now) { if (!armed) { if (temperature < TOO_COLD || temperature > TOO_HOT) return; armed = true; Serial.println("Inside the range, alarm set."); } AlarmState next = alarmState; if (alarmState == NONE) { if (temperature > TOO_HOT) next = HOT; if (temperature < TOO_COLD) next = COLD; } else if (alarmState == HOT && temperature <= TOO_HOT - MARGIN) { next = NONE; } else if (alarmState == COLD && temperature >= TOO_COLD + MARGIN) { next = NONE; } if (next == alarmState) return; alarmState = next; alarmStart = now; if (alarmState == HOT) Serial.println("ALARM: too hot!"); if (alarmState == COLD) Serial.println("ALARM: too cold!"); if (alarmState == NONE) Serial.println("Back within the range."); } void setup() { pinMode(BUZZER_PIN, OUTPUT); Serial.begin(9600); display.setBrightness(5); probe.begin(); // The display and the sound keep going // while it measures: read it later probe.setWaitForConversion(false); Serial.print("Probes found: "); Serial.println(probe.getDeviceCount()); Serial.print("Range: "); Serial.print(TOO_COLD, 1); Serial.print(" - "); Serial.print(TOO_HOT, 1); Serial.println(" °C"); } void loop() { unsigned long now = millis(); if (!waiting && now - requestedAt >= MEASURE_EVERY) { probe.requestTemperatures(); requestedAt = now; waiting = true; } if (waiting && now - requestedAt >= CONVERSION_TIME) { waiting = false; measured = true; float reading = probe.getTempCByIndex(0); // -127: no probe, or a broken wire probeOk = reading != DEVICE_DISCONNECTED_C; if (probeOk) { temperature = reading; Serial.print("Temperature: "); Serial.print(temperature, 1); Serial.println(" °C"); checkLimits(now); } else { Serial.println("Cannot read the probe: check the wires and the 4.7 kΩ resistor."); // No sound for what it cannot read alarmState = NONE; } } showTemperature(now); updateBuzzer(now); }