Created
February 6, 2024 22:02
-
-
Save maagmirror/306c782f252f510cc368da88a01fed73 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Servo.h> | |
#include <ezButton.h> | |
// Constants | |
const int BUTTON_PIN_1 = 7; // Arduino pin connected to the first button's pin | |
const int BUTTON_PIN_2 = 6; // Arduino pin connected to the second button's pin | |
const int SERVO_PIN = 8; // Arduino pin connected to servo motor's pin | |
const int LED_PIN = 13; // Arduino pin connected to the LED | |
// Create ezButton objects for both buttons | |
ezButton button1(BUTTON_PIN_1); | |
ezButton button2(BUTTON_PIN_2); | |
Servo servo; // create servo object to control a servo | |
int angle = 0; // the current angle of the servo motor | |
int toangle = 180; | |
bool servoActive = false; // flag to track if the servo is currently active | |
void setup() { | |
Serial.begin(9600); // initialize serial | |
servo.attach(SERVO_PIN); // attaches the servo on pin 8 to the servo object | |
pinMode(LED_PIN, OUTPUT); // set LED pin as output | |
servo.write(angle); | |
} | |
void loop() { | |
button1.loop(); // MUST call the loop() function for the first button | |
// button2.loop(); // MUST call the loop() function for the second button | |
// Check the first button | |
if (button1.isPressed()) { | |
delay(50); // Pequeño retraso para evitar rebotes | |
if (button1.isPressed()) { // Verificar nuevamente si el botón sigue presionado | |
// Change angle of servo motor | |
if (!servoActive) { | |
angle = toangle; | |
servo.write(angle); | |
delay(500); // Pequeño retraso para permitir que el servo se mueva | |
servoActive = true; | |
} else { | |
angle = 0; | |
servo.write(angle); | |
delay(500); // Pequeño retraso para permitir que el servo se mueva | |
servoActive = false; | |
} | |
} | |
} | |
// Check the second button | |
if (button2.isPressed()) { | |
delay(50); // Pequeño retraso para evitar rebotes | |
if (button2.isPressed()) { // Verificar nuevamente si el botón sigue presionado | |
// Change angle of servo motor | |
if (!servoActive) { | |
angle = toangle; | |
servo.write(angle); | |
delay(500); // Pequeño retraso para permitir que el servo se mueva | |
servoActive = true; | |
} else { | |
angle = 0; | |
servo.write(angle); | |
delay(500); // Pequeño retraso para permitir que el servo se mueva | |
servoActive = false; | |
} | |
} | |
} | |
// Check if servo is at toangle (with a tolerance) and turn on LED | |
if (servoActive && abs(servo.read() - toangle) <= 2) { | |
digitalWrite(LED_PIN, HIGH); // Turn on LED | |
} else { | |
digitalWrite(LED_PIN, LOW); // Turn off LED | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment