Lag when receiving data in Arduino?

Hi,

We are doing some simple sending of bytes using serial from raspberry pi to Arduino when we encountered this problem. Although the data is shown as sent from raspberry side, it would take about 1 minute or so for the arduino to react (in this case we make a wheel move whenever a byte is received).

Upon further troubleshooting, we restarted the Raspberry pi with nothing running in background or startup.

when we connect the USB cable from arduino to raspberry pi,

  • the orange TX light is on and STEADY, not blinking (which my friend says it is transmitting data)
  • the yellow light is on and steady as well
  • when we disconnect the USB cable, the orange light is immediately off, and vice versa.
  • After a few minutes (probably through resetting arduino), the orange TX light dissapears, and when i send some bytes data over, the orange light blinked based on no of bytes sent, however again arduino does not move the wheel which we asked it to until about 1 minute delay.

Does anyone know if the Arduino is at fault or is there something wrong elsewhere?

In raspberry pi we are sending and integer of 32 bit/4 bytes with int32_t. On arduino side it is doing something like this:

#include <QueueArray.h>
#include "SharpIR.h"
#include "DualVNH5019MotorShield.h"
#include "PinChangeInt.h"

DualVNH5019MotorShield md;



// the setup function runs once when you press reset or power the board
void setup() {
	Serial.begin(9600);
	md.init();
}
int state = -1;


unsigned long command = 0;

unsigned int speed = 40;
unsigned int action = 0;

signed int distance = 0;
signed int degree = 0;

int action_divider = 100;
int dist_divider = 1000;

// the loop function runs over and over again until power down or reset
void loop() {
	if (Serial.available() > 0) {
		md.setM1Speed(200);
		delay(1000);
		md.setM1Speed(0);
		delay(1000);

		for (int i = 0; i < j / 4; i++) {
			unsigned long result = 0;
			unsigned long temp[4];

			for (int i = 0; i < 4; i++) {
				temp[i] = Serial.read();
				md.setM1Speed(200);
				delay(1000);
				md.setM1Speed(0);
				delay(1000);
			}
			result += temp[0] << 24;
			result += temp[1] << 16;
			result += temp[2] << 8;
			result += temp[3];
			//return result;
			command = result;
			
		}
		if (command > 0) {
			md.setM1Speed(200);
		}
	}
}

Help and guidance appreciated!! When sending integer over(in byte form), both are set at 9600 baud rate.

Using Arduino Uno R3 DEV-01080 with Ardumoto Motor Driver Shield & PCB, PROTO Arduino
Uno shield

Please show your full code. Maybe you have a 1 minute delay in there between receiving the individual bytes; we don't know.

And use code tags

[code] past your code here [/code] will result in

your pasted code here

Hi,

I have updated my post accordingly. It is a brand new sketch code and there is no delay, everything is really really simple and basic because we just want to test if Arduino can receive the sent data properly.

On raspberry pi, the C code sends out 1 byte by 1 byte to Arduino based on the RS232 library for Windows & Linux with the method RS232_SendBuf and with a specified buffer size of 4.

everything is really really simple

Yes, and without your seeing your code... your code might be the issue.

I have seen poorly written transmit loops prevent a board from working correctly.

Ok I updated the code based on the latest one i retrieved from my team member. Sorry I didnt had the complete code previously.

It takes about 1 minute for the wheel to start moving... not sure if arduino receives the message in buffer and holding it there for so long?

tam22:
Ok I updated the code based on the latest

Don’t make changes to a Post that people have already commented on. Post new material in a new Reply so the continuity of the discussion makes sense.

If you want responsive code don’t use delay() anywhere. Use millis() to managet timing without blocking as illustrated in Several Things at a Time

You may also find the examples in Serial Input Basics helpful. They are simple, reliable and non-blocking.

…R

tam22:
Hi,

I have updated my post accordingly. It is a brand new sketch code and there is no delay, everything is really really simple and basic because we just want to test if Arduino can receive the sent data properly.

On raspberry pi, the C code sends out 1 byte by 1 byte to Arduino based on the RS232 library for Windows & Linux with the method RS232_SendBuf and with a specified buffer size of 4.

And what do I see in your code :wink:

md.setM1Speed(200);

delay(1000);
md.setM1Speed(0);
delay(1000);

One problem in your code is that you only check once if data is available. You assume all 4 bytes are available. It might very well be that you receive on byte, another byte a little later and the last two bytes quickly.
I suggest that you read serial input basics