#define max_char 12
char message[max_char]; // stores your message
char r_char; // reads each character
byte index = 0; // defines the position in your array
int i;
int redPin = 11; // Red RGB pin -> D11
int greenPin = 10; // Green RGB pin -> D10
int bluePin = 9; // Blue RGB pin -> D9
int buttonPin = 2; // Push button pin -> D2
int redValue = 0; // Initialize Red value to 0
int greenValue = 0; // Initialize Green value to 0
int blueValue = 0; // Initialize Blue value to 0
String redTempValue; // Red RGB value
String greenTempValue; // Green RGB value
String blueTempValue; // Blue RGB value
int flag = 0;
char currentColor;
boolean buttonState = false; // Tracks the state of the button
boolean lastButtonState = false; // Tracks the previous state of the button
boolean ledState = true; // Tracks the state of the LED
boolean buttonPressed = false; // Tracks whether the button has been pressed
void setup() {
pinMode(redPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // Use the internal pull-up resistor
Serial.begin(9600); // Initialize serial communication at 9600 bits per second
}
void loop() {
// Read the state of the button
buttonState = digitalRead(buttonPin);
// Check if the button has been pressed
if (buttonState == LOW) {
buttonPressed = true;
}
// Check if the LED is on and the button has been pressed
if (ledState && buttonPressed) {
// Turn off the LED
ledState = false;
// Reset the button press flag
buttonPressed = false;
}
// Check if the LED is off and the button has been pressed
if (!ledState && buttonPressed) {
// Turn on the LED
ledState = true;
// Reset the button press flag
buttonPressed = false;
}
// Set the RGB values based on the LED state
if (ledState) {
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
Serial.println(message);
flag = 1;
for (i = 0; i < 12; i++) {
message[i] = '\0';
}
index = 0;
}
}