Step 3Program the arduino
Here is a simple breakdown to the code:
#include
Servo myservo; // create servo object to control a servo
Servo myservo1;
int incomingByte = 0, datacount = 0, counter = 0, ready = 0; // for incoming serial data
char data[10];
const char verify[8] = "ma11hew";
char command[3];
void setup() {
myservo.attach(9);
myservo1.attach(10);
Serial.begin(38400); // opens serial port, sets data rate
Serial.println("Hi Arduino Here!"); // added to help identify the serial port
This just sets up the serial port and servos.
int i;
for(i = 0; i<180;i++)
{
myservo.write(i);
delay(15);
}
myservo.write(5);
for(i = 0; i<180;i++)
{
myservo1.write(i);
delay(15);
}
myservo1.write(5);
}
Simple sweeping movement to verify the servos work correctly.
void loop() {
ready = 0;
counter = 0;
while (1==1)
{
if (Serial.read() != verify[counter])
{
break;
}
if(counter == 6)
{
delay(20);
command[0] = Serial.read();
command[1] = Serial.read();
//if (Serial.read() == ((command[1] * 12) % 8))
// {
ready = 1;
//}
Serial.println("saved command");
}
counter ++;
delay(2);
}
this checks the serial buffer for the correct authorization string then grabs two bytes for the command.
the commented if statement allows for a makeshift checksum but would make manual interfacing hard.
ready can be set to 0 so commands will not be parsed such as in the case of corrupted data.
//search through commands
if (ready == 1)
{
if (command[0] == 'T')
{
command[0] = 0;
Serial.print("throttle control on pin 9 to: ");
Serial.println(map(command[1], 32, 126, 2, 180), DEC);
myservo.write(map(command[1], 32, 126, 2, 180));
}
if (command[0] == 'S')
{
command[0] = 0;
Serial.print("throttle control on pin 10 to: ");
Serial.println(map(command[1], 32, 126, 2, 180), DEC);
myservo1.write(map(command[1], 32, 126, 2, 180));
}
}
}
the rest of the code is to search command[] for valid commands (T or S.)
if either match it takes the next byte and sends it to the servo.
more on the map(command[1], 32, 126, 2, 180) later...
the code here is expandable for whatever else you may need (eg. lights, motors, IR, etc.)
this code should work fine with no modifications.
| « Previous Step | Download PDFView All Steps | Next Step » |
5
comments
|
Add Comment
|
![]() |
Add Comment
|











































