Fast Serial reading ends up in methods overriding eachother? [urgent]

Hey pplz,

I’ve encountered a problem which got me stuck while working on my project.

I’m sending multiple Strings to arduino’s serial port, which translates the strings to certain methods,
so the strings basically represent ‘commands’.

Anyways, when I send one command, everyone works as it should. However,
when I send multiple strings in a short amount of time, the project starts to act strange, and
I believe methods are overriding or something like that.

I send commands as ‘id:amount’, where ‘id’ is a number that has a certain method attachted, ‘amount’ is a multiply for milliseconds.

What I want is: When the serial reads the strings, they get added on some queue, and the methods just run after eachother.


void setup() {
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);

void loop() {

 Reads all commands
void initiateCommands(){
   String cmd = Serial.readStringUntil('\n');
   String command = getValue(cmd, ':', 0);
   int cmdId = command.toInt();
   int amount = getValue(cmd, ':',1).toInt();
   if(amount < 1) amount = 1;
      case 0: up(amount); break;
      case 1: down(amount); break;
      case 2: left(amount); break;
      case 3: right(amount);break;
      case 4: forward(); break;
      case 5: backward(); break;
      case 6: bppLeft(amount); break; 
      case 7: bppRight(amount); break;
      case 8: wait(amount); break;

Note: ‘getValue’ is just something to split a string into new strings.

You code is stupid.

I suggest you learn how a switch/case statement works.

michinyon: You code is stupid.

I suggest you learn how a switch/case statement works.

Thanks for your constructive critism.

Please enlighten me about me mistake rather than wasting my time with a pointless reply.

As far as I know this switch-case is correct, as I have experience in programming in both java and PHP, however, in arduino i'm rather new, hence my question.

Last but not least, everyone makes mistakes, and replying with such an immature ring to it, it's just sad.

You need to post all of your code. I'd guess, though, that the problem is that up(), down(), etc. use delay().

You'll need to set that code aside as useless, and start over. This time, read, understand, and embrace, the blink without delay philosophy and learn about state machines. At any given point in time, the Arduino is in some state. That could be waiting for a new command. It would be up()ing (whatever the heck that means). On every pass through loop(), you need to determine if something has happened (like sufficient time has passed or a new command has arrived) that warrants changing to a new state. If so, you do whatever is needed to transition from the current state to the new state - record when, turn some pins off, turn some pins on, etc.

PaulS: snip

Thank you!

I solved it nevertheless, I used a String to queue commands, that way they fire after eachother, like I wanted.

My switch-case, however, is working just fine.

Thanks you PaulS, I will take your advice.

michinyon: You code is stupid.

I suggest you learn how a switch/case statement works.

Your spelling is atrocious.

Would you care to expand on your comments, please?

Have a look at serial input basics Simple, reliable and non-blocking.