0

This code seems to function, but only works once before I need to press the reset button. Otherwise, it repeats the same error message. It might have something to do with emptying the String variable.

// A program that will allow a user to turn ON one of several LED's
// using if...else statements.

String pinAsk="Please select which pin you would like to supply power to:";

// A different coloured LED will be attached to each pin number.
int Red=14; // This one would be the A0 Header on the Arduino
int Yellow=15; // A1
int Green=16; // A2
int Blue=17; // A3
int Purple=18; // A4
int Violet=19; // A5

int invalidColorRed;
int invalidColorYellow;
int invalidColorGreen;
int invalidColorBlue;
int invalidColorPurple;
int invalidColorViolet;
// If the user didn't select any of these colours, then all these variables will
// be given a 1. This triggers an if...else condition that prints out a message,
// to say that they didn't enter a valid input.

String inputColor;
 // The String variable can store integers as well as other characters

String activePin1="The ";
// The users input is printed out between these.
String activePin2=" LED is now active";

int Volts=102; // The 102 tells the Arduino to output 2+ (Two volts)

void setup() {
 Serial.begin(9600);
 Serial.println("Colour Selection");
 Serial.println("");

 pinMode(Red,OUTPUT);
 pinMode(Yellow,OUTPUT); 
 pinMode(Green,OUTPUT);
 pinMode(Blue,OUTPUT); 
 pinMode(Purple,OUTPUT);
 pinMode(Violet,OUTPUT);
}

