A flying Arduino Uno, hopefully.

I have a dream, probably an unlikely dream, but the coding is so fun I had to try to write the program before I even found out if it would work. Anyway, I wrote the following sketch that, from the serial monitor, works perfectly. Using 4 PWM pins, and entering 4 numbers separated by commas, I can successfully control 4 pager motors that I will put propellars on, and away I go!

int motor1 = 3;  //Motor Pin #'s
int motor2 = 5;
int motor3 = 6;
int motor4 = 9;
String readString;
String tempString = "";
char tempChars[18];
int motorSpeed[18];
int z = 1;
int count = 0;

void setup()
{
  pinMode(motor1, OUTPUT);  // sets the pin as output
  pinMode(motor2, OUTPUT);
  pinMode(motor3, OUTPUT);
  pinMode(motor4, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  while (Serial.available())
  {
    delay(10);
    if (Serial.available() >0)
    {
      char c = Serial.read();
      readString += c;
    }
  }
  if (readString.length() >0)
  {
    char carray[18];
    readString.toCharArray(carray, sizeof(carray));
    count = readString.length();
      for (int i=0; i<(count+1); i++)
        {
          if (carray[i] == ',' || i == count)
            {
              tempString.toCharArray(tempChars, sizeof(tempChars));
              motorSpeed[z] = atoi(tempChars);
              z = z + 1;
              tempString = "";              
            }
          else
            {
              tempString = tempString + carray[i];
            }
        }
    writeSpeed(motorSpeed[1], motorSpeed[2], motorSpeed[3], motorSpeed[4]);
    readString = "";
    z = 1;
  }
}
void writeSpeed(int x1, int x2, int x3, int x4)
  {
   analogWrite(motor1, x1);
   analogWrite(motor2, x2);
   analogWrite(motor3, x3);
   analogWrite(motor4, x4);
  }

The problem:

I wrote a VB.Net 2008 app that uses horizontal slider controls to control motor speed. 4 sliders, one for each motor, and an additional that controls all motor at the same time. It does what it is supposed to, but when I connect to the Arduino it gets very, very slow in its response. I can post the VB code if needed, but in a nutshell, when there is any change in any of the slider controls I "serial.write()" the values, as a string, to the Arduino. It should work in theory, but I think I may be looking for the serial data incorrectly or at the wrong time. Any help with my ArduinoCopter would be greatly appreciated.

Tom

Try switch serial to run at 115200 baud - 9600baud is about 1ms per character, and will be too slow for reliable control.

I tried that. It got even more, shall we say "unresponsive". It worked but not as well as 9600. Thanks for the reply! Any other thoughts?

Tom

while (Serial.available())
  {
    delay(10);
    if (Serial.available() >0)
    {
      char c = Serial.read();
      readString += c;
    }
  }

What's wrong with this?:

  while (Serial.available())
    readString += ((char) Serial.read());

Is there a reason to add the delay in? It seems like that's more likely to cause sluggishness than anything else.

The delay is there in the hopes that the entire packet will have arrived before the total delay time has elapsed - a silly way of "ensuring" that a complete packet is received, in my opinion.

Since OP has full control over the packet that is sent, adding an end-of-packet marker (and a start-of-packet marker, too) makes it easy to KNOW when a complete packet has been received, with no delay required, and allows for collecting partial packets, that don't get processed until the end of packet marker arrives.

OP, if you are interested, search the forum for "started && ended".

PaulS,

I was very interested in your reply, so I did as you suggested, searched for "started && ended", with and without quotes and got nothing. Any other input you can give me for OP?

Tom

Tomroth,

Give this a read, see if it helps clear things up for you.

Thanks, jraskell!!!

I'll modify my code to see if it makes a difference. It wasn't that different from mine other than looking for a definite start and end of the string sent. I'll give it a try.

Thanks again!!!

Tom

P.S. - Is this YOUR blog? JHaskell, jraskell?

tomroth:
P.S. - Is this YOUR blog? JHaskell, jraskell?

Yes, my blog. Just started it today.