Controlling servo speed with Arduino
Ok for the Tempest SXR mod, I had a comment on one of the sites it was featured on (Engadget IIRC) that it'd be neat if the vents opened and closed slower to give a more dramatic effect. I took a look at Arduino's website on servo control, but can't find anything on how to set servo speed. Anyone know how? :think:
Re: Controlling servo speed with Arduino
You can only set position, however you can make it take steps down to the final position with a small delay (a few ms) in between each step. This should give the desired results.
EDIT: If you post the code you're currently running, I can quickly modify it.
Re: Controlling servo speed with Arduino
here's what I'm running currently:
Code:
// Controlling a servo position using a temperature sensor
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
// edited 5-12-2010 by Will Lyon to include base setting for servo if voltage not detected on pin 7
// edited again 7-4-2010 by crenn to simplify the code a little
// edited yet again 7-5-2010 by crenn to add features
// edited again 7-21-2010 by Will Lyon - recalibrated servo positions
#include <Servo.h>
Servo myservo; // create servo object to control a servo
//Constants
const unsigned char CONTROL = 7; // digital pin used to detect if the system is on or off
const unsigned char temps = 0; // analog pin used to connect the temp sensor
const unsigned char MAX_VAL = 10;
//Main global varibles
char trigger = 0; // varible used to store the control pin value
unsigned int val; // variable to read the value from the analog pin
unsigned int updateAvgtemp(){
static int history[MAX_VAL]={0};
static unsigned char lastHist=0;
static unsigned char numHist=0;
unsigned int temp=0;
unsigned char counter=0;
unsigned char arcount=0;
history[lastHist] = analogRead(temps);
if(numHist<MAX_VAL)
++numHist;
arcount=lastHist;
++lastHist;
if(lastHist>=MAX_VAL)
lastHist=0;
temp=0;
counter=0;
do{
temp+=history[arcount];
arcount--;
if(arcount>MAX_VAL)
arcount=(MAX_VAL-1);
counter++;
}while(counter < numHist);
return (temp/numHist);
}
void setup()
{
pinMode (CONTROL, INPUT); // sets the control pin to input
myservo.attach(9); // attaches the servo on pin 9 to the servo object
digitalWrite(CONTROL, LOW); // ensure internal pullup resistor is disabled.
}
void loop()
{
trigger = digitalRead(CONTROL); // read input of pin CONTROL and store it
if (trigger == HIGH){ // reads if pin CONTROL, if true, do this:
val = updateAvgtemp(); // read the value of the temp sensor (value with range of 1024)
val = map(val, 350, 700, 160, 80); // scale it to use it with the servo (value between 160 and 80)
myservo.write(val); // sets the servo position according to the scaled value
}
else {
myservo.write(180); // sets servo position to 180 if above statment is false
}
delay(125); // wait 25ms for the servo to move to it's new position and also 100ms until it gets the new value
}
Re: Controlling servo speed with Arduino
Code:
// Controlling a servo position using a temperature sensor
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
// edited 5-12-2010 by Will Lyon to include base setting for servo if voltage not detected on pin 7
// edited again 7-4-2010 by crenn to simplify the code a little
// edited yet again 7-5-2010 by crenn to add features
// edited again 7-21-2010 by Will Lyon - recalibrated servo positions
// edited again 10-15-2010 by crenn - slow down servo when computer is off
#include <Servo.h>
Servo myservo; // create servo object to control a servo
//Constants
const unsigned char CONTROL = 7; // digital pin used to detect if the system is on or off
const unsigned char TEMP_SENSOR = 0; // analog pin used to connect the temp sensor
const unsigned char MAX_SAMPLES = 10;
const unsigned char OFF_POSITION = 180;
const unsigned char LOW_POSITION = 160;
const unsigned char HIGH_POSITION = 80;
//Main global varibles
char trigger = 0; // varible used to store the control pin value
unsigned int val = OFF_POSITION; // variable to read the value from the analog pin
unsigned int updateAvgtemp(){
static int history[MAX_SAMPLES]={0};
static unsigned char lastHist=0;
static unsigned char numHist=0;
unsigned int temp=0;
unsigned char counter=0;
unsigned char arcount=0;
history[lastHist] = analogRead(TEMP_SENSOR);
if(numHist<MAX_SAMPLES)
++numHist;
arcount=lastHist;
++lastHist;
if(lastHist>=MAX_SAMPLES)
lastHist=0;
temp=0;
counter=0;
do{
temp+=history[arcount];
arcount--;
if(arcount>MAX_SAMPLES)
arcount=(MAX_SAMPLES-1);
counter++;
}while(counter < numHist);
return (temp/numHist);
}
void setup()
{
pinMode (CONTROL, INPUT); // sets the control pin to input
myservo.attach(9); // attaches the servo on pin 9 to the servo object
digitalWrite(CONTROL, LOW); // ensure internal pullup resistor is disabled.
}
void loop()
{
trigger = digitalRead(CONTROL); // read input of pin CONTROL and store it
if (trigger == HIGH){ // reads if pin CONTROL, if true, do this:
val = updateAvgtemp(); // read the value of the temp sensor (value with range of 1024)
val = map(val, 350, 700, LOW_POSITION, HIGH_POSITION); // scale it to use it with the servo (value between 160 and 80)
myservo.write(val); // sets the servo position according to the scaled value
}
else {
while(val<OFF_POSITION){
val++;
myservo.write(val);
delay(100);
}
myservo.write(OFF_POSITION); // sets servo position to 180 if above statment is false
}
delay(125); // wait 25ms for the servo to move to it's new position and also 100ms until it gets the new value
}
Here you go!
Re: Controlling servo speed with Arduino
thanks! I'll try it out tonight :D
I'm thinking I mgiht also add in a line to blink an LED (4 actually), and set some amber ones up so as it's closing (when the computer get shut down) the LEDs will blink slowly, like warning lights almost. we'll see though. I don't want it to get too corny :think:
Re: Controlling servo speed with Arduino
Re: Controlling servo speed with Arduino
IDK if this would work, but would work, but would it be possible to either reduce the voltage to the servo to slow it down? I think crenn's way is simpler, I'm just wondering if that'll make the movement jerky at all?
Re: Controlling servo speed with Arduino
well it slowed it down a tad, but not much at all. how could I alter it to slow it more?
and no jitter :D
Re: Controlling servo speed with Arduino
Quote:
Originally Posted by
SXRguyinMA
well it slowed it down a tad, but not much at all. how could I alter it to slow it more?
and no jitter :D
You should be able to increase the pause time between movements to slow it down. Good to hear it's smooth.
Re: Controlling servo speed with Arduino
would I increase the 100ms delay after the "myservo.write" or the 125ms delay at the end? :?