1 year ago

#347940

test-img

OptiShen

How do I use serial.available 2 times in this code?

int input;
int choices;
void setup() {
 Serial.begin(9600);

Serial.println("BIN, DEC, HEX, OCT Converter\n");
retry:
Serial.println("Kindly choose a number system to convert (1, 2, 3, 4)");
Serial.println("1. BIN\n2. DEC\n3. HEX\n4. OCT\n");
while (Serial.available() == 0) {}

choices = Serial.parseInt();

if (choices == 1) {
Serial.println("You have chosen BIN, please enter a valid input");
  }
else if (choices == 2) {
Serial.println("You have chosen DEC, please enter a valid input");
  }
else if (choices == 3) {
Serial.println("You have choses HEX, please enter a valid input");
  }
else if (choices == 4) {
Serial.println("You have chosen OCT, please enter a valid input");
  }
else {
Serial.println("Invalid choice."); \
goto retry;
  }

switch (choices) {
case 1: while (Serial.available() == 0) {}
  input = Serial.parseInt();
  Serial.print("You have input ");
  Serial.print(input);
  break;
case 2: while (Serial.available() == 0) {}
  input = Serial.parseInt();
  Serial.print("You have input ");
  Serial.print(input);
  break;
  }

  }
  void loop() {
  }

In the part switch, when I run the code, it shows only 0 and it doesn't give me the option to enter at the serial monitor again.

I can't seem to figure this out, I'm a beginner who's trying to improve in the semantics of Arduino IDE. Thank you

arduino-ide

arduino-esp8266

0 Answers

Your Answer

Accepted video resources