Hello everyone, I want to turn a servo for certain number of times then stop. I was able to write a code that works but when I add a button, that is the servo should turn only when the button is pressed, this to be hard for me to figure out to write it correctly.
The code below(without button) works fine
#include <VarSpeedServo.h>
VarSpeedServo myservo;
const int servoPin = 9;
int count = 0;
int readcount;
void setup() {
myservo.attach(servoPin); // attaches the servo on pin 9 to the servo object
myservo.write(0, 255, true); // set the intial position of the servo, as fast as possible, wait until done
}
void loop() {
count++;
readcount = count;
myservo.write(180, 100, true); // move the servo to 180, max speed, wait until done
// write(degrees 0-180, speed 1-255, wait to complete true-false)
myservo.write(0, 100, true); // move the servo to 180, slow speed, wait until done
if (readcount == 3)
{
myservo.detach();
count = 0;
}
}
This is the not working code
#include <VarSpeedServo.h>
VarSpeedServo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int Button_pin3 = 8;
const int servoPin = 9; // the digital pin used for the servo
int Button_Pressed3;
int count = 0;
int readcount;
void setup() {
myservo.attach(servoPin); // attaches the servo on pin 9 to the servo object
myservo.write(0, 255, true); // set the intial position of the servo, as fast as possible, wait until done
pinMode(Button_pin3, INPUT);
}
void loop() {
Button_Pressed3 = digitalRead(Button_pin3);
count++;
readcount = count;
if (Button_Pressed3 == HIGH)
{
if (pressBt == true)
{
myservo.write(180, 100, true); // move the servo to 180, max speed, wait until done
// write(degrees 0-180, speed 1-255, wait to complete true-false)
myservo.write(0, 100, true); // move the servo to 180, slow speed, wait until done
pressBt = false;
}
if (readcount == 3)
{
myservo.detach();
count = 0;
}
}
}
Thanks