//Pins are defined here(defined as a integer constant and not a variable)
const int buttonRed = D9;
const int buttonGreen = D8;
const int buttonBlue = D7;
const int ledRed = D2;
const int ledGreen = D3;
const int ledBlue = D4;
void setup() {
Serial.begin(115200); //Starts serial communication at 115200 baud rate
Serial.println("Ready"); //Prints Ready when the program has started successfully
pinMode(buttonRed, INPUT); // When the button is not pressed, the pin reads HIGH (due to the pull-up resistor)
pinMode(buttonGreen, INPUT);
pinMode(buttonBlue, INPUT);
pinMode(ledRed, OUTPUT); //This allows the microcontroller to control the LEDs by setting them HIGH (turning them on) or LOW (turning them off)
pinMode(ledGreen, OUTPUT);
pinMode(ledBlue, OUTPUT);
digitalWrite(ledRed, LOW); //ensures that all LEDs are off at the start of the program
digitalWrite(ledGreen, LOW);
digitalWrite(ledBlue, LOW);
}
void loop() {
bool redState = digitalRead(buttonRed); //reads the current state (HIGH or LOW) of each button
bool greenState = digitalRead(buttonGreen);
bool blueState = digitalRead(buttonBlue);
digitalWrite(ledRed, !redState); //controls each LED by setting it to HIGH (on) or LOW (off)
digitalWrite(ledGreen, !greenState);
digitalWrite(ledBlue, !blueState);
Serial.print("Red: "); Serial.print(!redState); //Prints the LED states (1 for on, 0 for off) to the Serial Monitor
Serial.print(" Green: "); Serial.print(!greenState);
Serial.print(" Blue: "); Serial.println(!blueState);
delay(100);
}
Loading
xiao-esp32-c3
xiao-esp32-c3