void loop() {
 Serial.println(pinAsk);
 Serial.println("Red = A0 (14)");
// These describe the available pins and their colors.
 Serial.println("Yellow = A1 (15)");
 Serial.println("Green = A2 (16)");
 Serial.println("Blue = A3 (17)");
 Serial.println("Purple = A4 (18)");
 Serial.println("Violet = A5 (19)");

  while (Serial.available()==0) { } // Wait for user to respond

 inputColor=Serial.readString(); // Store their input into an empty variable

// The following segment checks whether the user has selected to turn ON
// the Red LED.

 if (inputColor=="14") analogWrite(Red,Volts);
 // The String variable can also store integers as well as other characters
 else if (inputColor=="A0") { analogWrite(Red,Volts); }
 else if (inputColor=="Red") { analogWrite(Red,Volts); }
 else if (inputColor=="red") { analogWrite(Red,Volts); }
// User may only use lower-case letters.
  // The "equal equal" sign == doesn't assign a new value.
  // It will test whether a number or string is the same
// as described at the end of the equal's sign.

 else { analogWrite(Red,LOW); }
// If the users input, stored within inputColor, isn’t "14", "A0", "Red",
// or "red" then this Red LED will be turned off.

 if (inputColor!="14" && inputColor!="A0" && inputColor!="Red" && inputColor!="red") { invalidColorRed=1; }

// The exclamation mark or bang ! is a NOT operator.
// A "bang equal" sign != is placed after a variable, such as inputPin!=
// to say that the value must NOT be equal to "14". If the expression is true
// (The condition is fulfilled), then a 1 is given to invalidColorRed.

// The AND operator, shown as a double ampersand && combines
// all the conditions into the same expression. By putting this operator `
// between them, it tells the Arduino that all the variables must NOT be
// any of the values described.

// Each colour has its own invalidColor variable.
// If the user does not enter any of the valid inputs
// (Which means the LED is off), a 1 will be assigned to this colors
// invalidColor variable.

// If all these invalidColor variables have a value of 1, it means all
// the LED’s are off. This can be made into another condition that causes
// the Arduino to read an expression that tells the user that they haven’t
// entered a proper input and to try again.


// Checking to see if the user has opted to turn on the Yellow LED.
 if (inputColor=="15") analogWrite(Yellow,Volts);
 else if (inputColor=="A1" || "Yellow" || "yellow") { analogWrite(Yellow,Volts); } 
// The OR || operator combines the Lower-case and Upper-case letters in
// the same expression. A space between the operator and variables is not
// necessary, but improves readability. Also, a single pair of quotation
// marks "" can encase the conditions if || is being used.
 else { analogWrite(Yellow,LOW); }

 if (inputColor!="15" && inputColor!="A1" && inputColor!="Yellow" && inputColor!="yellow") { invalidColorYellow=1; }


// Checking to see if the user has opted to turn on the Green LED.
 if (inputColor=="16") analogWrite(Green,Volts);
 else if (inputColor=="A2") { analogWrite(Green,Volts); }
 else if (inputColor=="Green") { analogWrite(Green,Volts); }
 else if (inputColor=="green") { analogWrite(Green,Volts); }
 else { analogWrite(Green,LOW); }

 if (inputColor!="16" && inputColor!="A2" && inputColor!="Green" && inputColor!="green") { invalidColorGreen=1; }


// Checking to see if the user has opted to turn on the Blue LED.
 if (inputColor=="17") analogWrite(Blue,Volts);
 else if (inputColor=="A3") { analogWrite(Blue,Volts); }
 else if (inputColor=="Blue") { analogWrite(Blue,Volts); }
 else if (inputColor=="blue") { analogWrite(Blue,Volts); }
 else { analogWrite(Blue,LOW); }

 if (inputColor!="17" && inputColor!="A2" && inputColor!="Blue" && inputColor!="blue") { invalidColorBlue=1; }


// Checking to see if the user has opted to turn on the Purple LED.
 if (inputColor=="18") analogWrite(Purple,Volts);
 else if (inputColor=="A4") { analogWrite(Purple,Volts); }
 else if (inputColor=="Purple") { analogWrite(Purple,Volts); }
 else if (inputColor=="purple") { analogWrite(Purple,Volts); } 
 else { analogWrite(Purple,LOW); }

 if (inputColor!="18" && inputColor!="A4" && inputColor!="Purple" && inputColor!="purple") { invalidColorPurple=1; }


// Checking to see if the user has opted to turn on the Violet LED.
 if (inputColor=="19") analogWrite(Violet,Volts);
 else if (inputColor=="A5") { analogWrite(Violet,Volts); }
 else if (inputColor=="Violet") { analogWrite(Violet,Volts); }
 else if (inputColor=="violet") { analogWrite(Violet,Volts); }
 else { analogWrite(Violet,LOW); }

 if (inputColor!="19" && inputColor!="A5" && inputColor!="Violet" && inputColor!="violet") { invalidColorViolet=1; }


 if (invalidColorRed==1 && invalidColorYellow==1 && invalidColorGreen==1 && invalidColorBlue==1 && invalidColorViolet==1)
 {
  // This if statement is asking "Are all invalidPin variables equal to 1?"
  // In other words, "Are all the LED's off?"
  Serial.println("");
  Serial.println("That is not a valid option, please try again");
  Serial.println(""); }
  else if (invalidColorRed==0 || invalidColorYellow==0 || invalidColorGreen==0 || invalidColorBlue==0 || invalidColorViolet==0)
  {
  // If the user entered a valid input for at least one LED,
  // this message will play instead.
  Serial.println("");
  Serial.print(activePin1);
  Serial.print(inputColor); // The users input is printed out here.
  Serial.println(activePin2);
  Serial.println(""); }

  inputColor=""; // Shouldn't this empty the variable?
}

Can someone tell me whats wrong?

  • Could you post the output you get from the console? – Gerben Jul 15 '18 at 11:17
  • If a valid answer is entered, such as blue, it will print out 'The blue LED is is now active'. After that, it will repeat the first message, asking the user to select a color from the list presented. Even if the user gives an acceptable answer, it will read out the error message 'That is not a valid option, please try again'. The reset button needs to be pressed before the user can enter another color. – Maitland Jul 16 '18 at 8:59
  • You set the invalidColor___ to 1, but you never reset it. So if you set the blue color, all variables invalidColor___ except for invalidColorBlue are set to 1. Then if in the next loop you set the red color, invalidColorBlue` is also set to 1, and now all invalidColor___s are set to 1 and you get the error message. You need to set them back to 0 at the beginning of the loop. – Gerben Jul 16 '18 at 9:11
0

Without looking deeply into your code, this is wrong:

if (inputColor=="A1" || "Yellow" || "yellow") 

You need to repeat what you are testing against, ie.

if (inputColor=="A1" || inputColor=="Yellow" || inputColor=="yellow") 

You might want to use the auto-format tool (Ctrl+T). Putting a lot of stuff in one line is just confusing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.