Arduino – Serial message to method too fast

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

I’m sending multiple Strings to arduino’s serial port using java, 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.

When I send commands, I want them to fire a certain method (from arduino) like I was firing them from the loop method, which works, as in, they fire as the order they’re placed in, after one’s delay is done.

Sorry if the question is vague.

Here’s my code:

    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;

If I would get two commands like, ‘6:1’ and ‘7:1’, I want to fire
the method with the case 6 case 7, which are bppLeft(int) and bppRight(int).

The ‘int’ values are not of importance right now, so don’t worry about that.
Anyway, both methods have this delay, which is like an action that lasts for the delay time.

Recreated my result would look like:

void loop(){

But then read from serial prints, and coverted to an method.

Hope this question makes sense,


Source: java

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.