have a BLUno bluetooth arduino based on uno.
the logic kind of works, I am connecting ok to the phone, getting and sending messages via blue tooth
but with the serial data, Im having problems using it, and with the logic.
I want to have the option to turn ON/OFF some RGB LEDs, the problem is that the state is sometimes erratic.
here the code
int redLED = 2;
int greenLED = 4;
int blueLED = 3;
static bool redRunning;
static bool greenRunning;
static bool blueRunning;
static bool freq = 34;
void setup() {
Serial.begin(115200); //initial the Serial
Serial.write("started");
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(blueLED, OUTPUT);
digitalWrite(redLED, HIGH);
digitalWrite(greenLED, HIGH);
digitalWrite(blueLED, HIGH);
redRunning = false;
greenRunning = false;
blueRunning = false;
freq = false;
}
void loop()
{
if (Serial.available() > 0)
f{
String s = Serial.readStringUntil('\n');
// String s = Serial.readStringUntil("#"); // Until CR (Carriage Return)
// s.replace("#", "");
Serial.println("s:" + s);
if (s == "red_ON")
{
redRunning = true;
}
if (s == "red_OFF")
{
redRunning = false;
} if (s == "green_ON")
{
greenRunning = true;
} if (s == "green_OFF")
{
greenRunning = false;
} if (s == "blue_ON")
{
blueRunning = true;
} if (s == "blue_OFF")
{
blueRunning = false;
}
}
//
if (redRunning) {
// Serial.println("redRunning");
digitalWrite(redLED, LOW);
delay(125);
digitalWrite(redLED, HIGH);
delay(125);
}
if (greenRunning) {
// Serial.println("greenRunning");
digitalWrite(greenLED, LOW);
delay(125);
digitalWrite(greenLED, HIGH);
delay(125);
}
if (blueRunning) {
// Serial.println("blueRunning");
digitalWrite(blueLED, LOW);
delay(125);
digitalWrite(blueLED, HIGH);
delay(125);
}
if (!redRunning && !greenRunning && !blueRunning) {
digitalWrite(redLED, HIGH);
digitalWrite(greenLED, HIGH);
digitalWrite(blueLED, HIGH);
}
}
So what is the proper way to handle state? so the LED stay blinking? but also can turn off all LEDs also if I can turn LEDs combined, at the moment, I'm turning on one, then the other,
All these problems come from not understanding how to handle state internally, and manage changes that come via serial.
Thanks!