@@ -15,6 +15,9 @@ const uint8_t ledPin = 2; // Set your pin here if your board has not defined LE
15
15
#warning "Do not forget to set the LED pin"
16
16
#endif
17
17
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
+
18
21
// Matter Protocol Endpoint Callback
19
22
bool setLightOnOff (bool state) {
20
23
Serial.printf (" User Callback :: New Light State = %s\r\n " , state ? " ON" : " OFF" );
@@ -64,7 +67,7 @@ void setup() {
64
67
}
65
68
66
69
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
68
71
void loop () {
69
72
// Check Matter Light Commissioning state
70
73
if (!Matter.isDeviceCommissioned ()) {
@@ -82,6 +85,8 @@ void loop() {
82
85
Serial.println (" Matter Node not commissioned yet. Waiting for commissioning." );
83
86
}
84
87
}
88
+ // Initializa the USER BUTTON (Boot button) GPIO that will act as a toggle switch
89
+ pinMode (buttonPin, INPUT_PULLUP);
85
90
// Initialize the LED (light) GPIO and Matter End Point
86
91
pinMode (ledPin, OUTPUT);
87
92
Serial.printf (" Initial state: %s\r\n " , OnOffLight.getOnOff () ? " ON" : " OFF" );
@@ -90,11 +95,9 @@ void loop() {
90
95
delay (10000 );
91
96
}
92
97
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!" );
98
101
lastMillis = millis ();
99
102
OnOffLight.toggle (); // Matter Controller also can see the change
100
103
}
0 commit comments