106Views3Replies
Need help with changing arduino code for more servos
This code makes a continuous rotation servo motor turn on and run until you let go of the button. I need to change this sketch so that there are 3 buttons, instead of 1, and if I press 1, a continuous rotation servo would turn for each button until I let go of the button.
#include <Servo.h>
Servo myServo;
int servoPin = 2;
int buttonPin = 7;
int buttonState = 0;
void setup() {
// put your setup code here, to run once:
pinMode(buttonPin, INPUT);
myServo.attach(servoPin);
}
void loop() {
// put your main code here, to run repeatedly:
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
myServo.write(0);
}else{
myServo.write(90);
}
delay(15);
}
Discussions
Best Answer 4 years ago
I have not tested it since i have no Servos.
#include <Servo.h>
/* Group together the information you want to keep track of for a servo */
struct servos{
Servo servo;
int servoPin;
int buttonPin;
int buttonState;
};
/* Create 3 servos with extra info */
struct servos myServo[ 3 ];
int myServoCnt;
int i;
void setup() {
/* Fill in the extra info for each servo */
myServo[ 0 ].servoPin = 2;
myServo[ 0 ].buttonPin = 7;
myServo[ 0 ].buttonState = 0;
myServo[ 1 ].servoPin = 3;
myServo[ 1 ].buttonPin = 8;
myServo[ 1 ].buttonState = 0;
myServo[ 2 ].servoPin = 4;
myServo[ 2 ].buttonPin = 9;
myServo[ 2 ].buttonState = 0;
/* calculate the number of array elements */
myServoCnt = sizeof( myServo ) / sizeof( struct servos );
/* setup the button pins as inputs and attatch the servo pin to the Servo */
for( i = 0; i < myServoCnt; i++ ){
pinMode( myServo[ i ].buttonPin, INPUT );
myServo[ i ].servo.attach( myServo[ i ].servoPin );
}
}
void loop() {
/* Read your buttons ... */
for( i = 0; i < myServoCnt; i++ )
myServo[ i ].buttonState = digitalRead( myServo[ i ].buttonPin );
/* ... and turn the coresponding servo on ( 0 ) or off ( 90 ) */
for( i = 0; i < myServoCnt; i++ )
if( myServo[ i ].buttonState == HIGH )
myServo[ i ].servo.write( 0 );
else
myServo[ i ].servo.write( 90 );
delay( 15 );
}
4 years ago
Ok, thankyou
4 years ago
This is a bold guess since i dont know about your Servos but
myServo[ i ].servo.write( 90 )
equals to
myServo[ i ].servo.writeMicroseconds( 1500 )
libraries/Servo/src/Servo.h defines
#define MIN_PULSE_WIDTH 544 // the shortest pulse sent to a servo
#define MAX_PULSE_WIDTH 2400 // the longest pulse sent to a servo
#define DEFAULT_PULSE_WIDTH 1500 // default pulse width when servo is attached
#define REFRESH_INTERVAL 20000 // minumim time to refresh servos in microseconds
i assume that the non moving position of your servo is not 1500 but slightly more or less, depending on if it moves towards maximum or minumum position.
add
int idlePos;
to the struct, initialize all to
myServo[ i ].idlepos = 1500;
replace
myServo[ i ].servo.writeMicroseconds( myServo[ i ].idlepos )
and tweak
myServo[ i ].idlepos = x;
until it stops moving.
4 years ago
ritterst, thanks for the code and for answering so quickly. I've tested it out but I'm having a small problem though. Two of the servos work perfectly fine but one of them rotates very slowly, even if I'm not pushing down the button. When I do push the button, the servo works like the other motors. I don't know if its a problem with the code or the way I connected everything. Please help me, thanks