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