Hello,
For school i need to make a kind of windshield wiper system using a servo. The servo needs to turn to 180 degrees and back continuisly when a button is pressed once. But when i press the button again it needs to stop. Right now the script starts fine. the servo starts turning when i press the button but it won't turn off.
I think i need something like a loop function but i can't find anything that will help me.
Please post your code in a code block.
You are calling loop() from loop()! You should NEVER do that. This is called a recursive call and will crash your code eventually.
How can you see that ?
I don’t see any code !
this is the code i got so far
#include <Servo.h> // servo library adding
Servo ruitenwisser; // naming the servo
int sw1 = 7; // name the switch needed
int sw1State = 1; // name variables
int pos = 0; // make a variable for the servo
void setup() {
Serial.begin(9600);
ruitenwisser.attach(3); // connect the servo to pin 3
pinMode (sw1, INPUT_PULLUP); // make switch 1 a pullup input
}
void loop() {
//this part is the part where the trouble is.
sw1State = digitalRead(sw1);
if (sw1State == LOW) {
loop();
for (pos = 0; pos <= 180; pos += 1){ // gaat van 0 naar 180 graden in stappen van 4.1 graad
ruitenwisser.write(pos); // vertelt de servo om naar de positie te gaan
delay(15); // wacht 15ms voordat de motor terug gaat
}
for (pos = 180; pos >= 0; pos -= 1) { // gaat van 180 naar 0 graden in stappen van 4.1 graad
ruitenwisser.write(pos); // vertelt de servo om naar de positie te gaan
delay(15); // wacht 15ms voordat de motor terug gaat
}
}
}
this is the code i got so far:
This is the code i got so far
#include <Servo.h> // servo library adding
Servo ruitenwisser; // naming the servo
int sw1 = 7; // name the switch needed
int sw1State = 1; // name variables
int pos = 0; // make a variable for the servo
void setup() {
Serial.begin(9600);
ruitenwisser.attach(3); // connect the servo to pin 3
pinMode (sw1, INPUT_PULLUP); // make switch 1 a pullup input
}
void loop() {
//this part is the part where the trouble is.
sw1State = digitalRead(sw1);
if (sw1State == LOW) {
loop();
for (pos = 0; pos <= 180; pos += 1){ // gaat van 0 naar 180 graden in stappen van 4.1 graad
ruitenwisser.write(pos); // vertelt de servo om naar de positie te gaan
delay(15); // wacht 15ms voordat de motor terug gaat
}
for (pos = 180; pos >= 0; pos -= 1) { // gaat van 180 naar 0 graden in stappen van 4.1 graad
ruitenwisser.write(pos); // vertelt de servo om naar de positie te gaan
delay(15); // wacht 15ms voordat de motor terug gaat
}
}
}
You can convert the switches action to act like a push ON push OFF switch.
On a change in state to pushed, set a Flag, on the next push to ON you reset that Flag etc.
Use the Fag to enable the motor sweeping action.
Goedenavond aan Nederland
Hello snagkuul
Your comment "wacht 15ms voordat de motor terug gaat" is misleading.
That is the time between old positie to new positie.
Delete
loop();
and test again.
Geniet van de avond en heb plezier