Testing 3 DOF gripper from Bajdi on Vimeo.
I started writing a sketch to control my 3 DOF gripper, it has 3 servos controlled by an Arduino. All the servo example sketches that I have seen use the delay function. You need to have a delay after sending the servo the command to move to a certain position. The delay function is the easiest way to do this, but this stops the code running on the Arduino. You can not do anything else during that delay. When you have a lot of delays in your program you will end up with a very unresponsive program. This is an issue I have seen many times on the Arduino forum.
As I will use a remote control to move the servos I want instant reaction of my servos. So I had to come up with a sketch to control a servo without the delay function. To do that you can use the millis function instead, this is a timer that runs continuously on the Arduino. The Arduino IDE comes with the “blink without delay” example, this example uses the millis function to blink a LED. So I integrated this example with a servo example I found on the Arduino forum. To test it I connected the 3 servos of my gripper to my spare Arduino Mega 2560 together with 3 potentiometers.
I powered the 3 servos from my lab power supply. Don’t try to power servos directly from an Arduino, the regulator on the Arduino is not powerful enough. You can control many servos with an Arduino but not power them. Always make sure you have a common ground if you’re using a different power supply to power the Arduino and servos. For example when you use a battery to power the servos and use an USB cable to power the Arduino you must connect the ground of the Arduino with the ground of the battery. Else your servos won’t be working or will do funny things.
Here is the sketch I have come up with to control one servo with a potentiometer without using the delay function:
/* http://www.bajdi.com Moving a servo without the delay function. The servo is controlled by a potentiometer connected to A0. */ #include <Servo.h> Servo myservo; // create servo object to control a servo int potValue; //variables to hold A0 input int iOldPos, iNewPos = 0; // servo position long previousMillis = 0; const int interval = 20; int pot = 0; //potentiometer connected to A0 void setup() { //Initialize serial port Serial.begin(9600); Serial.println("Servo Demo"); // Initialize servo myservo.attach(9); // attaches the servo on pin 9 } void loop() { potValue = analogRead(pot); // read the value of the potentiometer iNewPos = potValue/5; // convert to quasi-degrees (alter per your potentiometer) unsigned long currentMillis = millis(); if(iOldPos != iNewPos && currentMillis - previousMillis > interval) { // Issue command only if desired position changes previousMillis = currentMillis; iOldPos = iNewPos; Serial.print("Potentiometer = "); // Value of potentiometer on serial port Serial.print(potValue); Serial.print(", ~degrees = "); Serial.println(iNewPos); // Set servo in degrees from approximately 0 to 180 myservo.write(iNewPos); // tell servo to go to position in variable 'iNewPos' } }
[…] a sketch to control a servo with a potentiometer without using the delay function, you can find it here. This time I wanted to move or sweep a servo between 2 positions. The Arduino IDE comes with a […]
plz how can i do this for 5 servos
Have a look at the code of my Bob robot: http://www.bajdi.com/bob-the-biped/