Skip to content

Commit

Permalink
Merge pull request #2643 from caternuson/esp32_ledc_updates
Browse files Browse the repository at this point in the history
Update ESP32 Arduino examples for LEDC usage
  • Loading branch information
caternuson authored Oct 12, 2023
2 parents 89e3823 + 7e064d5 commit e8e837e
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@

uint8_t i=0;

uint8_t red_out = RED_LED;
uint8_t green_out = GREEN_LED;
uint8_t blue_out = BLUE_LED;

void setup() {
Serial.begin(115200);
Serial.println("\nProp-Maker Wing: LED Example");
Expand All @@ -73,21 +77,32 @@ void setup() {

// Set up the LED Pins
#if defined(ESP32) // and ESP32-S2!
ledcSetup(RED_LED, 5000, 8);
ledcAttachPin(RED_PIN, RED_LED);
ledcSetup(GREEN_LED, 5000, 8);
ledcAttachPin(GREEN_PIN, GREEN_LED);
ledcSetup(BLUE_LED, 5000, 8);
ledcAttachPin(BLUE_PIN, BLUE_LED);
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 1)
// newer LEDC API, use pins instead of channel
red_out = RED_PIN;
green_out = GREEN_PIN;
blue_out = BLUE_PIN;
ledcAttach(RED_PIN, 5000, 8);
ledcAttach(GREEN_PIN, 5000, 8);
ledcAttach(BLUE_PIN, 5000, 8);
#else
// older LEDC API, use channel, attach pin to channel
ledcSetup(RED_LED, 5000, 8);
ledcAttachPin(RED_PIN, RED_LED);
ledcSetup(GREEN_LED, 5000, 8);
ledcAttachPin(GREEN_PIN, GREEN_LED);
ledcSetup(BLUE_LED, 5000, 8);
ledcAttachPin(BLUE_PIN, BLUE_LED);
#endif
#else
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(BLUE_LED, OUTPUT);
pinMode(red_out, OUTPUT);
pinMode(green_out, OUTPUT);
pinMode(blue_out, OUTPUT);
#endif

analogWrite(RED_LED, 0);
analogWrite(GREEN_LED, 0);
analogWrite(BLUE_LED, 0);
analogWrite(red_out, 0);
analogWrite(green_out, 0);
analogWrite(blue_out, 0);
}

uint32_t Color(uint8_t r, uint8_t g, uint8_t b) {
Expand Down Expand Up @@ -119,8 +134,8 @@ void loop()
digitalWrite(POWER_PIN, HIGH);

// write colors to the 3W LED
analogWrite(RED_LED, red);
analogWrite(GREEN_LED, green);
analogWrite(BLUE_LED, blue);
analogWrite(red_out, red);
analogWrite(green_out, green);
analogWrite(blue_out, blue);
delay(2);
}
}
17 changes: 12 additions & 5 deletions FunHouse_Arduino_Demos/rainbow/rainbow.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,31 @@ uint8_t LED_dutycycle = 0;

void setup() {
Serial.begin(115200);

pinMode(LED_BUILTIN, OUTPUT);


#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 1)
ledcAttach(LED_BUILTIN, 5000, 8);
#else
ledcSetup(0, 5000, 8);
ledcAttachPin(LED_BUILTIN, 0);

#endif

pixels.begin(); // Initialize pins for output
pixels.show(); // Turn all LEDs off ASAP
pixels.setBrightness(20);
}



void loop() {
Serial.println("Hello!");

// pulse red LED
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 1)
ledcWrite(LED_BUILTIN, LED_dutycycle++);
#else
ledcWrite(0, LED_dutycycle++);
#endif

