Untitled
const int ledPin = 13; const int ledLifePin = 11; const int devicePin = 12; const int sensorPin = 2; const int buttonPin = 3; const unsigned long interval = 900000; const unsigned long delaySensorForButton = 10000; const unsigned long delayForButton = 300; const unsigned long timeCheckWhenOpening = 45000; const int onStatus = LOW; const int offStatus = HIGH; int state = 0; unsigned long lastTurnOn = 0; unsigned long lastButtonPushed = 0; unsigned long lastSensorActive = 0; unsigned long currentMillis = 0; unsigned long openMillis = 0; unsigned long closeMillis = 0; class LedLife { public: int pin; int lastMillis; int state = 0; LedLife(int pin) { this->pin = pin; } void update() { unsigned long cdelay = 5000; switch (state) { case 0: cdelay = 7000; break; case 1: case 2: case 3: case 4: case 5: case 6: case 7: cdelay = 100; break; } unsigned long currentMillis = millis(); if (currentMillis - lastMillis > cdelay) { lastMillis = currentMillis; state++; if (state > 7) { state = 0; } } digitalWrite(pin, (state + 1) % 2); } }; class Button { public: int pin; int state; int lastMillis; int pushedLevel = LOW; unsigned long bounceTime = 50; void (*buttonPushed)(); void (*buttonReleased)(); Button(int pin) { this->pin = pin; state = 0; } void update() { int buttonState = digitalRead(pin); if (state == 0 && buttonState == pushedLevel) { state = 1; // debouncing push lastMillis = millis(); } else if (state == 1 && buttonState == pushedLevel) { unsigned long currentMillis = millis(); if (currentMillis - lastMillis > bounceTime) { state = 2; // pushed if (buttonPushed) buttonPushed(); } } else if (state == 2 && buttonState != pushedLevel) { state = 3; // debouncing release lastMillis = millis(); } else if (state == 3 && buttonState != pushedLevel) { unsigned long currentMillis = millis(); if (currentMillis - lastMillis > bounceTime) { state = 0; // released if (buttonReleased) buttonReleased(); } } } }; // buoc vao thi bat, sau 45 giay ma buoc ra thi tat // neu sau 45 giay van chua buoc ra thi sau 20 phut se tu dong tat // khi bam vao nut thi bat/tat binh thuong, trong vong 10 giay se khong nhan lenh tu sensor // den chi bao hoat dong cu 5 giay se nhap nhay 1 lan // day cot la led Button button(buttonPin); Button sensor(sensorPin); LedLife ledLife(ledLifePin); void updateStatus() { if (state == 0) { digitalWrite(devicePin, offStatus); openMillis = millis(); } else { digitalWrite(devicePin, onStatus); closeMillis = millis(); } } void setup() { pinMode(ledPin, OUTPUT); pinMode(devicePin, OUTPUT); pinMode(sensorPin, INPUT); pinMode(buttonPin, INPUT_PULLUP); pinMode(ledLifePin, OUTPUT); digitalWrite(devicePin, offStatus); Serial.begin(9600); button.pushedLevel = LOW; button.buttonPushed = []() { Serial.println("Nut nhan"); lastButtonPushed = currentMillis; if (currentMillis - lastTurnOn < delayForButton) return; lastTurnOn = millis(); state = 1 - state; // toggle updateStatus(); }; sensor.pushedLevel = HIGH; sensor.buttonPushed = []() { Serial.println("Cam bien kich hoat"); if (currentMillis - lastButtonPushed < delaySensorForButton) { Serial.println("Cam bien bat nhung do vua dung button nen bo qua"); return; } if (state == 0) { state = 1; Serial.println("Bat bang cam bien"); updateStatus(); } else { // dang bat if (currentMillis - openMillis < timeCheckWhenOpening) { Serial.print(currentMillis - openMillis); Serial.println(" --- "); Serial.print(timeCheckWhenOpening); Serial.println(" --- "); // dung tat neu thoi gian bat chua nhieu Serial.println("Chua du thoi gian de cam bien co the tat"); }else{ state = 0; Serial.println("Tat bang cam bien"); updateStatus(); } } }; delay(3000); Serial.println("Ready"); } void loop() { int sensorState = digitalRead(sensorPin); digitalWrite(ledPin, sensorState); currentMillis = millis(); button.update(); sensor.update(); ledLife.update(); if(state == 1 && currentMillis - openMillis > interval){ Serial.println("Tat tu dong"); state = 0; updateStatus(); } }
Leave a Comment