Arduino serial buffer

(1/2) > >>

icek:
Arduino serial continues to pose challenges for me.  I am currently sending FSR data from one Arduino Due to another Arduino Due to move the other Arduino Due's servos.  At first I didn't have any time break and just let the data flow as fast as the serial connection pushes and received.  Then I decided to slow down the sending by using delay(500).  Sure enough the data flow slowed quite a bit, but the servos still did not receive the data in a near real time fashion. In either case the incoming data is not being read real time and causes the servo to react only after minutes have gone by (a very excessive amount of time for any communication)

My question is, is there some way of either managing (increasing or decreasing) the Arduino buffer to accept the data and use it?  Or is the buffer irrelevant?  I'm trying to figure out why it can take minutes for the servos to react to data that is being sent close to real time.  I also noticed that the reaction is faster earlier in the connection than later.

The code I'm using is:

Code:

#include <Servo.h>
 
Servo servo1;  // create first servo object to control first servo
Servo servo2;  // create second servo object to control second servo
 
// Common servo setup values
int minPulse = 600;   // minimum servo position, us (microseconds)
int maxPulse = 2400;  // maximum servo position, us

// User input for servo and position
int userInput[3];    // raw input from serial buffer, 3 bytes
int startbyte;       // start byte, begin reading input
int servo;           // which servo to pulse?
int pos;             // servo angle 0-180
int i;               // iterator

int fsrOne = 5;  // analog pin used to connect the first FSR
int fsrTwo = 4;  // analog pin used to connect the second FSR
int val = 0;    // reading from the sensor

void setup() {
  Serial.begin(9600);  
  servo1.attach(9, minPulse, maxPulse);  // attaches the first servo on pin 9 to the first servo object
  servo2.attach(8, minPulse, maxPulse);  // attaches the second servo on pin 8 to the second servo object
}

void loop() {
  if (Serial.available() > 2)  // Wait for serial input (min 3 bytes in buffer)
    // Read the first byte
    startbyte = Serial.read();
    // If it's really the startbyte (255) ...
    if (startbyte == 253) {
      // ... then get the next two bytes
      for (i=0;i<2;i++) {
        userInput[i] = Serial.read();
      }
      // First byte = servo to move?
      servo = userInput[0];
      // Second byte = which position?
      pos = userInput[1];
      // Packet error checking and recovery
      if (pos == 253) { servo = 253; }

      // Assign new position to appropriate servo
      switch (servo) {
        case 1:
          servo1.write(pos);    // move servo1 to 'pos'
          break;
        case 2:
          servo2.write(pos);
          break;}
  delay(10);                           // waits for the servo to get there
    }
      val = analogRead(fsrOne);
    // Qualitatively eliminate first sensitivity level
  if (val >= 0 && val < 200) {
    val = map(val, 0, 199, 5, 5);
  } else if (val >= 200 && val < 800) {
    val = map(val, 200, 799, 35, 120);
  } else if (val >= 800 && val < 1024) {
    val = map(val, 800, 1023, 121, 254);
}
  Serial.write(253);
  Serial.write(1);
  Serial.write(val);     // the raw analog reading
  
    val = analogRead(fsrTwo);
    // Qualitatively eliminate first sensitivity level
  if (val >= 0 && val < 200) {
    val = map(val, 0, 199, 5, 5);
  } else if (val >= 200 && val < 800) {
    val = map(val, 200, 799, 35, 120);
  } else if (val >= 800 && val < 1024) {
    val = map(val, 800, 1023, 121, 254);
  }
  Serial.write(253);
  Serial.write(2);
  Serial.write(val);     // the raw analog reading
  
  delay(500);
  
}

Any help would be much appreciated.

zoomkat:
You might try using the serial monitor to see just what is being sent to the second arduino.

icek:
I verified that correct information is being sent.  So somewhere the board is delaying receiving it.  I figured it must the buffer (or lack thereof).

wyager:
In the notes you said "255", but in the code itself "253". Is that the problem? Also, how big is the serial buffer on an ATMega?

icek:
The note doesn't matter, though that should be cleaned up for clarification.  As for the buffer my understanding is that there are 128 bytes worth, but can it be managed or is it somehow automatically managed?  I don't know.

Navigation

[0] Message Index

[#] Next page