Arduino nano does not recognize green as command

I've installed LED strips all over the house as sources of light, and I'm trying to controll them with python console(basic program that reads from console and just sends it to serial port), every color and command is accepter except "green", idk why, but in the same line I tried changing command to some random words, and it works...
Arduino NANO btw.

Code:

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');
//
if(command.equals("red")){
  myCom="red";
  command.equals("0");
  Serial.println(com);
}else
if(command.equals("green")){
  myCom="green";
  command.equals("0");
  Serial.println(com);
}else
if(command.equals("purple")){
  myCom="purple";
  command.equals("0");
  Serial.println(com);
}else
if(command.equals("all red")){
  myCom="2red";
  command.equals("0");
  Serial.println(com);
}else
if(command.equals("all green")){
  myCom="2green";
  command.equals("0");
  Serial.println(com);
}else
if(command.equals("all white")){
  myCom="2white";
  command.equals("0");
  Serial.println(com);
}else
if(command.equals("all blue")){
  myCom="2blue";
  command.equals("0");
  Serial.println(com);
}else
if(command.equals("white")){
  myCom="white";
  command.equals("0");
  Serial.println(com);
}else
if(command.equals("blue")){
  myCom="blue";
  command.equals("0");
  Serial.println(com);
}else
if(command.equals("red2")){
  myCom="red2";
  command.equals("0");
  Serial.println(com);
}else
if(command.equals("green2")){
  myCom="green2";
  command.equals("0");
  Serial.println(com);
}else
if(command.equals("white2")){
  myCom="white2";
  command.equals("0");
  Serial.println(com);
}else
if(command.equals("blue2")){
  myCom="blue2";
  command.equals("0");
  Serial.println(com);
}else
if(command.equals("alloff")){
  myCom="alloff";
  command.equals("0");
  Serial.println(com);
}else
if(command.equals("off1")){
  myCom="off1";
  command.equals("0");
  Serial.println(com);
}else
if(command.equals("off2")){
  myCom="off2";
  command.equals("0");
  Serial.println(com);
}else
if(command.equals("100")){
  myCom="100";
  command.equals("0");
  Serial.println(com);
}else
if(command.equals("75")){
  myCom="75";
  command.equals("0");
  Serial.println(com);
}else
if(command.equals("50")){
  myCom="50";
  command.equals("0");
  Serial.println(com);
}else
if(command.equals("25")){
  myCom="25";
  command.equals("0");
  Serial.println(com);
}else
if(command.equals("alarm")){
  myCom="ALARM";
  command.equals("0");
  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);
}

"Pin" and "Pin2" are sections of the house

Can someone please help?

These serve no purpose...

I suggest removing them.

Huh. Your code is dangerously close to being an event driven state machine. That's cool.

I'm not seeing any mistakes in the code. Post a schematic. Maybe there's hardware trouble.

Why do you use a nested if-else structure to detect the commands, but a series of consecutive 'if's to execute them?

Have you written any LED test code to confirm the hardware independently from your logic?

command = Serial.readStringUntil('\n');

Isn't it supposed to be a backslash?

3 Likes

Try the sketch with the suggestions by the others:

Start the simulation and type in "red", "red2", "green", "green2", "blue", "blue2", "alarm" and so on in the simulated Serial Monitor (the field to enter text is in the lower-right of the screen).

Thank you so much, I missed it, this fixed a problem. :slight_smile:

1 Like

Excellent. Glad it's working now.
Please mark that as the solution so others know that the thread is solved.

1 Like

Didn't you see the warnings?


/Users/john/Documents/Arduino/sketch_oct09a/sketch_oct09a.ino:34:38: warning: multi-character character constant [-Wmultichar]
     command = Serial.readStringUntil('/n');
                                      ^~~~
/Users/john/Documents/Arduino/sketch_oct09a/sketch_oct09a.ino: In function 'void loop()':
/Users/john/Documents/Arduino/sketch_oct09a/sketch_oct09a.ino:34:42: warning: overflow in implicit constant conversion [-Woverflow]
     command = Serial.readStringUntil('/n');
                                          ^

If you didn't get that helpful warning you should go into Preferences and set your "Compiler warnings" to "All".

1 Like

Thank you for pointing that out, compiler warnings were turned off.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.