Windshield wiper motor doesnt work

but the motor now only goes forward instead of 1sec forward,1sec backwards then waiting 1.8sec before resuming

To help you control the transitions in the operating states, it will be helpful to add some boolean control variables. See if this doesn't give you some ideas to work with.

boolean forward = false;
boolean backward =  false;
boolean stopt = false;

Then the initial state and the start time will be set when the condition is called with the button click function

void RuitenwisserInterval()
{
  //Alle loops uit zodat er tijdens dat er een andere loop gedaan wordt er zonder
  //op de alles uit knop gedrukt kan worden gewisseld kan worden
  buttonAllesUitstatus = LOW;
  buttonXmaalsnellerstatus = LOW;
  buttonSnelheid1status = LOW;

  //Start loop voor interval
  buttonIntervalstatus = HIGH;
  Serial.println("Ruitenwisser gaat met een interval aan");
  startMillis = millis();//add intial start time
  forward = true;
}

Then the forward/backward/stopt variables will control the timing in the enabled block.

if (buttonIntervalstatus == HIGH) //intial start time and state and was set before entry
  {

    if (forward == true)
    {
      digitalWrite(Hbrugaan, HIGH); // zet de H-brug aan
      analogWrite(MototAchteruit, 0);
      analogWrite(MotorVoorruit, Power);
    }

    if (forward == true and currentMillis - startMillis > aan )
    {
      forward = false;
      backward = true;
      //startMillis = millis();
      //motor beweegt achteruit
      analogWrite(MotorVoorruit, 0);
      analogWrite(MototAchteruit, Power);
    }
    
    if (backward == true and currentMillis - startMillis > 2 * aan)
    {
      backward = false;
      stopt = true;
      analogWrite(MotorVoorruit, 0);
      analogWrite(MototAchteruit, 0);
      digitalWrite(Hbrugaan, LOW);
    }

    if (stopt == true and  currentMillis - startMillis > (2 * aan) + wacht)
    {
      stopt = false;
      forward = true;
      startMillis = millis();
    }


    buttonAllesUit.tick();
    buttonInterval.tick();
    buttonXmaalsneller.tick();
    buttonSnelheid1.tick();
    delay(10);

  }
//Onebuttons aanmaken
#include "OneButton.h"               //roept de OneButton library aan zodat deze gebruikt kan worden
OneButton buttonAllesUit(9, true); // OneButton variabelenaam(pinnummer,aan of uit)
OneButton buttonInterval(8, true);
OneButton buttonXmaalsneller(12, true);
OneButton buttonSnelheid1(13, true);



// Constantes
const int Hbrugaan        = 2;
const int MotorVoorruit   = 10;
const int MototAchteruit  = 11;
const unsigned long aan   = 1000;
const unsigned long wacht = 1800;

// Variabelen
int Power = 62;                    //snelheid waarmee de motor rond draait
int buttonAllesUitstatus     = 0;
int buttonIntervalstatus     = 0;
int buttonXmaalsnellerstatus = 0;
int buttonSnelheid1status    = 0;
unsigned long startMillis;
unsigned long currentMillis;

//operating states
boolean forward = false;
boolean backward =  false;
boolean stopt = false;

void setup()
{
  //Input en output aangeven
  pinMode(MotorVoorruit, OUTPUT);
  pinMode(MototAchteruit, OUTPUT);
  pinMode(Hbrugaan, OUTPUT);

  startMillis = millis();//slaat de start tijd op

  //Knoppen binnden aan externe functies
  buttonAllesUit.attachClick(RuitenwisserUit);
  buttonInterval.attachClick(RuitenwisserInterval);
  buttonXmaalsneller.attachClick(RuitenwisserXmaalSneller);
  buttonSnelheid1.attachClick(RuitenwisserSnelheid1);

  Serial.begin(9600);

}


