/*
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int servo_angle = 0;
unsigned long start_Time_servo = 0;
unsigned long interval_servo = 20;
const int servopin = 9;
const int taster = 7;
bool statusTaster = HIGH;
bool statusTasterneu = LOW;
void setup() {
Serial.begin(9600);
myservo.attach(servopin);
pinMode(taster, INPUT);
}
void loop() {
unsigned current_Time = millis();
statusTaster = digitalRead(taster);
if(statusTaster != statusTasterneu) {
if (current_Time - start_Time_servo >= interval_servo) {
start_Time_servo = current_Time;
myservo.write(servo_angle);
servo_angle++;}
if (servo_angle == 180) {
myservo.write(servo_angle);
servo_angle--;
statusTasterneu = statusTaster;
}
Serial.println(servo_angle);
statusTasterneu = statusTaster;
Serial.println(statusTaster);
}
}*/
#include <Servo.h>
#include <ezButton.h>
// constants won't change
const int BUTTON_PIN = 7; // Arduino pin connected to button's pin
const int SERVO_PIN = 9; // Arduino pin connected to servo motor's pin
ezButton button(BUTTON_PIN); // create ezButton object that attach to pin 7;
Servo servo; // create servo object to control a servo
unsigned long start_Time_servo = 0;
unsigned long interval_servo = 20;
// variables will change:
int angle = 0; // the current angle of servo motor
void setup() {
Serial.begin(9600); // initialize serial
button.setDebounceTime(25); // set debounce time to 50 milliseconds
servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object
servo.write(angle);
}
void loop() {
button.loop(); // MUST call the loop() function first
unsigned current_Time = millis();
if(button.isPressed()) {
Serial.println("The button is pressed");
if (current_Time - start_Time_servo >= interval_servo) {
start_Time_servo = current_Time;
angle++;}
if (angle== 180) {
angle--;
}
/*
// change angle of servo motor
if(angle == 0)
angle = 90;
else
if(angle == 90)
angle = 0;
*/
// control servo motor arccoding to the angle
servo.write(angle);
}
}