Windshield wiper motor doesnt work

hi guys im back again with another assignment i got from school. This time it is a windshield wiper that has to have 4 different modes:

  1. motor off
  2. Wiper on with a interval(1 sec forward, 1sec backwards then 1.8sec off before looping)
  3. wiper on with 1sec forward, 1 sec off, 1sec backward, 1sec off before looping.
  4. wiper on but 4.1 times faster than at mode 3 still 1 sec forward, 1sec backwards then 1.8sec off before looping but the rpm needs to be 4.1 times faster so normaly it would go at power 60 and in this mode the power becomes 255

This can be done with how many buttons as i would like. I first made a flow chart(i dont know if its correct)

and after that i made a code which uses OneButton. I did everything exactly the same as my previous code(other code) which worked fine. The problem I'm running into at the moment is that as soon as it is in 1 of the if statements in my loop code it doesnt want to read any other button i am pressing and I dont know why as it didn't have this problem before with my other code. And yes I know I only have one if statement in my code at the moment, this is because I want to first fix my code before writing the other if statements.

my code:

//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(10,true);
 OneButton buttonSnelheid1(11,true);



// Constantes
const int Hbrugaan        = 2;
const int MotorVoorruit   = 3;
const int MototAchteruit  = 5;

// Variabelen
int Power = 255;                    //snelheid waarmee de motor rond draait
int buttonAllesUitstatus     = 0;
int buttonIntervalstatus     = 0;
int buttonXmaalsnellerstatus = 0;
int buttonSnelheid1status    = 0;

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

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

  Serial.begin(9600);

}


void loop()
{

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

  //Ruitenwisser met interval code
  if(buttonIntervalstatus==HIGH)
    {
        // motor gaat vooruit
        digitalWrite(Hbrugaan,HIGH); // zet de H-brug aan
        analogWrite(MototAchteruit,0);
        analogWrite(MotorVoorruit,Power);
        delay(1000);

    
        //motor stopt
        digitalWrite(Hbrugaan,LOW);
        analogWrite(MotorVoorruit,0);
        analogWrite(MototAchteruit,0);
       
        delay(1800);

    
        //motor beweegt achteruit
       // digitalWrite(Hbrugaan,HIGH);
        analogWrite(MotorVoorruit,0);
        analogWrite(MototAchteruit,Power);
        delay(1000);

    
        //motor stopt
        analogWrite(MotorVoorruit,0);
        analogWrite(MototAchteruit,0);
        digitalWrite(Hbrugaan,LOW);
        delay(1800);


  }
  
        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");
  }


void RuitenwisserUit()
  {
    buttonIntervalstatus = LOW;
    buttonAllesUitstatus = LOW;
    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");
  }

The buttons are connected normally with 10k resitor and to the pin described in the code.
The rest of the board so motor,H-bridge and other wires are connected like the picture


with all the black wires on GND and red on the 5V.

I hope someone can help me because i have tried everything i know.

Aren't these motors 12V?

Are you really trying to power a motor from the Uno 5V? Motors need their own, external, power supply capable of supplying the motor rated voltage and stall current.

What is the rated voltage of the motor. The L293 motor driver drops 2V to over 4V of the motor supply voltage. With that driver chip and 5V motor power the most that the motor will see is 3V. The L293 is an ancient and inefficient motor driver. There are much better modern drivers available.

Can you use switches instead of momentary buttons? Most cars have switches,

I'd use switches (or one rotary switch is ideal). Otherwise, you'll have to keep track of the last button pushed and that would probably be a separate function, probably with an interrupt when a button is pushed.

With a switch, I'd use a switch-case function to select which function is called.

Then each function runs a loop that checks the (physical) switch state at the end of each loop and returns from the function when the (physical) switch selection no longer matches the currently running function.

unfortunately we can only use buttons

@groundFungus @jfjlaros yes it is im sorry i cut the picture badly this is the whole setup

