#define LED_PIN 2
#define BUTTON_PIN 13
#define PIN_RED 23 // GPIO23
#define PIN_GREEN 22 // GPIO22
#define PIN_BLUE 21 // GPIO21
int led_state = LOW; // the current state of LED
int button_state; // the current state of button
int last_button_state; // the previous state of button
void setup() {
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
pinMode(PIN_RED, OUTPUT);
pinMode(PIN_GREEN, OUTPUT);
pinMode(PIN_BLUE, OUTPUT);
// initialize the pushbutton pin as an pull-up input
pinMode(BUTTON_PIN, INPUT_PULLUP);
button_state = digitalRead(BUTTON_PIN);
}
void loop() {
last_button_state = button_state; // save the last state
button_state = digitalRead(BUTTON_PIN); // read new state
if (last_button_state == HIGH && button_state == LOW) {
Serial.println("The button is pressed");
// toggle state of LED
led_state = !led_state;
// control LED arccoding to the toggled state
digitalWrite(LED_PIN, led_state);
Serial.println("Flashing RGB...");
// color code #00C9CC (R = 0, G = 201, B = 204)
setColor(0, 201, 204);
delay(1000); // keep the color 1 second
// color code #F7788A (R = 247, G = 120, B = 138)
setColor(247, 120, 138);
delay(1000); // keep the color 1 second
// color code #34A853 (R = 52, G = 168, B = 83)
setColor(52, 168, 83);
delay(1000); // keep the color 1 second
Serial.println("Done");
}
//digitalWrite(LED, HIGH);
//delay(500);
//digitalWrite(LED, LOW);
//delay(500);
}
void setColor(int R, int G, int B) {
analogWrite(PIN_RED, R);
analogWrite(PIN_GREEN, G);
analogWrite(PIN_BLUE, B);
}