Arduino connected to Dobot serial input too fast?

I am connecting my Arduino to a Dobot in order to do certain actions in order. I am able to turn it on when digitalRead StartButton == HIGH, however I am unable to turn it off when digitalRead StartButton == LOW, instead the code runs for a while more before stopping. I believe it is because the serial inputs to the arduino is faster than the execution of commands by the Dobot, however I am unsure. This is what my serial monitor prints when digitalRead == HIGH. I am still new to Arduino and C++, I apologise if the question is dumb. I have put parts of the code down.

High
Tx Packet: Packet header:aa aa, id:84, ctrl:03, payloadLen:19
params: 02 00 80 89 43 00 00 00 00 00 00 20 42 00 00 00 00
checksum:f9
Tx Packet: Packet header:aa aa, id:84, ctrl:03, payloadLen:19
params: 02 00 00 00 00 00 00 7a 43 00 00 20 41 00 00 00 00
checksum:89
Tx Packet: Packet header:aa aa, id:84, ctrl:03, payloadLen:19
params: 02 00 00 00 00 00 00 7a 43 00 00 20 c1 00 00 00 00
checksum:09
Tx Packet: Packet header:aa aa, id:84, ctrl:03, payloadLen:19
params: 02 00 00 00 00 00 00 7a 43 00 00 20 42 00 00 00 00
checksum:88
Tx Packet: Packet header:aa aa, id:84, ctrl:03, payloadLen:19
params: 02 00 00 6b 43 00 00 00 00 00 00 f0 c1 00 00 00 00
checksum:48
High
Tx Packet: Packet header:aa aa, id:84, ctrl:03, payloadLen:19
params: 02 00 00 6b 43 00 00 00 00 00 00 20 42 00 00 00 00
checksum:97
Tx Packet: Packet header:aa aa, id:84, ctrl:03, payloadLen:19
params: 02 00 00 00 00 00 00 7a 43 00 00 20 41 00 00 00 00
checksum:89
Tx Packet: Packet header:aa aa, id:84, ctrl:03, payloadLen:19
params: 02 00 00 00 00 00 00 7a 43 00 00 20 c1 00 00 00 00
checksum:09
Tx Packet: Packet header:aa aa, id:84, ctrl:03, payloadLen:19
params: 02 00 00 00 00 00 00 7a 43 00 00 20 42 00 00 00 00
checksum:88
Tx Packet: Packet header:aa aa, id:84, ctrl:03, payloadLen:19
params: 02 00 00 7f 43 00 00 00 00 00 00 20 42 00 00 00 00
checksum:83
Tx Packet: Packet header:aa aa, id:84, ctrl:03, payloadLen:19
params: 02 00 00 7f 43 00 00 00 00 00 00 f0 c1 00 00 00 00


void loop() 
{

    ButtonStateStart = digitalRead(StartButton);
    
    InitRAM();
  
    ProtocolInit();
    
    printf("\r\n======Enter demo application======\r\n");
    
    for(; ;)
    {
        static uint32_t timer = millis();
        static uint32_t count = 0;
        #ifdef JOG_STICK
        #else
        if(millis() - timer > 2000)
        {
            timer = millis();
            count++;
            while (ButtonStateStart = digitalRead(StartButton) == HIGH)
            {
                  move1();
                  Serial.println("High");
  
                  PutDown();
                  ButtonStateStart = digitalRead(StartButton);
                  if (ButtonStateStart == LOW)
                      break;
                  
  
                  move2();
                  Serial.println("High");
         
                  PutDown();
                  ButtonStateStart = digitalRead(StartButton);
                  if (ButtonStateStart == LOW)
                      break;
                  
  
                  move3();
                  Serial.println("High");
  
                  PutDown();
                  ButtonStateStart = digitalRead(StartButton);
                  if (ButtonStateStart == LOW)
                      break;
                  
  
                  move4();
                  Serial.println("High");
  
                  PutDown();
                  ButtonStateStart = digitalRead(StartButton);
                  if (ButtonStateStart == LOW)
                      break;
                  
  
                
                  
              }
          }
          #endif
      }
  }

void PutDown()
{
                gPTPCmd.z = 40;
                
                SetPTPCmd(&gPTPCmd, true, &gQueuedCmdIndex);
                ProtocolProcess();
                
                gPTPCmd.x = 0;
                gPTPCmd.y = 250;
                gPTPCmd.z = 10;
                gPTPCmd.r = 0;

                SetPTPCmd(&gPTPCmd, true, &gQueuedCmdIndex);
                ProtocolProcess();

                gPTPCmd.z = -10;
                
                SetPTPCmd(&gPTPCmd, true, &gQueuedCmdIndex);
                ProtocolProcess();

                delay(1000);

                StopSuck();

                gPTPCmd.z = 40;
                
                SetPTPCmd(&gPTPCmd, true,  &gQueuedCmdIndex);
                ProtocolProcess();

               
}

