// 2024.2.6, 3:15pm khw, added TMS1637 // after switched, on when the toothbrush in cradle nothing happen // when the tooth brush is removed, 4 LEDS are on, one will switch off for an interval of 30seconds // until all are off, then all 4 will flash for 20 seconds. //Whenever the brush is in the cradle, LEDs will flash for a while and then off. // the setup function runs once when you press reset or power the board //#include #include // for TMS1637 4-segment dsiplay const unsigned long COUNTDOWN_TIME = 130; //in seconds, 2 minutes +10 seconds foro preparation) #define CLK_PIN 8 #define DIO_PIN 9 TM1637Display display(CLK_PIN, DIO_PIN); unsigned long startTime; unsigned long currentTime; unsigned long elapsedTime; // for LED display //////////////////////////////////////////// int LED_A = 2; int LED_B = 3; int LED_C = 4; int LED_D = 5; int In1 = A0; //analogRead(In1) > 128) // brush is away;analogRead(In1) < 128) // brush is in cradle void setup() { Serial.begin(115200); // open the serial port at 9600 bps: // for TMS1637 //////////////////////////////////////////////// display.setBrightness(7); // Set the brightness of the display (0-7) display.clear(); // Clear the display startTime = millis(); // Record the starting time //Fors LED //////////////////////////////////////////// // initialize digital pin LED_BUILTIN as an output. pinMode(LED_A, OUTPUT); pinMode(LED_B, OUTPUT); pinMode(LED_C, OUTPUT); pinMode(LED_D, OUTPUT); pinMode(In1, INPUT); digitalWrite(LED_A, HIGH); // turn the LED on/off (LOW is off, HIGH is on) digitalWrite(LED_B, HIGH); // turn the LED on/off (LOW is off, HIGH is on) digitalWrite(LED_C, HIGH); // turn the LED on/off (LOW is off, HIGH is on) digitalWrite(LED_D, HIGH); // turn the LED on/off (LOW is off, HIGH is on) } void loop() { // if (analogRead(In1) < 128) that menas tooth brush in cradle if (analogRead(In1) < 128) { display.showNumberDecEx(0, 0b01000000, false); //init. state, not counting when brish in cradle digitalWrite(LED_A, LOW); // turn the LED on/off (LOW is off, HIGH is on) digitalWrite(LED_B, LOW); // turn the LED on/off (LOW is off, HIGH is on) digitalWrite(LED_C, LOW); // turn the LED on/off (LOW is off, HIGH is on) digitalWrite(LED_D, HIGH); // turn the LED on/off (LOW is off, HIGH is on) startTime = millis(); // Record the starting time } else { show_display(); } } void show_display() { //TMS1637 deisplay ////////////////////////////////////////////////////// currentTime = millis(); // Get the current time elapsedTime = (currentTime - startTime) / 1000; // Calculate elapsed time in seconds Serial.println("startTime="); Serial.println(startTime); Serial.println("currentTime="); Serial.println(currentTime); Serial.println("elapsedTime="); Serial.println(elapsedTime); Serial.println("................................................"); //delay(1000); if (elapsedTime <= COUNTDOWN_TIME) { unsigned long remainingTime = COUNTDOWN_TIME - elapsedTime; // Display remaining time in Minutes:Seconds format unsigned int minutes = remainingTime / 60; unsigned int seconds = remainingTime % 60; display.showNumberDecEx(minutes * 100 + seconds, 0b01000000, false); /////// for diaply LEDS ////////////////////////////////////////// if (remainingTime < 130) { digitalWrite(LED_A, HIGH); // turn the LED on (HIGH is the voltage level) digitalWrite(LED_B, HIGH); // turn the LED on (HIGH is the voltage level) digitalWrite(LED_C, HIGH); // turn the LED on (HIGH is the voltage level) digitalWrite(LED_D, HIGH); // turn the LED on (HIGH is the voltage level) } if (remainingTime < 90) { digitalWrite(LED_A, LOW); // turn the LED on (HIGH is the voltage level) digitalWrite(LED_B, HIGH); // turn the LED on (HIGH is the voltage level) digitalWrite(LED_C, HIGH); // turn the LED on (HIGH is the voltage level) digitalWrite(LED_D, HIGH); // turn the LED on (HIGH is the voltage level) } if (remainingTime < 60) { digitalWrite(LED_A, LOW); // turn the LED on (HIGH is the voltage level) digitalWrite(LED_B, LOW); // turn the LED on (HIGH is the voltage level) digitalWrite(LED_C, HIGH); // turn the LED on (HIGH is the voltage level) digitalWrite(LED_D, HIGH); // turn the LED on (HIGH is the voltage level) } if (remainingTime < 30) { digitalWrite(LED_A, LOW); // turn the LED on (HIGH is the voltage level) digitalWrite(LED_B, LOW); // turn the LED on (HIGH is the voltage level) digitalWrite(LED_C, LOW); // turn the LED on (HIGH is the voltage level) digitalWrite(LED_D, HIGH); // turn the LED on (HIGH is the voltage level) } // if (remainingTime == 0) { while (true) { // display.showNumberDecEx(0, 0b01000000, true); // Display "00:00" display.showNumberDecEx(0, 0b01000000, false); // Display "00:00" digitalWrite(LED_A, LOW); // turn the LED on/off (LOW is off, HIGH is on) digitalWrite(LED_B, LOW); // turn the LED on/off (LOW is off, HIGH is on) digitalWrite(LED_C, LOW); // turn the LED on/off (LOW is off, HIGH is on) digitalWrite(LED_D, LOW); // turn the LED on/off (LOW is off, HIGH is on) delay(500); display.clear(); // Clear the display digitalWrite(LED_A, HIGH); // turn the LED on/off (LOW is off, HIGH is on) digitalWrite(LED_B, HIGH); // turn the LED on/off (LOW is off, HIGH is on) digitalWrite(LED_C, HIGH); // turn the LED on/off (LOW is off, HIGH is on) digitalWrite(LED_D, HIGH); // turn the LED on/off (LOW is off, HIGH is on) delay(500); if (analogRead(In1) < 128) { //in cradle display.showNumberDecEx(0, 0b01000000, false); // Display "00:00" digitalWrite(LED_A, LOW); // turn the LED on/off (LOW is off, HIGH is on) digitalWrite(LED_B, LOW); // turn the LED on/off (LOW is off, HIGH is on) digitalWrite(LED_C, LOW); // turn the LED on/off (LOW is off, HIGH is on) digitalWrite(LED_D, HIGH); // turn the LED on/off (LOW is off, HIGH is on) startTime = millis(); // Record the starting time break; // when the toothbrush is put back to the cradle, exit to the main loop } } } } }