// rainbow dotstars
for (int i=0; i<pixels.numPixels(); i++) { // For each pixel in strip...
Expand All @@ -54,4 +61,4 @@ void rainbow(int wait) {
pixels.show(); // Update strip with new contents
delay(wait); // Pause for a moment
}
}
}
67 changes: 42 additions & 25 deletions FunHouse_Arduino_Demos/selftest/selftest.ino
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void setup() {
//while (!Serial);
Serial.begin(115200);
delay(100);

pixels.begin(); // Initialize pins for output
pixels.show(); // Turn all LEDs off ASAP
pixels.setBrightness(20);
Expand All @@ -37,7 +37,7 @@ void setup() {
pinMode(BUTTON_UP, INPUT_PULLDOWN);

//analogReadResolution(13);

tft.init(240, 240); // Initialize ST7789 screen
pinMode(TFT_BACKLIGHT, OUTPUT);
digitalWrite(TFT_BACKLIGHT, HIGH); // Backlight on
Expand All @@ -52,8 +52,8 @@ void setup() {
tft.setTextColor(ST77XX_YELLOW);
tft.print("DP310? ");

if (! dps.begin_I2C()) {

if (! dps.begin_I2C()) {
tft.setTextColor(ST77XX_RED);
tft.println("FAIL!");
while (1) delay(100);
Expand All @@ -67,8 +67,8 @@ void setup() {
tft.setCursor(0, 20);
tft.setTextColor(ST77XX_YELLOW);
tft.print("AHT20? ");
if (! aht.begin()) {

if (! aht.begin()) {
tft.setTextColor(ST77XX_RED);
tft.println("FAIL!");
while (1) delay(100);
Expand All @@ -79,12 +79,18 @@ void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(SPEAKER, OUTPUT);

#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 1)
ledcAttach(LED_BUILTIN, 2000, 8);
ledcAttach(SPEAKER, 2000, 8);
ledcWrite(SPEAKER, 0);
#else
ledcSetup(0, 2000, 8);
ledcAttachPin(LED_BUILTIN, 0);

ledcSetup(1, 2000, 8);
ledcAttachPin(SPEAKER, 1);
ledcWrite(1, 0);
#endif
}


Expand All @@ -94,11 +100,11 @@ void loop() {

/********************* sensors */
sensors_event_t humidity, temp, pressure;

tft.setCursor(0, 0);
tft.setTextColor(ST77XX_YELLOW, BG_COLOR);
dps.getEvents(&temp, &pressure);

tft.print("DP310: ");
tft.print(temp.temperature, 0);
tft.print(" C ");
Expand All @@ -125,23 +131,23 @@ void loop() {
tft.setCursor(0, 40);
tft.setTextColor(ST77XX_YELLOW);
tft.print("Buttons: ");
if (! digitalRead(BUTTON_DOWN)) {
if (! digitalRead(BUTTON_DOWN)) {
tft.setTextColor(ST77XX_GREY);
} else {
Serial.println("DOWN pressed");
tft.setTextColor(ST77XX_WHITE);
}
tft.print("DOWN ");

if (! digitalRead(BUTTON_SELECT)) {
if (! digitalRead(BUTTON_SELECT)) {
tft.setTextColor(ST77XX_GREY);
} else {
Serial.println("SELECT pressed");
tft.setTextColor(ST77XX_WHITE);
}
tft.print("SEL ");
if (! digitalRead(BUTTON_UP)) {

if (! digitalRead(BUTTON_UP)) {
tft.setTextColor(ST77XX_GREY);
} else {
Serial.println("UP pressed");
Expand All @@ -151,25 +157,25 @@ void loop() {

/************************** CAPACITIVE */
uint16_t touchread;

tft.setCursor(0, 60);
tft.setTextColor(ST77XX_YELLOW, BG_COLOR);
tft.print("Captouch 6: ");
touchread = touchRead(6);
if (touchread < 10000 ) {
if (touchread < 10000 ) {
tft.setTextColor(ST77XX_GREY, BG_COLOR);
} else {
tft.setTextColor(ST77XX_WHITE, BG_COLOR);
}
tft.print(touchread);
tft.println(" ");
Serial.printf("Captouch #6 reading: %d\n", touchread);

tft.setCursor(0, 80);
tft.setTextColor(ST77XX_YELLOW, BG_COLOR);
tft.print("Captouch 7: ");
touchread = touchRead(7);
if (touchread < 20000 ) {
if (touchread < 20000 ) {
tft.setTextColor(ST77XX_GREY, BG_COLOR);
} else {
tft.setTextColor(ST77XX_WHITE, BG_COLOR);
Expand All @@ -183,7 +189,7 @@ void loop() {
tft.setTextColor(ST77XX_YELLOW, BG_COLOR);
tft.print("Captouch 8: ");
touchread = touchRead(8);
if (touchread < 20000 ) {
if (touchread < 20000 ) {
tft.setTextColor(ST77XX_GREY, BG_COLOR);
} else {
tft.setTextColor(ST77XX_WHITE, BG_COLOR);
Expand All @@ -200,7 +206,7 @@ void loop() {
tft.setTextColor(ST77XX_YELLOW, BG_COLOR);
tft.print("Analog 0: ");
analogread = analogRead(A0);
if (analogread < 8000 ) {
if (analogread < 8000 ) {
tft.setTextColor(ST77XX_WHITE, BG_COLOR);
} else {
tft.setTextColor(ST77XX_RED, BG_COLOR);
Expand All @@ -214,7 +220,7 @@ void loop() {
tft.setTextColor(ST77XX_YELLOW, BG_COLOR);
tft.print("Analog 1: ");
analogread = analogRead(A1);
if (analogread < 8000 ) {
if (analogread < 8000 ) {
tft.setTextColor(ST77XX_WHITE, BG_COLOR);
} else {
tft.setTextColor(ST77XX_RED, BG_COLOR);
Expand All @@ -223,12 +229,12 @@ void loop() {
tft.println(" ");
Serial.printf("Analog A1 reading: %d\n", analogread);


tft.setCursor(0, 160);
tft.setTextColor(ST77XX_YELLOW, BG_COLOR);
tft.print("Analog 2: ");
analogread = analogRead(A2);
if (analogread < 8000 ) {
if (analogread < 8000 ) {
tft.setTextColor(ST77XX_WHITE, BG_COLOR);
} else {
tft.setTextColor(ST77XX_RED, BG_COLOR);
Expand All @@ -245,21 +251,25 @@ void loop() {
tft.print(analogread);
tft.println(" ");
Serial.printf("Light sensor reading: %d\n", analogread);

/************************** Beep! */
if (digitalRead(BUTTON_SELECT)) {
if (digitalRead(BUTTON_SELECT)) {
Serial.println("** Beep! ***");
fhtone(SPEAKER, 988.0, 100.0); // tone1 - B5
fhtone(SPEAKER, 1319.0, 200.0); // tone2 - E6
delay(100);
//fhtone(SPEAKER, 2000.0, 100.0);
}

/************************** LEDs */
// pulse red LED
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 1)
ledcWrite(LED_BUILTIN, LED_dutycycle);
#else
ledcWrite(0, LED_dutycycle);
#endif
LED_dutycycle += 32;

// rainbow dotstars
for (int i=0; i<pixels.numPixels(); i++) { // For each pixel in strip...
int pixelHue = firstPixelHue + (i * 65536L / pixels.numPixels());
Expand All @@ -271,9 +281,16 @@ void loop() {


void fhtone(uint8_t pin, float frequency, float duration) {
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 1)
ledcAttach(SPEAKER, frequency, 8);
ledcWrite(SPEAKER, 128);
delay(duration);
ledcWrite(SPEAKER, 0);
#else
ledcSetup(1, frequency, 8);
ledcAttachPin(pin, 1);
ledcWrite(1, 128);
delay(duration);
ledcWrite(1, 0);
#endif
}
Loading

0 comments on commit e8e837e

Please sign in to comment.