void move1()
{               
                gPTPCmd.x = 235;
                gPTPCmd.y = 0;
                gPTPCmd.z = 40;
                gPTPCmd.r = 0; 

                SetPTPCmd(&gPTPCmd, true, &gQueuedCmdIndex);
                ProtocolProcess();

                gPTPCmd.z = -30;
             
                SetPTPCmd(&gPTPCmd, true, &gQueuedCmdIndex);
                ProtocolProcess();

                Suck();
                delay(1300);
}

I have no idea what a "Dobot" is, but in the absence of you posting any code I'm going to say that might be due to a delay() during which the button's not being read. Wild guess though, and more meaningful replies would rely on seeing your code.

I have put down some of the code. Yes I put down delay, can you further explain why the button is not being read due to it?

When you enter the while-loop, you call move1() which has a delay of 1300; during that time your code will not read the button. You also call other funcvtions from move1(), if they use delay as well you have lost more time before the button will be read.

Next you call PutDown() which delays 1000; so the first time that your button will be read will be after at least 2.3 seconds after entering the while-loop.

You will need a millis() based approach and you probably need a finite state machine.

Sorry I am still new to coding and arduino is there an example you could show me? I have seen you comment on many of the posts and you seem like a veteran in arduino. Excuse me if I am being rude.

You're not rude. And no, I'm not a veteran, I'm a relative youngster :wink:

Below to give you the idea.

enum class LOOP_STATES
{
  INITRAM,
  PROTOCOLINIT,
  WAITSTART,
  MOVE1,
  PUTDOWN1,
  MOVE2,
  PUTDOWN2,
  MOVE3,
  PUTDOWN3,
  MOVE4,
  PUTDOWN4,
};

void loop()
{
  static LOOP_STATES state = LOOP_STATES::INITRAM;
  ButtonStateStart = digitalRead(StartButton);

  if(ButtonStateStart == LOW)
  {
    // do what needs to be done
    // e.g. reset the state to INITRAM
    // or to WAITSTART
  }

  switch (state)
  {
    case LOOP_STATES::INITRAM:
      InitRAM();
      state = LOOP_STATES::PROTOCOLINIT;
      break;
    case LOOP_STATES::PROTOCOLINIT:
      ProtocolInit();
      state = LOOP_STATES::PROTOCOLINIT;
      break;
    case LOOP_STATES::WAITSTART:
      if (ButtonStateStart == HIGH)
      {
        state = LOOP_STATES::MOVE1;
      }
      break;
    case LOOP_STATES::MOVE1:
      if (move1() == false)
      {
        state = LOOP_STATES::PUTDOWN1;
      }
      break;
    case LOOP_STATES::PUTDOWN1:
      ...
      ...
      state = LOOP_STATES::MOVE2;
      break;
    case LOOP_STATES::MOVE2:
      if (move2() == false)
      {
        state = LOOP_STATES::PUTDOWN2;
      }
      break;
    case LOOP_STATES::PUTDOWN2:
      ...
      ...
      state = LOOP_STATES::MOVE3;
      break;
    case LOOP_STATES::MOVE3:
      if (move3() == false)
      {
        state = LOOP_STATES::PUTDOWN3;
      }
      break;
    case LOOP_STATES::PUTDOWN3:
      ...
      ...
      state = LOOP_STATES::MOVE4;
      break;
    case LOOP_STATES::MOVE4:
      if (move4() == false)
      {
        state = LOOP_STATES::PUTDOWN4;
      }
      break;
    case LOOP_STATES::PUTDOWN4:
      ...
      ...
      // process complete; use one of below depending on need
      state = LOOP_STATES::WAITSTART;
      state = LOOP_STATES::INITRAM;
      break;
  }
}

enum class MOVE1_STATES
{
  CALCULATE,
  SUCK,
  WAIT,
};


/*
  move1 (whaever that is)
  returns:
    true if move1() is in progress, else false
*/
bool move1X()
{
  static uint32_t delayStartTime;
  static MOVE1_STATES state = MOVE1_STATES::CALCULATE;

  switch (state)
  {
    case MOVE1_STATES::CALCULATE:
      ...
      ...
      state = MOVE1_STATES::SUCK;
      break;
    case MOVE1_STATES::SUCK:
      ...
      ...
      delayStartTime = millis();
      state = MOVE1_STATES::WAIT;
      break;
    case MOVE1_STATES::WAIT:
      if (millis() - delayStartTime >= 1300)
      {
        state = MOVE1_STATES::CALCULATE;
        // indicate to caller that move1 is finished
        return false;
      }
      break;
  }
  // indicate to caller that move1 is in progress
  return true;
}

