/*
This code is made for WDMC to control the speed of main former DC motor
the servo is connected to the pot of pwm singal genrator
connect the another relay paraller with main control relay
when relay gets on the servo changes the angle slowly and the relay gets opened
to NC positio then pot come back to ist orioginal position
*/
#include <Servo.h>
const int servoPin = 9; // Servo signal pin
const int buttonPin = 2; // Push button pin
const int buttonDelay = 50; // Delay to debounce the button (in milliseconds)
const int servoIncrement = 1; // Angle increment for each iteration
const int servoDelay = 5; // Delay between angle increments (in milliseconds)
Servo servo;
int currentAngle = 1; // To change the starting Angle of servo
bool buttonPressed = false;
void setup() {
servo.attach(servoPin);
pinMode(buttonPin, INPUT_PULLUP);
servo.write(currentAngle); // Set initial position to 0 degrees
}
void loop() {
if (digitalRead(buttonPin) == LOW && !buttonPressed) {
buttonPressed = true;
increaseAngle();
}
if (digitalRead(buttonPin) == HIGH && buttonPressed) {
buttonPressed = false;
returnToOriginalPosition();
}
}
void increaseAngle() {
while (digitalRead(buttonPin) == LOW && currentAngle < 180) {
currentAngle += servoIncrement;
servo.write(currentAngle);
delay(servoDelay); // write a servo delay for angle increment for
}
}
void returnToOriginalPosition() {
while (currentAngle > 0) {
currentAngle -= servoIncrement;
servo.write(currentAngle);
delay(servoDelay); // write a servo delay for return servo to 0 insted od servoDelay
}
}