Let's Make Robots!

Arduino coding help

Hello I recently visited the arduino playground site to look how to control two servos and when i used thier code it brought up an error thing is anyone willing to post some code for me on controlling two servos please.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

There are 3 potential ways you can control servos with the Arduino.

  1. Software Servo library
  2. Hardware Servo Library
  3.  Hardcode

I always did the hardcode method just because I like it better. Here is an example of controlling a servo that way

 

 


// code written by Thomas Countz

/* Directions: This code is to control a servo connected to pin '9' on the Arduino.
After you upload the code to the arduino and connect the sevo to pin '9'
the servo should be centered. To control the servo, open the serial monitor
by hitting the button to the right of the 'upload' button in the Arduino application.
Send '8' to set the servo to it's max, 5 to center the servo, and 2 to set the
servo to it's min. These can be changed by changin the variables in the code. These
numbers were to be used on a number pad and have not been tested on the row of numbers
at the top of a QWERTY keyboard.*/


int servoPin = 9; // attach the data line of the servo to this pin
int minPulse = 600; // the min pulse of the servo
int maxPulse = 2400; // the max pulse of the servo
int moveServo; // user define variable
int refreshTime = 20; // sends the pulseWidth to the servo every 20ms
int centerServo; // pulseWidth that the servo need to be centered (determined by the code)       
int pulseWidth; // pulseWidth for the servo       
long lastPulse = 0; // a counter to determine when to send a pulse to the servo 

void setup() { 
  pinMode(servoPin, OUTPUT); // define the servoPin as an output
  centerServo = maxPulse - ((maxPulse - minPulse)/2); // calculate centerServo
  pulseWidth = centerServo; // set the servo to center or it will float 
  Serial.begin(9600); // allow for a serial connection


void loop() { 
  if (Serial.available() > 0) {  // if a serial connection is available
  moveServo = Serial.read();} // determine the valuse of 'moveServo' base on the serial connection
  if (moveServo == 56) { pulseWidth = maxPulse; }  // When you send '8' the servo will go to 'maxPulse'
  if (moveServo == 53) { pulseWidth = centerServo; } // When you send '5' the servo will go to 'centerServo'
  if (moveServo == 50) { pulseWidth = minPulse; } // When you send '2' the servo will go to 'minPulse'

  if (pulseWidth > maxPulse) { // This if statement is a safety, just so the servo doesn't go past it's max pulse width
    pulseWidth = maxPulse;
  } 
  if (pulseWidth < minPulse) { // This if statement is a satety, just so the servo doesn't go past it's min pulse
    pulseWidth = minPulse;
  }   
  if (millis() - lastPulse >= refreshTime) {  // if 20ms has gone by
    digitalWrite(servoPin, HIGH); // send 5v to the servoPin
    delayMicroseconds(pulseWidth); // for 'pulseWidth' us
    digitalWrite(servoPin, LOW); // send 0v to servoPin
    lastPulse = millis(); // update lastPulse          
  }
}

// Code written by Thomas Countz

 

 


IF THAT DOESN'T WORK, PLEASE LET ME KNOW.

 

I like to control servos this way because it allow for an explaination of how servos work. A lot of people would advice against this because, among other reasons, it takes up a lot of space. I do it anyway because I like it. I don't know what to tell you about what you tried, but maybe if you supply us with the error, we can help more.

Hope this helped

-Thomas Countz

 

 

 

What was the error? Post the code you were trying to run and we will take a look at it. My 4-servo walker use the arduino and the Servo library. It is here on LMR (too lazy to find the link) and the code is there too.