You might have a lot of repeating code (move1() .. move4() etc). If you post your complete code, you might get more taiolored advice.

Every uction that you use that uses delays should return true or false depending on the state as demonstrated in move1().

Note
You need to fill in the dotsl.

Thank you I learned alot from it. I have tried to make my own version of it, very crude haha, I still hope to fully adapt to your code later on. But with the code I have now and after changing the delays to millis(), I still have the same problem where the machine executes slower than the commands being send through Tx packets. Does lowering the baudrate help? Or do I need to queue packets? If so how would that work?

void PutDown()
{
                gPTPCmd.z = 40;
                
                SetPTPCmd(&gPTPCmd, true, &gQueuedCmdIndex);
                ProtocolProcess();
                
                gPTPCmd.x = 0;
                gPTPCmd.y = 250;
                gPTPCmd.z = 10;
                gPTPCmd.r = 0;

                SetPTPCmd(&gPTPCmd, true, &gQueuedCmdIndex);
                ProtocolProcess();

                gPTPCmd.z = -10;
                
                SetPTPCmd(&gPTPCmd, true, &gQueuedCmdIndex);
                ProtocolProcess();

                if (millis() - timer >= 1000){}  

                StopSuck();

                gPTPCmd.z = 40;
                
                SetPTPCmd(&gPTPCmd, true,  &gQueuedCmdIndex);
                ProtocolProcess();

               
}

void move1()
{               
                gPTPCmd.x = 235;
                gPTPCmd.y = 0;
                gPTPCmd.z = 40;
                gPTPCmd.r = 0; 

                SetPTPCmd(&gPTPCmd, true, &gQueuedCmdIndex);
                ProtocolProcess();

                gPTPCmd.z = -30;
             
                SetPTPCmd(&gPTPCmd, true, &gQueuedCmdIndex);
                ProtocolProcess();

                Suck();
                if (millis() - timer >= 1300){Serial.println("Pause1");}  
}

void move2()
{            
                gPTPCmd.x = 255;
                gPTPCmd.y = 0;
                gPTPCmd.z = 40;
                gPTPCmd.r = 0; 

                SetPTPCmd(&gPTPCmd, true, &gQueuedCmdIndex);
                ProtocolProcess();

                gPTPCmd.z = -30;
                
                SetPTPCmd(&gPTPCmd, true, &gQueuedCmdIndex);
                ProtocolProcess();

                Suck();
                if (millis() - timer >= 1300){Serial.println("Pause2");}                
}

void move3()
{             
                gPTPCmd.x = 275;
                gPTPCmd.y = 0;
                gPTPCmd.z = 40;
                gPTPCmd.r = 0; 

                
                SetPTPCmd(&gPTPCmd, true, &gQueuedCmdIndex);
                ProtocolProcess();

                gPTPCmd.z = -30;
                
                SetPTPCmd(&gPTPCmd, true, &gQueuedCmdIndex);
                ProtocolProcess();  

                Suck();
                if (millis() - timer >= 1300){Serial.println("Pause3");}  

}

void move4()
{

                gPTPCmd.x = 295;
                gPTPCmd.y = 0;
                gPTPCmd.z = 40;
                gPTPCmd.r = 0; 

          
                SetPTPCmd(&gPTPCmd, true, &gQueuedCmdIndex);
                ProtocolProcess();

                gPTPCmd.z = -30;
               
                SetPTPCmd(&gPTPCmd, true, &gQueuedCmdIndex);
                ProtocolProcess(); 

                
               gPTPCmd.z = 40;
               SetPTPCmd(&gPTPCmd, true, &gQueuedCmdIndex);
               ProtocolProcess();

               Suck();
               if (millis() - timer >= 1000){Serial.println("Pause4");}  
}
void SM(){
  switch (state){
    
  case 0: //move1
  if (digitalRead(StartButton) == HIGH){move1(); PutDown();}
      Serial.println("Move1");
      state++;
    
    break;
  
  case 1://move2
  if (digitalRead(StartButton) == HIGH){move2();PutDown();}
      Serial.println("Move2");
      state++;
    
    break;
  
  case 2://move3
  if (digitalRead(StartButton) == HIGH){move3();PutDown();}
      Serial.println("Move3");
      state++;
  
    break;
  
  case 3://move4
  if (digitalRead(StartButton) == HIGH){move4();PutDown();}
      Serial.println("Move4");
      state = 0;
  
   break;
  
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.