void loop()
{
  currentMillis = millis();   //de tijd op dit moment(tijd vanaf het begin van programma)


  //controlleert de status van de knoppen
  buttonAllesUit.tick();
  buttonInterval.tick();
  buttonXmaalsneller.tick();
  buttonSnelheid1.tick();
  delay(10);

  //Ruitenwisser met interval code

  if (buttonIntervalstatus == HIGH) //intial start time and was set before entry
  {

    if (forward == true)
    {
      digitalWrite(Hbrugaan, HIGH); // zet de H-brug aan
      analogWrite(MototAchteruit, 0);
      analogWrite(MotorVoorruit, Power);
    }

    if (forward == true and currentMillis - startMillis > aan )
    {
      forward = false;
      backward = true;
      //startMillis = millis();
      //motor beweegt achteruit
      analogWrite(MotorVoorruit, 0);
      analogWrite(MototAchteruit, Power);
    }
    
    if (backward == true and currentMillis - startMillis > 2 * aan)
    {
      backward = false;
      stopt = true;
      analogWrite(MotorVoorruit, 0);
      analogWrite(MototAchteruit, 0);
      digitalWrite(Hbrugaan, LOW);
    }

    if (stopt == true and currentMillis - startMillis > (2 * aan) + wacht)
    {
      stopt = false;
      forward = true;
      startMillis = millis();
    }


    buttonAllesUit.tick();
    buttonInterval.tick();
    buttonXmaalsneller.tick();
    buttonSnelheid1.tick();
    delay(10);

  }

  else {
    analogWrite(MotorVoorruit, 0);
    analogWrite(MototAchteruit, 0);
    digitalWrite(Hbrugaan, LOW);
  }


}

void RuitenwisserInterval()
{
  //Alle loops uit zodat er tijdens dat er een andere loop gedaan wordt er zonder
  //op de alles uit knop gedrukt kan worden gewisseld kan worden
  buttonAllesUitstatus = LOW;
  buttonXmaalsnellerstatus = LOW;
  buttonSnelheid1status = LOW;

  //Start loop voor interval
  buttonIntervalstatus = HIGH;
  Serial.println("Ruitenwisser gaat met een interval aan");
  startMillis = millis();//add intial start time
  forward = true;
}


void RuitenwisserUit()
{

  buttonIntervalstatus = LOW;
  buttonAllesUitstatus = HIGH;
  buttonXmaalsnellerstatus = LOW;
  buttonSnelheid1status = LOW;
  Serial.println("Ruitenwisser staat nu uit");
}
void RuitenwisserXmaalSneller()
{
  buttonAllesUitstatus = LOW;
  buttonSnelheid1status = LOW;
  buttonIntervalstatus = LOW;

  buttonXmaalsnellerstatus = HIGH;
  Serial.println("Ruitenwisser gaat nu op en neer maar dan 4.1x zo snel ");
}


void RuitenwisserSnelheid1()
{
  buttonAllesUitstatus = LOW;
  buttonXmaalsnellerstatus = LOW;
  buttonIntervalstatus = LOW;

  buttonSnelheid1status = HIGH;
  Serial.println("Ruitenwisser gaat nu op en neer met snelheid 1");
}

Wipers are usually controlled with a crank and limit switch(es) mechanism. Trying to control sweep angle with timing won't work reliably. Post a link to motor datasheet or brand name and exact part number.

It would be better to change state when a button becomes pressed rather than when it is pressed

I think that with the one button library and the use of .attachClick() the code is indeed based on the state change of becomes pressed.

My thought was the code and processing time for each of the buttons will be so long that no one will hold a button for that much time. And even if they did, the cycle would just repeat.

My thought was that the system should stay in the same state unless/until a different state was selected and, as such, the code for each state should be non blocking. This is supported by the fact that one of the required states is motor off and mimics better the action of a real wiper

We will see what the OP decides.

It's not a real one they're building though; it seems just to be a coding exercise in getting a motor to move backwards and forwards in various patterns. I guess the teacher just tried to give the project some real-world cred?

As mentioned by others, no real wiper mechanism would be operated by buttons anyway.

There was a project back in the 80s IIRC - or perhaps earlier in the 70s - in Electronics Australia magazine which was a windscreen wiper controller with two buttons.

Push one and it gives a single wipe. Push it again and it gives another wipe and then continues to wipe at the interval determined. If it takes too long, another push will set the new interval.

The second button cancels the wiping, but if the first is pushed again, it will repeat according to the interval since the last wipe. If you pushed the first button twice in succession then the interval would be set shorter than the actual wipe time; it would of course wipe continuously.

Noting the vintage, this was based on analog electronics - an LM324.

I am truly surprised that this concept - in the digital version - never appeared in a full market car as it really is so truly elegant. Just two contacts to do the whole gamut of intermittent, variable wiping. Could be extended to provide two wipes at a time (just press the button for longer). Of course there are "automatic" wipers but whether they work well is another matter; I don't know. :grin:

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