Go Down

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

P1X3

As I mentioned earlier code is no polished.

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

Code: [Select]
    motor.write((byte)(data[0]));
    servo.write((byte)(data[1]));

robtillaart

[not read all the posts]
Quote
...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)

P1X3

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()
{
 Serial.begin(9600);
 Serial.println("Waiting...");
 
 delay(2000);
 
 Serial.println("Arming...");
 motor.attach(2);
 servo.attach(10);
 motor.write(0);
 delay(2000);
 Serial.println("Armed");
}

// 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: ");
   Serial.println(readChar);
   #endif
   
   if (readChar == startchar)
   {
     serial_start = true;
     index = 0;
     #ifdef DEBUG
       Serial.println("Starting char received");
     #endif
   }
   else if (readChar == endchar)
   {
     serial_end = true;
     #ifdef DEBUG
       Serial.println("Ending char received");
     #endif
     break;
   }
   else
   {
     #ifdef DEBUG
       Serial.println("Data char received");
     #endif
     if (index <= 1)
     {
      data[index] = readChar;
      #ifdef DEBUG
       Serial.print("Data char assigned to index ");
       Serial.println(index);
      #endif
      index++;
     }
   }
 }
 
 if (serial_start && serial_end)
 {
   #ifdef DEBUG
     Serial.print("Packet received with data [");
     Serial.print(data[0]);
     Serial.print(",");
     Serial.print(data[1]);
     Serial.println("]");
   #endif
   motor.write((byte)(data[0]));
   servo.write((byte)(data[1]));
   Serial.print("Setting motor and servo to: ");
   Serial.print((byte)(data[0]));
   Serial.print(" ");
   Serial.println((byte)(data[1]));
   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
           gs.Update();

           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;

           seriesX.Points.Clear();
           seriesY.Points.Clear();
           seriesX.Points.Add(leftStickXINT);
           seriesY.Points.Add(leftStickYINT);
           lblControllerX.Text = leftStickXINT.ToString();
           lblControllerY.Text = leftStickYINT.ToString();

           seriesX2.Points.Clear();
           seriesY2.Points.Clear();
           seriesX2.Points.Add(leftStickX);
           seriesY2.Points.Add(leftStickY);
           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]
http://www.youtube.com/watch?v=K6s6QX5N2Mg

Enjoy my engrish

Go Up