Skip to content

Commit cb1759b

Browse files
committed
feat(matter): adds a light toggle switch button
1 parent 9380713 commit cb1759b

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

libraries/Matter/examples/MatterOnOffLight/MatterOnOffLight.ino

+9-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ const uint8_t ledPin = 2; // Set your pin here if your board has not defined LE
1515
#warning "Do not forget to set the LED pin"
1616
#endif
1717

18+
// set your board USER BUTTON pin here
19+
const uint8_t buttonPin = 0; // Set your pin here. Using BOOT Button. C6/C3 use GPIO9.
20+
1821
// Matter Protocol Endpoint Callback
1922
bool setLightOnOff(bool state) {
2023
Serial.printf("User Callback :: New Light State = %s\r\n", state ? "ON" : "OFF");
@@ -64,7 +67,7 @@ void setup() {
6467
}
6568

6669
uint32_t lastMillis = millis();
67-
const uint32_t toggle_interval = 15000; // light will toggle every 15 seconds
70+
const uint32_t debouceTime = 70; // button debounce time
6871
void loop() {
6972
// Check Matter Light Commissioning state
7073
if (!Matter.isDeviceCommissioned()) {
@@ -82,6 +85,8 @@ void loop() {
8285
Serial.println("Matter Node not commissioned yet. Waiting for commissioning.");
8386
}
8487
}
88+
// Initializa the USER BUTTON (Boot button) GPIO that will act as a toggle switch
89+
pinMode(buttonPin, INPUT_PULLUP);
8590
// Initialize the LED (light) GPIO and Matter End Point
8691
pinMode(ledPin, OUTPUT);
8792
Serial.printf("Initial state: %s\r\n", OnOffLight.getOnOff() ? "ON" : "OFF");
@@ -90,11 +95,9 @@ void loop() {
9095
delay(10000);
9196
}
9297

93-
//displays the Light state every 3 seconds
94-
Serial.printf("Matter Light is %s\r\n", OnOffLight.getOnOff() ? "ON" : "OFF");
95-
delay(3000);
96-
if (millis() - lastMillis > toggle_interval) {
97-
Serial.println("Toggling Light!");
98+
// Onboard User Button is used as a Light toggle switch
99+
if (digitalRead(buttonPin) == 0 && millis() - lastMillis > debouceTime) {
100+
Serial.println("User button pressed. Toggling Light!");
98101
lastMillis = millis();
99102
OnOffLight.toggle(); // Matter Controller also can see the change
100103
}

0 commit comments

Comments
 (0)