How to send 2 serial connection using switch case?

Hi,

Thanks for the advise I manage to program the motor. But now I have a problem face which is how do program to send the comment to arduino as following:

  1. Press down W---to enable to move forward (which I have done using switch case)
  2. When W is press, then A is press--- to enable turning (how to do the loop inside the switch case)

Can anyone help me for this?

Thank you and appreciate for your reply.

Regards

zheng koon

You don't loop inside the switch. Instead, your loop() function should pick up new input each time, and figure out new output each time, without any waiting.

Thus, the loop() probably looks something like:

bool forward;
bool turn;

loop() {
  if (Serial.available()) {
    int ch = Serial.readChar(); // I forget the exact function
    switch (ch) {
    case 'w': forward = true;
    case 's': forward = false;
    case 'a': turn = true;
    case 'd': turn = false;
    }
  }
  if (forward) {
    set_forward_signals();
  }
  else {
    set_stop_signals();
  }
  if (turn) {
    set_turn_signals();
  }
  else {
    set_straight_signals();
  }
}

Note that the set_ functions will be called all the time (each time through loop()) with the state of the controller. This is OK; setting forward when you're already moving forward should just keep moving forward. If that's not the case in your hardware (special hardware) then you have to also remember the "current" state in addition to the "desired" state, and only send a change when the desired state changes.

Hi jwatte

thanks for the reply and your code did help me a lot.
I am wondering is it possible to get the code to detect the second button press like below?

if (forward) {
    if (Serial.read()='a')
    {
        *doing forward and turning*;
    }
    else {
         set_stop_signals();
   }

I getting the error for the code.

Please post the error message. Then read the error message. Then, assuming that's the actual code you're trying to compile, the error message should actually tell you what's wrong :slight_smile: