Servo motor program
often when ask question ends being obvious, right not obvious me. trying increment servo motor 1 degree (0-180) every time button pushed (buttonpin). have led (ledpin) attached indicator tells me program seeing button pushed. there no errors in program not working way want. instead of increment 1 degree per button push servo motor moves 180 degrees , resets 0.
below code.
thank you.
below code.
code: [select]
const int buttonpin = 2;
const int ledpin = 4;
int buttonstate = 0;
int buttonpushcounter = 0;
int lastbuttonstate = 0;
#include <servo.h>;
servo myservo;
void setup() {
pinmode (buttonpin, input);
pinmode (ledpin, output);
myservo.attach(9);
}
void loop() {
buttonstate = digitalread (buttonpin);
if (buttonstate != lastbuttonstate) {
if (buttonstate == high) {
digitalwrite (ledpin, high);
buttonpushcounter++;}
(buttonpushcounter>-1;buttonpushcounter<180;buttonpushcounter +=1){
myservo.write(buttonpushcounter);
delay(10);}}
else {
digitalwrite (ledpin, low);
myservo.write(buttonpushcounter);
delay (10);
}
lastbuttonstate = buttonstate;
}
thank you.
it's because inside if goes when button state high, have whole sweep 0-180 for.
you need have line increments pos when goes there, , write new pos.
you need have line increments pos when goes there, , write new pos.
Arduino Forum > Using Arduino > Programming Questions > Servo motor program
arduino
Comments
Post a Comment