hi guys im using winch servos to move the controls on a device. Im using a modified 'knob' sketch. Ive already had some help from people on this forum and am greatfull! But i need your help once more - when the device is turned on, the servo motors move to the value given to them by the analoge input using the potpin and val functions in the code. I want to find a way of ordering the servos to move to position zero ( as a pwm value 1000 in this code) as they are powered up or as the code is initiated. I thought perhaps using a section of the servo library 'sweep' sketch underlined below:
[u]int pos = 0; // variable to store the servo position[/u]
[s]void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
} [/s]
[u]void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
} [/u]
if i put this whole section into the setup rather than the loop can i command it to move to a certain point before it recives a reading from the potentiometer, or am i barking up the wrong tree? Here is the current code :
#include <Servo.h>
Servo myservo1; // create servo object to control a servo
Servo myservo2;
Servo myservo3;
Servo myservo4;
int potpin = 0; // analog pin used to connect the potentiometer
int val1; // variable to read the value from the analog pin
int potpin2 = 1; // analog pin used to connect the potentiometer
int val2; // variable to read the value from the analog pin
int potpin3 = 2; // analog pin used to connect the potentiometer
int val3; // variable to read the value from the analog pin
int potpin4 = 3; // analog pin used to connect the potentiometer
int val4; // variable to read the value from the analog pin
void setup()
{
myservo1.attach(9); // attaches the servo on pin 9 to the servo object
myservo2.attach(10); // attaches the servo on pin 10 to the servo object
myservo3.attach(5); // attaches the servo on pin 11 to the servo object
myservo4.attach(6); // attaches the servo on pin 12 to the servo object
}
void loop()
{
val1 = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val1 = map(val1, 0, 1023, 1000, 1925); // scale it to use it with the servo (value between 0 and 180)
myservo1.writeMicroseconds(val1); // sets the servo position according to the scaled value
// waits for the servo to get there
val2 = analogRead(potpin2); // reads the value of the potentiometer (value between 0 and 1023)
val2 = map(val2, 0, 1023, 1000, 1925); // scale it to use it with the servo (value between 0 and 180)
myservo2.writeMicroseconds(val2); // sets the servo position according to the scaled value
val3 = analogRead(potpin3); // reads the value of the potentiometer (value between 0 and 1023)
val3 = map(val3, 0, 1023, 1000, 1925); // scale it to use it with the servo (value between 0 and 180)
myservo3.writeMicroseconds(val3); // sets the servo position according to the scaled value
val4 = analogRead(potpin4); // reads the value of the potentiometer (value between 0 and 1023)
val4 = map(val4, 0, 1023, 1000, 1925); // scale it to use it with the servo (value between 0 and 180)
myservo4.writeMicroseconds(val4); // sets the servo position according to the scaled value
delay(15);
}