Skip to content

Commit 63a30e0

Browse files
authored
compatability for esp32 with rgb led sketch (#31)
1 parent 2518744 commit 63a30e0

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

examples/adafruitio_13_rgb/adafruitio_13_rgb.ino

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ void setup() {
3737
// wait for serial monitor to open
3838
while(! Serial);
3939

40+
41+
#if defined(ARDUINO_ARCH_ESP32) // ESP32 pinMode
42+
// assign rgb pins to channels
43+
ledcAttachPin(RED_PIN, 1);
44+
ledcAttachPin(GREEN_PIN, 2);
45+
ledcAttachPin(BLUE_PIN, 3);
46+
// init. channels
47+
ledcSetup(1, 12000, 8);
48+
ledcSetup(2, 12000, 8);
49+
ledcSetup(3, 12000, 8);
50+
#else
51+
pinMode(RED_PIN, OUTPUT);
52+
pinMode(GREEN_PIN, OUTPUT);
53+
pinMode(BLUE_PIN, OUTPUT);
54+
#endif
55+
4056
// connect to io.adafruit.com
4157
Serial.print("Connecting to Adafruit IO");
4258
io.connect();
@@ -56,7 +72,6 @@ void setup() {
5672
// we are connected
5773
Serial.println();
5874
Serial.println(io.statusText());
59-
color->get();
6075

6176
// set analogWrite range for ESP8266
6277
#ifdef ESP8266
@@ -92,8 +107,14 @@ void handleMessage(AdafruitIO_Data *data) {
92107
Serial.println(data->value());
93108

94109
// invert RGB values for common anode LEDs
95-
analogWrite(RED_PIN, 255 - data->toRed());
96-
analogWrite(GREEN_PIN, 255 - data->toGreen());
97-
analogWrite(BLUE_PIN, 255 - data->toBlue());
98-
110+
#if defined(ARDUINO_ARCH_ESP32) // ESP32 analogWrite
111+
ledcWrite(1, 255 - data->toRed());
112+
ledcWrite(2, 255 - data->toGreen());
113+
ledcWrite(3, 255 - data->toBlue());
114+
#else
115+
analogWrite(RED_PIN, 255 - data->toRed());
116+
analogWrite(GREEN_PIN, 255 - data->toGreen());
117+
analogWrite(BLUE_PIN, 255 - data->toBlue());
118+
#endif
119+
99120
}

0 commit comments

Comments
 (0)