Hi, i know it may sound easy but not for me as I'm a noob. I couldnt find a solution to my project.
Basicly i want to turn a servo on, which is in "for" loop (servo sweep) by single clicking a push button and turn it off with another click. one click on, one click off. I want it to be working until the second press.
This was what i'm currently working on; but it make the servo sweeps for once only;
#include <Servo.h>
Servo myservo; // create Servo object to control a servo
// twelve Servo objects can be created on most boards
int pos = 0; // variable to store the servo position
int minAngle = 0;
int maxAngle = 130;
int servoState = 0;
int buttonPin = 12; // the number of the pushbutton pin
int servoPin = 8; // the number of the Servo pin
int buttonNew;
int buttonOld = 1;
int dt = 100;
void setup() {
Serial.begin(9600);
Serial.print("ButtonPin = ");
Serial.println(digitalRead(buttonPin));
Serial.print("ButtonOld = ");
Serial.println(digitalRead(buttonOld));
Serial.print("ButtonNew = ");
Serial.println(digitalRead(buttonNew));
delay(dt);
Serial.print("ServoState = ");
Serial.println(digitalRead(servoState));
Serial.print("ServoPin = ");
Serial.println(digitalRead(servoPin));
myservo.attach(servoPin);
pinMode(servoPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
buttonNew = digitalRead(buttonPin);
if (buttonOld == 0 && buttonNew == 1) {
if (servoState == 0) {
digitalWrite(servoPin, HIGH);
servoState = 1;
} else {
digitalWrite(servoPin, LOW);
servoState = 0;
}
do {
for (pos = minAngle; pos <= maxAngle; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(3); // waits 15 ms for the servo to reach the position
}
for (pos = maxAngle; pos >= minAngle; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(3);
}
}
while (digitalRead(servoPin) == HIGH);
}
buttonOld = buttonNew;
delay(dt);
}