3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Arduino based serial servo controller

Step 3Program the arduino

Program the arduino
now you can upload the sketch onto 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 StepDownload PDFView All StepsNext Step »
5 comments
Aug 11, 2010. 6:29 PMJanux says:
Hi there :) First, thanks!, for posting this mattie95, and regarding the issue that Dannne11 has (or had) to compile the code is because the first line of the code posted in the page says only " #include " instead of " #include <Servo.h> " (without quotes), but this should be added only if you copy the code from the page, if you download the file attached "radio_protocol_fix.pde", it should compile just fine. Just thought to point it out for someone else doing copy/paste from the code. :) Janux
Jul 19, 2010. 5:01 AMDannne11 says:
10: error: #include expects "FILENAME" or In function 'void setup()': At global scope: In function 'void setup()': Bad error line: -5 This is the code. posted the wrong code first srry
Jul 19, 2010. 4:53 AMDannne11 says:
the error code is "10: error: #include expects "FILENAME" or In function 'void setup()': At global scope: Bad error line: -3 "
Jul 18, 2010. 5:36 AMDannne11 says:
the code do not work with the arduino software :(

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
2
Followers
1
Author:mattie95