Go Down

Topic: How can one read an integer from serial as fast as possible? (Read 2048 times) previous topic - next topic


As I mentioned earlier code is no polished.

I actually cast data[0] data[1] as bytes

Code: [Select]


[not read all the posts]
...serial as fast as possible?

The first thing I would do to is use a higher baud-rate if possible 115200 is 12x faster than 9600
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)


For those that are kinda interested here is an update. Everything is going according to the plan. I'll leave the code that works for me as of today. Still not polished and I plan to send one byte for motor speed and steering. Also have to upgrade esc and motor along with setting up dual servo for steering. Plus some CNC milling for custom mounts. But really happy as of now

Code: [Select]
#include <Servo.h>

//#define DEBUG

Servo motor;
Servo servo;

char data[2];
boolean serial_start = false;
boolean serial_end = false;
int index = 0;

byte startchar = 254;
byte endchar = 255;

void setup()

// Receives {CC} where C is a char
// Only CC is stored in data

void loop()
 while(Serial.available() > 0)
   byte readChar = Serial.read();
   #ifdef DEBUG
   Serial.print("Received: ");
   if (readChar == startchar)
     serial_start = true;
     index = 0;
     #ifdef DEBUG
       Serial.println("Starting char received");
   else if (readChar == endchar)
     serial_end = true;
     #ifdef DEBUG
       Serial.println("Ending char received");
     #ifdef DEBUG
       Serial.println("Data char received");
     if (index <= 1)
      data[index] = readChar;
      #ifdef DEBUG
       Serial.print("Data char assigned to index ");
 if (serial_start && serial_end)
   #ifdef DEBUG
     Serial.print("Packet received with data [");
   Serial.print("Setting motor and servo to: ");
   Serial.print(" ");
   serial_start = false;
   serial_end = false;

And here is C# code that sends xbox input to arduino. It's a mess and I should make it better, but it will happen in time.

Code: [Select]
       void timer1_Tick(object sender, EventArgs e)
           // Update controller data

           float leftStickX = gs.LeftStick.Position.X;
           float leftStickY = gs.LeftStick.Position.Y;
           int leftStickXINT = (int)(leftStickX * 1000);
           int leftStickYINT = (int)(leftStickY * 1000);

           if (leftStickYINT < 0)
               leftStickYINT = 0;

           lblControllerX.Text = leftStickXINT.ToString();
           lblControllerY.Text = leftStickYINT.ToString();

           lblControllerX2.Text = leftStickX.ToString();
           lblControllerY2.Text = leftStickY.ToString();

           byte motorSpeed = Map(leftStickYINT, 0, 1000, 94, 100 + 1);
           byte steerValue = Map(leftStickXINT, -1000, 1000, 180, 0 );

           lblRCThrottle.Text = motorSpeed.ToString();
           progressBar1.Value = Map(leftStickYINT, 0, 1000, 0, 100 + 1);

           lblRCSteer.Text = steerValue.ToString();

           if (sp.IsOpen && (motorSpeed != oldSpeed || steerValue != oldSteer))
               sp.Write(new byte[] { 254, motorSpeed, steerValue, 255 }, 0, 4);
               oldSpeed = motorSpeed;
               oldSteer = steerValue;

[font=Verdana]LOWER YOUR VOLUME AT THE END!!! I WARNED YOU!![/font][/size]

Enjoy my engrish

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131