Windshield wiper with servo and oneButtons

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:

  1. completely off and back to starting position, button 1
  2. wiping one time, button 2 one click.
  3. wiping with an interval, button 2 two clicks.
  4. continious wiping, button 3 one click.
  5. 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);                       
    }
  }
}

Personally I always thought this article explained it best.YMMV
Multi-tasking the Arduino

after implement this, i think this is a challenging exercise.

i question the need for 3 buttons. couldn't one button sequence from one-time, to interval, to continuous, to high-speed and back to off?

but if 3 buttons are required, i think the 3rd button should also sequence thru the interval, continuous and high speed cases, otherwise the 2nd button needs to be pressed while the wipers are sweeping and before completing the sweep where the "state" is set back to "off".

i don't believe this multitasking. but the routine to sweep the wipers needs to allow the recognition of button presses and i believe is better if implemented as a routine that is repeatedly called and advances the servo one step and reverses direction at the endpoint allowing normal button recognition in between calls

having the wiper routine return the position is helpful to know when it is at the start position and no longer needs to be called. for the "once" case, the wiper routine would be repeated called until it returns to the 0th position at which point the "state" is set to "off"

the interval case needs a timer which is only used to call the wiper routine the first time. knowing the servo position is necessary in that state to know NOT to call the wiper routine if it is as the 0th position, relying the timer to call it to get is started

a timer is not needed for continuous or high speed states, the difference between them being the delay between servo steps which can be changed between sweeps

finally, there's a need for a stop state to continue calling the wiper routine until it reaches the 0th position and the state is set to "off"

Can you share schematic (circuit diagram)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.