// For: https://forum.arduino.cc/t/arduino-nano-does-not-recognize-green-as-command/1039813
// Fixed the '\n' as mentioned by "er_name_not_found"
// Removed the command.equals("0") as mentioned by Coding_Badly
// Formatted the text
const int rPin2 = 3;
const int gPin2 = 5;
const int bPin2 = 6;
const int rPin = 9;
const int gPin = 10;
const int bPin = 11;
String myCom;
String command;
String com = "OK";
float dFact = 1;
float rBright = 255;
float gBright = 255;
float bBright = 255;
float rBright2 = 255;
float gBright2 = 255;
float bBright2 = 255;
void setup() {
Serial.begin(9600);
pinMode(rPin, OUTPUT);
pinMode(gPin, OUTPUT);
pinMode(bPin, OUTPUT);
pinMode(rPin2, OUTPUT);
pinMode(gPin2, OUTPUT);
pinMode(bPin2, OUTPUT);
}
void loop() {
if (Serial.available()) {
command = Serial.readStringUntil('\n'); // fixed
if (command.equals("red")) {
myCom = "red";
Serial.println(com);
} else if (command.equals("green")) {
myCom = "green";
Serial.println(com);
} else if (command.equals("purple")) {
myCom = "purple";
Serial.println(com);
} else if (command.equals("all red")) {
myCom = "2red";
Serial.println(com);
} else if (command.equals("all green")) {
myCom = "2green";
Serial.println(com);
} else if (command.equals("all white")) {
myCom = "2white";
Serial.println(com);
} else if (command.equals("all blue")) {
myCom = "2blue";
Serial.println(com);
} else if (command.equals("white")) {
myCom = "white";
Serial.println(com);
} else if (command.equals("blue")) {
myCom = "blue";
Serial.println(com);
} else if (command.equals("red2")) {
myCom = "red2";
Serial.println(com);
} else if (command.equals("green2")) {
myCom = "green2";
Serial.println(com);
} else if (command.equals("white2")) {
myCom = "white2";
Serial.println(com);
} else if (command.equals("blue2")) {
myCom = "blue2";
Serial.println(com);
} else if (command.equals("alloff")) {
myCom = "alloff";
Serial.println(com);
} else if (command.equals("off1")) {
myCom = "off1";
Serial.println(com);
} else if (command.equals("off2")) {
myCom = "off2";
Serial.println(com);
} else if (command.equals("100")) {
myCom = "100";
Serial.println(com);
} else if (command.equals("75")) {
myCom = "75";
command.equals("0");
Serial.println(com);
} else if (command.equals("50")) {
myCom = "50";
Serial.println(com);
} else if (command.equals("25")) {
myCom = "25";
Serial.println(com);
} else if (command.equals("alarm")) {
myCom = "ALARM";
Serial.println(com);
} else {
Serial.println("Invalid Command");
}
}
if (myCom == "alloff") {
rBright = 0;
gBright = 0;
bBright = 0;
rBright2 = 0;
gBright2 = 0;
bBright2 = 0;
}
if (myCom == "red2") {
rBright2 = 255;
gBright2 = 0;
bBright2 = 0;
}
if (myCom == "2red") {
rBright2 = 255;
gBright2 = 0;
bBright2 = 0;
rBright = 255;
gBright = 0;
bBright = 0;
}
if (myCom == "green2") {
rBright2 = 0;
gBright2 = 255;
bBright2 = 0;
}
if (myCom == "2green") {
rBright2 = 0;
gBright2 = 255;
bBright2 = 0;
rBright = 0;
gBright = 255;
bBright = 0;
}
if (myCom == "white2") {
rBright2 = 255;
gBright2 = 255;
bBright2 = 255;
}
if (myCom == "2white") {
rBright2 = 255;
gBright2 = 255;
bBright2 = 255;
rBright = 255;
gBright = 255;
bBright = 255;
}
if (myCom == "blue2") {
rBright2 = 0;
gBright2 = 0;
bBright2 = 255;
}
if (myCom == "2blue") {
rBright2 = 0;
gBright2 = 0;
bBright2 = 255;
rBright = 0;
gBright = 0;
bBright = 255;
}
if (myCom == "red") {
rBright = 255;
gBright = 0;
bBright = 0;
}
if (myCom == "green") {
rBright = 0;
gBright = 255;
bBright = 0;
}
if (myCom == "white") {
rBright = 255;
gBright = 255;
bBright = 255;
}
if (myCom == "blue") {
rBright = 0;
gBright = 0;
bBright = 255;
}
if (myCom == "purple") {
rBright2 = 255;
gBright2 = 0;
bBright2 = 180;
rBright = 255;
gBright = 0;
bBright = 255;
}
if (myCom == "off1") {
rBright = 0;
gBright = 0;
bBright = 0;
}
if (myCom == "off2") {
rBright2 = 0;
gBright2 = 0;
bBright2 = 0;
}
if (myCom == "25") {
dFact = .15;
}
if (myCom == "50") {
dFact = .5;
}
if (myCom == "75") {
dFact = .75;
}
if (myCom == "100") {
dFact = 1;
}
if (myCom == "ALARM") {
dFact = 1;
gBright = 0;
bBright = 0;
gBright2 = 0;
bBright2 = 0;
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
analogWrite(rPin, fadeValue);
analogWrite(rPin2, fadeValue);
delay(5);
}
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
analogWrite(rPin, fadeValue);
analogWrite(rPin2, fadeValue);
delay(5);
}
}
analogWrite(rPin, rBright * dFact);
analogWrite(gPin, gBright * dFact);
analogWrite(bPin, bBright * dFact);
analogWrite(rPin2, rBright2 * dFact);
analogWrite(gPin2, gBright2 * dFact);
analogWrite(bPin2, bBright2 * dFact);
}