Untitled
Led stateunsigned long buttonPin = 2; unsigned long led1Pin = 8; unsigned long led2Pin = 9; unsigned long led3Pin = 10; unsigned long dayMillis() { return millis() % 86400000; } bool timeReached(unsigned long startTime, unsigned long timeDelay) { unsigned long now = dayMillis(); if (now < startTime) { now += 86400000; } return now - startTime >= timeDelay; } class Button { public: int pin; int status; int state; unsigned long lastCheck = 0; int debounceDelay = 50; void (*pushDown)(); void (*pushUp)(); void init(int pin) { this->pin = pin; pinMode(this->pin, INPUT_PULLUP); } void run() { int status = digitalRead(this->pin); switch (this->state) { case 0: if (status == LOW) // nhan { this->lastCheck = dayMillis(); this->state = 1; } break; case 1: if (status == HIGH && !timeReached(this->lastCheck, this->debounceDelay)) { // chua du thoi gian this->state = 0; } else if (status == LOW && timeReached(this->lastCheck, this->debounceDelay)) { // dang nhan this->state = 2; if (this->pushDown != NULL) { this->pushDown(); } } break; case 2: if (status == HIGH) { this->state = 3; this->lastCheck = dayMillis(); } break; case 3: if (status == LOW && !timeReached(this->lastCheck, this->debounceDelay)) { // chua du thoi gian this->state = 2; } else if (status == HIGH && timeReached(this->lastCheck, this->debounceDelay)) { this->state = 0; if (this->pushUp != NULL) { this->pushUp(); } } } } }; class Led { private: int pin; Button *button; // Sử dụng con trỏ thay vì sao chép đối tượng int startTime = 0; public: void on() { digitalWrite(this->pin, HIGH); } void off() { digitalWrite(this->pin, LOW); } void init(int pin, Button *button, int startTime) { this->pin = pin; this->button = button; this->startTime = startTime; pinMode(this->pin, OUTPUT); } void run() { } }; Led led1; Led led2; Led led3; Button button; int status = -1; int bstatus = -1; unsigned long lastBStatusTime = 0; void buttonPushDown() { status++; switch (status) { case 0: led1.on(); led2.on(); led3.on(); break; case 1: led1.off(); led2.off(); led3.off(); break; case 2: case 3: bstatus = 0; lastBStatusTime = dayMillis(); break; case 4: status = -1; led1.off(); led2.off(); led3.off(); break; } } void buttonPushUp() { Serial.println("button up"); } void setup() { Serial.begin(9600); led1 = Led(); led2 = Led(); led3 = Led(); button = Button(); button.init(buttonPin); led1.init(led1Pin, &button, 1200); // Truyền địa chỉ của button led2.init(led2Pin, &button, 1600); led3.init(led3Pin, &button, 2000); button.pushDown = buttonPushDown; button.pushUp = buttonPushUp; } void loop() { digitalWrite(13, button.status); button.run(); // Cập nhật trạng thái nút trước if (status == 2 || status == 3){ int delay = 300; if(status == 3){ delay = 100; } switch (bstatus) { case 0: if (timeReached(lastBStatusTime, delay)){ lastBStatusTime = dayMillis(); bstatus = 1; } break; case 1: led1.on(); if (timeReached(lastBStatusTime, delay)){ lastBStatusTime = dayMillis(); bstatus = 2; } break; case 2: led1.off(); led2.on(); if (timeReached(lastBStatusTime, delay)){ lastBStatusTime = dayMillis(); bstatus = 3; } break; case 3: led2.off(); led3.on(); if (timeReached(lastBStatusTime, delay)){ lastBStatusTime = dayMillis(); bstatus = 0; led3.off(); } break; } } }
Leave a Comment