@@ -26,17 +26,44 @@ const uint8_t buttonPin = 0; // Set your pin here. Using BOOT Button. C6/C3 use
26
26
// set your board Analog Pin here - used for changing the Fan speed
27
27
const uint8_t analogPin = A0; // Analog Pin depends on each board
28
28
29
+ // set your board PWM Pin here - used for controlling the Fan speed (DC motor example)
30
+ // for this example, it will use the builtin board RGB LED to simulate the Fan DC motor using its brightness
31
+ #ifdef RGB_BUILTIN
32
+ const uint8_t dcMotorPin = RGB_BUILTIN;
33
+ #else
34
+ const uint8_t dcMotorPin = 2 ; // Set your pin here if your board has not defined LED_BUILTIN
35
+ #warning "Do not forget to set the RGB LED pin"
36
+ #endif
37
+
29
38
// WiFi is manually set and started
30
39
const char *ssid = " your-ssid" ; // Change this to your WiFi SSID
31
40
const char *password = " your-password" ; // Change this to your WiFi password
32
41
42
+ void fanDCMotorDrive (bool fanState, uint8_t speedPercent) {
43
+ // drive the Fan DC motor
44
+ if (fanState == false ) {
45
+ // turn off the Fan
46
+ digitalWrite (dcMotorPin, LOW);
47
+ } else {
48
+ // set the Fan speed
49
+ uint8_t fanDCMotorPWM = map (speedPercent, 0 , 100 , 0 , 255 );
50
+ #ifdef RGB_BUILTIN
51
+ rgbLedWrite (dcMotorPin, fanDCMotorPWM, fanDCMotorPWM, fanDCMotorPWM);
52
+ #else
53
+ analogWrite (dcMotorPin, fanDCMotorPWM);
54
+ #endif
55
+ }
56
+ }
57
+
33
58
void setup () {
34
59
// Initialize the USER BUTTON (Boot button) GPIO that will toggle the Fan (On/Off)
35
60
pinMode (buttonPin, INPUT_PULLUP);
36
61
// Initialize the Analog Pin A0 used to read input voltage and to set the Fan speed accordingly
37
62
pinMode (analogPin, INPUT);
38
63
analogReadResolution (10 ); // 10 bits resolution reading 0..1023
39
-
64
+ // Initialize the PWM output pin for a Fan DC motor
65
+ pinMode (dcMotorPin, OUTPUT);
66
+
40
67
Serial.begin (115200 );
41
68
while (!Serial) {
42
69
delay (100 );
@@ -99,6 +126,8 @@ void setup() {
99
126
Fan.onChange ([](MatterFan::FanMode_t fanMode, uint8_t speedPercent) {
100
127
// just report state
101
128
Serial.printf (" Fan State: Mode %s | %d%% speed.\r\n " , Fan.getFanModeString (fanMode), speedPercent);
129
+ // drive the Fan DC motor
130
+ fanDCMotorDrive (fanMode != MatterFan::FAN_MODE_OFF, speedPercent);
102
131
// returns success
103
132
return true ;
104
133
});
0 commit comments