0

I'm trying to build a test program for some hardware. Basically I want to select the piece of hardware with the outer switch case then select the test with the inner switch case. I'm trying to accomplish this by using serial input in the Arduino IDE monitor.

I can't make it work whatever I do. I either end up stuck in the outer switch case or I never enter the inner switch case where I want to select the test.

I've got a bit of code below:

char select_addon;
char select_test;

void setup() {
  Serial.begin(9600);
  Serial.println("(1) PB1");
  Serial.println("(2) PB2");
  Serial.println("(3) PB3");
}

void loop() {
  if (Serial.available() > 0) {
    select_addon = Serial.read();
    switch (select_addon) {
      case '1':
        Serial.println("Test");
        if (Serial.available() > 0 ) {
          select_test = Serial.read();
          switch (select_test) {
            case '4':
              Serial.println(" Test 1.1");
              break;
            case '5':
              Serial.println(" Test 1.2");
              break;
            case '6':
              Serial.println(" Test 1.3");
              break;
          }
        }
        break;
      case '2':
        Serial.println("TEST 2");
        break;
      case '3':
        Serial.println("TEST 3");
        break;
    }
  }
}

Obviously this isn't the finished code, but it's the switch case structure I've been trying to implement. Any suggestions on how I can enter the inner switch case?

  • switch on ardunio seems to want numbers. your chars might be being compared as char-codes. try something like int select_addon = String(Serial.read()).toInt(); and case 3, instead case '3' – dandavis May 31 '17 at 19:22
2

What happens of the if (Serial.available() > 0) call fails? That's the problem, because when there isn't anything available it's skipping the second switch statement.

On a point of style it actually might be better to implement the inner switch as a separate function. Imagine what your code will look like with 5 cases in the outer switch, each with 5 cases in the inner switches.

Also it might be worth, for debugging purposes, to always have a default statement, at least then you will know what key it was that you pressed.

  • At 9600 baud the data comes in a byte per millisecond. The inner Serial.available does not see the next byte yet. Three solutions: (A) add a delay of about 5ms as soon as the outer Serial.available sees data. This is a bad solution, dont' do it. (B) Use the Arduino 'parse' functions arduino.cc/en/Reference/ParseInt . (C) Describe the serial data en describe that a linefeed at the end is mandatory. Read the data in a buffer until the linefeed is read or until the buffer is full, and then process the buffer. This is the best solution. – Jot May 30 '17 at 7:23
  • Yeah, I was going to put the inner switch case into functions as soon as I got them to work. Is there anything I can do to get the program to wait for another input once I've chosen the outer function? Lets say I type '1' in serial input, can I get it stuck somehow in that case and wait for 4,5 or 6? Because when I do select '1' all it does is print "Test" and goes back to the outer case. – Tisa May 30 '17 at 16:40
  • As I said in the answer the cause of you problem is if (Serial.available() > 0), if there is data in the serial buffer then execute the inner switch otherwise (i.e. there is no data in the couple of ms since you pressed one) do nothing. The problem is you aren't typing fast enough for you code. You need to press 4 within 1000th of a second of releasing 1. OR you could change you if statement to while (Serial.available() < 1); – Code Gorilla May 31 '17 at 7:05
  • 1
    Oh I understand now, that actually makes perfect sense. Thanks bud. – Tisa May 31 '17 at 19:43

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.