Переменная, которая содержит введенные пользователем данные, не очищается сама по себе, поэтому программа работает только один раз.

Кажется, этот код работает, но срабатывает только один раз, прежде чем мне нужно нажать кнопку сброса. В противном случае он повторяет то же самое сообщение об ошибке. Возможно, это связано с очисткой переменной String.

// 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?
}

Может ли кто-нибудь сказать мне, в чем проблема?

, 👍0


1 ответ


Лучший ответ:

0

Не заглядывая глубоко в ваш код, можно сказать, что это неверно:

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

Вам нужно повторить то, с чем вы проводите тестирование, т.е.

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

Возможно, вам стоит воспользоваться инструментом автоформатирования (Ctrl+T). Размещение большого количества информации в одной строке просто сбивает с толку.

,