#include <Servo.h>
Servo brakeServo; // Create a servo object
int servoPin = 9; // Pin where the brake servo is connected
int brakePosition = 0; // Neutral (off) position
int brakeAngle = 45; // Braking angle
void setup() {
brakeServo.attach(servoPin); // Attach the brake servo to the pin
brakeServo.write(brakePosition); // Start with brake off (neutral position)
}
void loop() {
// Check if the brake (servo) is in braking position
if (isBrakeActivated()) {
stopMotor(); // Stop the motor when brake is activated
} else {
moveMotor(); // Allow motor movement when brake is not activated
}
}
// Function to check if the servo is at the braking angle
bool isBrakeActivated() {
int currentAngle = brakeServo.read(); // Read the current angle of the servo
return currentAngle == brakeAngle; // Return true if the servo is in the braking position
}
// Function to stop the motor when the brake is applied
void stopMotor() {
brakeServo.write(brakeAngle); // Move the servo to the braking angle (engages brake)
}
// Function to allow motor movement when the brake is not pressed
void moveMotor() {
brakeServo.write(brakePosition); // Move the servo to neutral position (releases brake)
}