ArduinoESP32-C3 DevKitM-1Lab virtual

ESP32-C3: button and RGB LED

A button on GPIO 4 steps the C3 DevKitM-1’s RGB LED through colours.

ESP32-C3: button and RGB LED in Argynix Lab

The code

sketch.inoC++
// ESP32-C3 DevKitM-1: a button on GPIO 4 (to GND) steps its RGB LED (GPIO8) through colours.
const int BUTTON = 4;
const uint8_t colours[][3] = {{0, 0, 0}, {255, 0, 0}, {0, 255, 0}, {0, 0, 255}, {255, 255, 255}};
int colour = 0;

void setup() {
  Serial.begin(115200);
  pinMode(BUTTON, INPUT_PULLUP);
}

void loop() {
  static bool was = HIGH;
  bool now = digitalRead(BUTTON);
  if (was == HIGH && now == LOW) {
    colour = (colour + 1) % 5;
    rgbLedWrite(RGB_BUILTIN, colours[colour][0], colours[colour][1], colours[colour][2]);
    Serial.printf("colour %d\n", colour);
  }
  was = now;
  delay(10);
}