Im using processing to send input via serial to the microcontroller. Now commands seem to get from processing to the arduino microcontroller quickly. However, from the microcontroller to the computer is quite slow (gyro info being sent as characters). It seems that unless i put delay(50) in the arduino code that the buffer becomes overloaded with data and freezes for 10 seconds before spitting out the data. is there anyway i can correct this?????? I need this to be close to real time and need to find out how to get rid of this lag. i would greatly appreicate any help. thanks alotttt.
this is the code that i programmed my arduino with if that helps:
#include <Servo.h>
#include <AFMotor.h>
#include "math.h"
#include <SoftwareSerial.h>
#include "math.h"
/*** Definitions ***/
#define rxPin 6 // Defines the "recieve" pin for software serial to motor controller
#define txPin 7 // Defines the "transmit" pin for software serial to motor controller
#define resetPin 8 // Defines the reset pin for the motor controller
// Create a special SoftwareSerial for direction communications with the motor controller
SoftwareSerial motorSerial = SoftwareSerial(rxPin, txPin);
int i=0;
float vzero=analogRead(0);
float vinput=analogRead(1);
float voltage=3.30;
float bias=0.00;
float sense=2.00;
float degree=0.00;
float degreechange=00.00;
float totaldegreechange=00.00;
float angv=0.000000;
float time=50.00;
float reading=0.00;
float analog=0.00;
float lag=0.00;
float degreemax=0.00;
float degreemin=0.00;
float angvcorr=4.282.5;
float degreecorr=1.31.139*(90.00/270.00);
int correction=0;
int count=0;
int check=2;
int dir=1;
float array[1]={0};
AF_DCMotor motor(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
Servo myservo1;
Servo myservo2;
int mspeed;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
myservo1.attach(9);
InitMotor();
// send data only when you receive data:
}
void loop()
{
if (Serial.available() > 0)
{
mspeed=Serial.read();
}
if((mspeed!=(-1))&&(Serial.available() > 0)&&(mspeed!=56))
{
SetMotor(0,mspeed);
// motor.setSpeed((mspeed));
// Serial.println(mspeed);
// motor.run(FORWARD); // turn it on going forward
if(Serial.read()=='8')
{
myservo1.write(360);
}
else if(Serial.read()=='5')
{
myservo1.write(-360); // sets the servo position at 90 degrees
}
else if(Serial.read()=='2')
{
motor.run(FORWARD);
}
else if(Serial.read()=='4')
{
motor.run(BACKWARD);
}
else if(Serial.read()=='6')
{
}
}
count=count+1;
vzero=analogRead(0);
vinput=analogRead(1);
if (correction==0)
{
bias=vinput-vzero;
correction=1;
}
analog=(vinput)-(vzero)-bias;
if((analog==-2)||(analog==-1)||(analog==1)||(analog==2))
{
analog=0.00;
}
angv=(angvcorr*(analog/sense));
degree=(degreecorr*(angv*((time)/1000.00000)))+degree;
Serial.println(char(angv/2));
delay(time);
}
void InitMotor()
{
// Setup Pins for I/O
pinMode(txPin, OUTPUT); // Serial Transmit Pin
pinMode(resetPin, OUTPUT); // Motor Reset Pin
// Set the baud rate of software serial
motorSerial.begin(9600);
// Reset the motor controller (By setting low then high signals to the resetPin)
digitalWrite(resetPin, HIGH);
delay(10);
digitalWrite(resetPin, LOW);
delay(10);
digitalWrite(resetPin, HIGH);
delay(10);
}
// Public: Set a speed (0 to 127) and direction (boolean) to a motor
void SetMotor(int motorIndex, int speed)
{
// Create the packet buffer
unsigned char buffer[4];
// Start byte and device ID byte
buffer[0] = 0x80;
buffer[1] = 0x00;
// Set motor index
unsigned char index = 0;
// Motor number and direction
buffer[2] = 0x00 | index;
// Motor speed
unsigned char targetSpeed = speed;
buffer[3] = targetSpeed;
// Send the packet
SendPacket(buffer, 4);
}
// Private: Send a packet through the custom serial motorSerial
void SendPacket(unsigned char buffer, int bufferCount)
{
for(int i = 0; i < bufferCount; i++)
motorSerial.print(buffer, BYTE);*
}
i also tried playing around with the baud rate. However, i still did not have any luck. :(.