hello i've been toying around with stepper motors and found they would be too hard for me so i have decided to try with a servo.
im trying to get a servo to drive from 0 - 180 degrees with the press of a button, first button driving - direction till 0 degrees and second button driving + direction too 180 degrees
i can get the servo to drive from 0 - 180 degrees but i want it to do this 1 step at a time rather then telling it to go from 0 - 180 degrees. this way i would be able to stop as soon as i let go of the button.
i am making an arm that can spin with 2 buttons forward and backwards so i need to be able to stop when i let go of the button and not go to predefined positions any help is greatly appreciated.
here is the code i have scrapped up so far.
#include <Servo.h>
const int forwardbuttonPin = 2; // the number of the pushbutton pin.
const int backwardbuttonPin = 3;
const int ledPin = 13; // the number of the LED pin.
// variables will change:
int forwardState = 0; // variable for reading the pushbutton status.
int backwardState = 0;
int pos = 0; // variable to store the servo position
Servo aaxisservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
void setup() {
aaxisservo.attach(11); // attaches the servo on pin 9 to the servo object
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output.
pinMode(forwardbuttonPin, INPUT); // initialize the pushbutton pin as an input.
pinMode(backwardbuttonPin, INPUT);
aaxisservo.write(pos=90);
}
void loop(){
forwardState = digitalRead(forwardbuttonPin); // read the state of the pushbutton value.
backwardState = digitalRead(backwardbuttonPin);
if (forwardState == HIGH) { // check if the pushbutton is pressed.
digitalWrite(ledPin, HIGH); // turn LED on.
aaxisservo.write(pos=pos+1); // tell servo to go to position
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
if (backwardState == HIGH) {
digitalWrite(ledPin, HIGH);
aaxisservo.write(pos=pos-1); // tell servo to go to position
}
else {
digitalWrite(ledPin, LOW);
}
}
cheers ash.