#include <Servo.h>
Servo brakeServo; // Create a servo object
int servoPin = 9; // Pin where the servo is connected
int brakePosition = 0; // Neutral position (adjust based on your setup)
//add another brakeposition for -45
int brakeAngle = 45; // Braking angle
unsigned long previousMillis = 0; // Store the last time the brake was activated
unsigned long brakeDuration = 5000; // Time the brake is applied (in milliseconds)
unsigned long releaseTime = 1000; // Time between brake applications
void setup() {
brakeServo.attach(servoPin); // Attach the servo to the pin
brakeServo.write(brakePosition); // Set the servo to the neutral position
}
void loop() {
unsigned long currentMillis = millis(); // Get current time
// Apply brake for a certain duration
if (currentMillis - previousMillis >= releaseTime) {
brakeServo.write(brakeAngle); // Apply the brake (adjust angle as needed)
previousMillis = currentMillis; // Update last brake application time
}
// Release brake after brakeDuration
if (currentMillis - previousMillis >= brakeDuration && brakeServo.read() == brakeAngle) {
brakeServo.write(brakePosition); // Return to neutral position
}
}