Hi guys, for a school project (Fontys ABP8) i need to make a working windshield wiper system using a servo motor. I have five different states it needs to do:
- completely off and back to starting position, button 1
- wiping one time, button 2 one click.
- wiping with an interval, button 2 two clicks.
- continious wiping, button 3 one click.
- continious wiping with high speed, button 3 two clicks.
I need to use only three buttons, so use the oneButton library.
The code and board works, but it needs to be able to wipe continiously and change from state when a button is pressed, even if it is in a different state.I've got tips to change the delays for millis, but i dont know how to use them in this code.
here is the code.
//ruitenwisser sketch knoppen
#include "OneButton.h" //oneButton library oproepen
OneButton button(A1, true); //knoppen benoemen
OneButton button2(A0, true);
OneButton button3(A2, true);
#include <Servo.h> //servo library oproepen
Servo myservo; //servo benoemen
int pos = 0; //start positie van servo
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period_interval = 2700;
const unsigned long period_50 = 50;
const unsigned long period = 1000;
const unsigned long period_15 = 15
void setup(){
//Input en output aangeven
myservo.attach(9);
pinMode(13,OUTPUT);
button.attachClick(een_keer);
button.attachDoubleClick(interval);
button2.attachClick(continu);
button2.attachDoubleClick(continuversneld);
button3.attachClick(alles_uit);
startMillis = millis();
}
void loop() {
//knoppen lezen en tellen hoeveel keer geklikt
button.tick();
button2.tick(); //variabele onthouden, knop vast houden en delays veranderen bijv millis
button3.tick();
delay(50);
}
void alles_uit(){ //bij alles uit gaat de ruitenwisser terug naar start positie
Serial.print("alles uit");
myservo.write(0);
}
void een_keer(){ //met een lage snelheid een keer heen en weer
Serial.print("een keer");
for (pos = 0; pos <= 180; pos += 3) {
// in steps of 3 degree
myservo.write(pos);
delay(15);
}
for (pos = 180; pos >= 0; pos -= 3) {
myservo.write(pos);
delay(15);
}
}
void interval(){ //bij interval met een lage snelheid een keer, dan 2,7 sec wachten
Serial.print("interval");
for(int n=1; n<3; n++){
for (pos = 0; pos <= 180; pos += 3) {
// in steps of 1 degree
myservo.write(pos);
delay(15);
}
for (pos = 180; pos >= 0; pos -= 3) {
myservo.write(pos);
delay(15);
}
delay(2700);
}
}
void continu(){
Serial.print("continu");
for(int n=1; n<3; n++){
for (pos = 0; pos <= 180; pos += 3) {
// in steps of 1 degree
myservo.write(pos);
delay(15);
}
for (pos = 180; pos >= 0; pos -= 3) {
myservo.write(pos);
delay(15);
}
}
}
void continuversneld(){
Serial.print("continuversneld");
for(int n=1; n<10; n++){
for (pos = 0; pos <= 180; pos += 15) {
// in steps of 1 degree
myservo.write(pos);
delay(50);
}
for (pos = 180; pos >= 0; pos -= 15) {
myservo.write(pos);
delay(50);
}
}
}