Skip to content

Commit d7b361f

Browse files
committed
tutorial 2 advance
1 parent b2efed8 commit d7b361f

File tree

5 files changed

+160
-4
lines changed

5 files changed

+160
-4
lines changed
232 KB
Loading
901 KB
Loading
152 KB
Loading

content/hardware/03.nano/boards/nano-matter/tutorials/matter-rgb-light/content.md

Lines changed: 159 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ Thanks to the seamless compatibility of the Nano Matter with almost any Matter n
4242
- [Arduino IDE 2.0+](https://www.arduino.cc/en/software) or [Arduino Cloud Editor](https://create.arduino.cc/editor)
4343
- [Amazon Alexa](https://www.amazon.com/Alexa-App/b?ie=UTF8&node=18354642011)
4444
- [Seeed_RGB_LED_Matrix](https://github.com/Seeed-Studio/Seeed_RGB_LED_Matrix) library to control the RGB LED matrix. You can install it as .ZIP using the Arduino IDE.
45-
- [DHT](https://github.com/mcmchris/DHT-sensor-library/tree/patch-1) library. Download from this [branch](https://github.com/mcmchris/DHT-sensor-library/tree/patch-1) so it support the Nano Matter.
4645

4746
### Board Core and Libraries
4847

@@ -60,7 +59,7 @@ Now navigate to **Tools > Board > Boards Manager** or click the Boards Manager i
6059

6160
Use the following connection diagram for the project:
6261

63-
![Project wiring diagram](assets/diagram-v1.png)
62+
![Project wiring diagram](assets/diagram-v2.png)
6463

6564
### Programming
6665

@@ -71,7 +70,164 @@ In the Arduino IDE upper menu, navigate to **Tools > Protocol stack** and select
7170
Copy and paste the following sketch:
7271

7372
```arduino
74-
73+
#include <Matter.h>
74+
#include <MatterLightbulb.h>
75+
#include "grove_two_rgb_led_matrix.h"
76+
77+
#define LED_R LED_BUILTIN
78+
#define LED_G LED_BUILTIN_1
79+
#define LED_B LED_BUILTIN_2
80+
81+
MatterColorLightbulb matter_color_bulb;
82+
83+
GroveTwoRGBLedMatrixClass matrix;
84+
85+
void update_led_color();
86+
void led_off();
87+
void handle_button_press();
88+
volatile bool button_pressed = false;
89+
90+
void setup() {
91+
Wire.begin();
92+
Serial.begin(115200);
93+
Matter.begin();
94+
matter_color_bulb.begin();
95+
matter_color_bulb.boost_saturation(51); // Boost saturation by 20 percent
96+
97+
// Set up the onboard button
98+
pinMode(BTN_BUILTIN, INPUT_PULLUP);
99+
attachInterrupt(BTN_BUILTIN, &handle_button_press, FALLING);
100+
101+
uint16_t VID = 0;
102+
VID = matrix.getDeviceVID();
103+
if (VID != 0x2886) {
104+
Serial.println("Can not detect led matrix!!!");
105+
while (1)
106+
;
107+
}
108+
Serial.println("Matrix init success!!!");
109+
110+
matrix.stopDisplay();
111+
112+
// Turn the LED off
113+
led_off();
114+
115+
Serial.println("Arduino Nano Matter - color lightbulb");
116+
117+
if (!Matter.isDeviceCommissioned()) {
118+
Serial.println("Matter device is not commissioned");
119+
Serial.println("Commission it to your Matter hub with the manual pairing code or QR code");
120+
Serial.printf("Manual pairing code: %s\n", Matter.getManualPairingCode().c_str());
121+
Serial.printf("QR code URL: %s\n", Matter.getOnboardingQRCodeUrl().c_str());
122+
}
123+
while (!Matter.isDeviceCommissioned()) {
124+
delay(200);
125+
}
126+
127+
Serial.println("Waiting for Thread network...");
128+
while (!Matter.isDeviceThreadConnected()) {
129+
delay(200);
130+
}
131+
Serial.println("Connected to Thread network");
132+
133+
Serial.println("Waiting for Matter device discovery...");
134+
while (!matter_color_bulb.is_online()) {
135+
delay(200);
136+
}
137+
Serial.println("Matter device is now online");
138+
139+
}
140+
141+
void loop() {
142+
// If the physical button state changes - update the lightbulb's on/off state
143+
if (button_pressed) {
144+
button_pressed = false;
145+
// Toggle the on/off state of the lightbulb
146+
matter_color_bulb.toggle();
147+
}
148+
149+
// Get the current on/off state of the lightbulb
150+
static bool matter_lightbulb_last_state = false;
151+
bool matter_lightbulb_current_state = matter_color_bulb.get_onoff();
152+
153+
// If the current state is ON and the previous was OFF - turn on the LED
154+
if (matter_lightbulb_current_state && !matter_lightbulb_last_state) {
155+
matter_lightbulb_last_state = matter_lightbulb_current_state;
156+
Serial.println("Bulb ON");
157+
// Set the LEDs to the last received state
158+
update_led_color();
159+
}
160+
161+
// If the current state is OFF and the previous was ON - turn off the LED
162+
if (!matter_lightbulb_current_state && matter_lightbulb_last_state) {
163+
matter_lightbulb_last_state = matter_lightbulb_current_state;
164+
Serial.println("Bulb OFF");
165+
led_off();
166+
}
167+
168+
static uint8_t hue_prev = 0;
169+
static uint8_t saturation_prev = 0;
170+
static uint8_t brightness_prev = 0;
171+
uint8_t hue_curr = matter_color_bulb.get_hue();
172+
uint8_t saturation_curr = matter_color_bulb.get_saturation_percent();
173+
uint8_t brightness_curr = matter_color_bulb.get_brightness_percent();
174+
175+
// If either the hue, saturation or the brightness changes - update the LED to reflect the latest change
176+
if (hue_prev != hue_curr || saturation_prev != saturation_curr || brightness_prev != brightness_curr) {
177+
update_led_color();
178+
hue_prev = hue_curr;
179+
saturation_prev = saturation_curr;
180+
brightness_prev = brightness_curr;
181+
}
182+
}
183+
184+
// Updates the color of the RGB LED to match the Matter lightbulb's color
185+
void update_led_color() {
186+
if (!matter_color_bulb.get_onoff()) {
187+
return;
188+
}
189+
uint8_t r, g, b;
190+
matter_color_bulb.get_rgb(&r, &g, &b);
191+
// If our built-in LED is active LOW, we need to invert the brightness values
192+
if (LED_BUILTIN_ACTIVE == LOW) {
193+
analogWrite(LED_R, 255 - r);
194+
analogWrite(LED_G, 255 - g);
195+
analogWrite(LED_B, 255 - b);
196+
} else {
197+
analogWrite(LED_R, r);
198+
analogWrite(LED_G, g);
199+
analogWrite(LED_B, b);
200+
}
201+
202+
uint32_t RGB = ((uint8_t)r << 16L) | ((uint8_t)g << 8L) | (uint8_t)b;
203+
matrix.displayColorBlock(RGB, 0, true);
204+
205+
Serial.printf("Setting bulb color to > r: %u g: %u b: %u\n", r, g, b);
206+
}
207+
208+
// Turns the RGB LED off
209+
void led_off() {
210+
// If our built-in LED is active LOW, we need to invert the brightness values
211+
if (LED_BUILTIN_ACTIVE == LOW) {
212+
analogWrite(LED_R, 255);
213+
analogWrite(LED_G, 255);
214+
analogWrite(LED_B, 255);
215+
} else {
216+
analogWrite(LED_R, 0);
217+
analogWrite(LED_G, 0);
218+
analogWrite(LED_B, 0);
219+
}
220+
matrix.stopDisplay();
221+
}
222+
223+
void handle_button_press() {
224+
static uint32_t btn_last_press = 0;
225+
if (millis() < btn_last_press + 200) {
226+
return;
227+
}
228+
btn_last_press = millis();
229+
button_pressed = true;
230+
}
75231
```
76232

77233
Once you uploaded the example code to the Nano Matter, open the Serial Monitor and reset the board.

content/hardware/03.nano/boards/nano-matter/tutorials/matter-temp-sensor/content.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Thanks to the seamless compatibility of the Nano Matter with almost any Matter n
4242
- [Arduino IDE 2.0+](https://www.arduino.cc/en/software) or [Arduino Cloud Editor](https://create.arduino.cc/editor)
4343
- [Google Home App](https://home.google.com/get-app/)
4444
- [U8g2](https://github.com/olikraus/u8g2) library to control the OLED display. You can install it from the Arduino IDE library manager.
45-
- [DHT](https://github.com/mcmchris/DHT-sensor-library/tree/patch-1) library. Download from this [branch](https://github.com/mcmchris/DHT-sensor-library/tree/patch-1) so it support the Nano Matter.
45+
- [DHT](https://github.com/mcmchris/DHT-sensor-library/tree/patch-1) library. Download from this [branch](https://github.com/mcmchris/DHT-sensor-library/tree/patch-1) so it supports the Nano Matter.
4646

4747
### Board Core and Libraries
4848

0 commit comments

Comments
 (0)