Tell us more about the actual wiper motor. IS it just the motor, or does it also have the gearing to rotate the wiper back and forth? Does it have extra wires for the home position? Do you realize the motor will take MANY amps at 12 volts to actually run?

Much better, but the motor will get just 4V, at most. Is that enough?

i tried it at the lowest rpm it needs to run on (60) and it runs fine without any trouble

I will copy my assignment and translate it to english so you guys can understand better what it is that i need to do

Task 2 Windshield Wiper (version A).
Using Arduino and breadboard +components, create a system that simulates the car's wiper
simulates. To do this, use the DC motor. Let the motor run 1 second in one direction and then
second in the other direction.
There are four states:
1. All off
2.Windshield wiper goes back and forth at an interval (i.e. back and forth, wait a few seconds, then back and forth).
seconds and then back and forth again)
3.Windshield wiper goes back and forth at speed 1
4.Windshield wiper goes back and forth but now X times as fast
Make sure you don't have to hold down the push buttons.
For the interval frequency and the speed of the fourth state, see Excel list. Enter your
Student number, not your pcn number.
Make your assignment into a flow chart, electrical diagram, your own code in Arduino with comments and
A video showing the operation of your assignment (must be recognizable that you made this). Submit
this assignment in Gradework.

Ok, then there is NO windshield wiper involved, just a simple DC motor. Have you done the logic flowchart, yet? Pretty important BEFORE doing any coding, other than motor testing.

these are the only flowcharts i have made:


just to clarify, the code does work to a certain extent. It wil start and when i press one of my 4 buttons it wil go into the if- statements i intended it to go but it wont get out of the loop as in, it doesn't look at the button inputs after being in one of the if- statements

That's due to all those delay(1000) and delay(1800) etc, which block the code from looking at the buttons. This project calls for being re-factored with a blink-without-delay() approach.

A couple more millis() timing tutorials.

Beginner's guide to millis().
Several things at a time.

1 Like

@groundFungus @octopirate i just read both articles you guys sent and this is what I made. i can now switch between the different button states but the motor now only goes forward instead of 1sec forward,1sec backwards then waiting 1.8sec before resuming

//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;

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)
    {
       if(currentMillis-startMillis>=aan){
        startMillis =currentMillis;
        // motor gaat vooruit
        digitalWrite(Hbrugaan,HIGH); // zet de H-brug aan
        analogWrite(MototAchteruit,0);
        analogWrite(MotorVoorruit,Power);
       }
       if(currentMillis-startMillis>=aan){
        startMillis =currentMillis;
        //motor beweegt achteruit
        analogWrite(MotorVoorruit,0);
        analogWrite(MototAchteruit,Power);
       }

      if(currentMillis-startMillis>=wacht){
        startMillis =currentMillis;
        //motor stopt
        analogWrite(MotorVoorruit,0);
        analogWrite(MototAchteruit,0);
        digitalWrite(Hbrugaan,LOW);
      }

        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");
  }


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");
  }```

Another handy trick is to use an LED with series resistor in place of the motor until you get all the logic functioning correctly.

Then worry about the problems you may well run into or may have already with the real motor.

You can expand the idea for running a motor two ways. Use the same circuit you would for any little DC motor that you can program to go one direction or the other, and instead of the motor use a green LED wired in parallel with a red LED (reversed polarity) and a series resistor. When the current goes left-to-right, one LED lights up, when you reverse the direction of the current (to make the "motor" spin the other way) the other LED will be the one to light up.

A7

Look at your second flow chart. It goes to an end. Should go back to test the switches, again.

i tried running the code i sent and now my arduino doesnt turn my motor anymore

Here is another way to think about your project. You have 4 buttons that can be pressed. Make your loop only contain the code to test each of the button to see if pressed.
If a button is pressed, call a function that will perform the details for that particular button and return to the loop. Any of those functions may call other functions. Some may even call a function that is called for a different switch operation.