I need some help with a washer machine

Hello, people I am a DIY addict and an enthusiast (yet new or wanabee) programmer but never really worked with a code this hard. I thought that it will be easy but I think that I am using wrong condition sentences fot this case.
I own an old washer machine and I want to make a program for it to work with two buttons. It has two sensors the one that indicates if it is full or empty and the one that indicates if the cap is closed or open.
With one button I want it to wash (a sequence where it activates a valve until the pres sensor indicates that it is full, then start the washing sequence only if the cap is closed, when it finishes the wash and spin (empty) it turns on the valve again for the sequence of the rinse sequence and then, to finish turn un the buzzer alarm).
And the other button to spin the water out of it juts to dry the hand washed clothes.
My code works for the Bot1 part, but I think I am doing wrong the conditions for the second button Bot2.
Can anyone help me solve that?

const int Lavar      =  2;
const int Exprimir  =  3;
const int Valvula    =  4;
const int Buzz       =  5;
const int Relay      =  6;

const int Tapa       = 10;
const int Pres       = 11;
const int Bot1       = 12;
const int Bot2       = 13;

int a         = 0;
int b         = 0;
int c         = 0;


void setup()
{
  pinMode(Lavar,     OUTPUT);   //I made an H brige. This pulse activates the washing mode
  pinMode(Exprimir, OUTPUT);  //This pulse activates the spin mode
  pinMode(Valvula,  OUTPUT);  //This is the valve to fill the washer
  pinMode(Relay,    OUTPUT);  //this one is to energize the motor (and H bridge)
  pinMode(Buzz,     OUTPUT);  //This one is the buzzer
  pinMode(Bot1,     INPUT);    //button 1
  pinMode(Bot2,     INPUT);    //button 2
  pinMode(Tapa,     INPUT);    //cap sensor
  pinMode(Pres,     INPUT);    //full or empty sensor
}


void loop()
{
  if (digitalRead(Bot1) == HIGH && a == 0)
  {
    a = 1;
    digitalWrite(Buzz,     LOW);   //my relays turn on with LOW pulses
    delay(50);
    digitalWrite(Buzz,     HIGH);
    digitalWrite(Lavar,    HIGH); //to wash or spin I must turn one mode on and the other one off
    digitalWrite(Exprimir, LOW);
    digitalWrite(Relay,    LOW);
    delay(4000);
    digitalWrite(Lavar,    HIGH);
    digitalWrite(Exprimir, HIGH);
    digitalWrite(Relay,    HIGH);
    digitalWrite(Buzz,     LOW);
    delay(300);
    digitalWrite(Buzz,     HIGH);
    a = 0;
  }

  else if ( digitalRead(Bot2) == HIGH && b == 0 && c == 0)
  {
    b = 1;
    digitalWrite(Lavar,    HIGH);
    digitalWrite(Exprimir, HIGH);
    digitalWrite(Relay,    HIGH);
    digitalWrite(Buzz,     LOW);
    delay(150);
    digitalWrite(Buzz,     HIGH);
    if (b == 1 && c == 0)
    {
      digitalWrite(Valvula, LOW);
      c = 1;
      if ( b == 1 && c == 1 && digitalRead(Pres) == HIGH)
      {
        c = 2;
        if ( b == 1 && c == 2 && digitalRead(Tapa) == HIGH)
        {
          digitalWrite(Lavar,    LOW);
          digitalWrite(Exprimir, HIGH);
          digitalWrite(Relay,    LOW);
          delay(4000);
          digitalWrite(Lavar,    HIGH);
          digitalWrite(Exprimir, HIGH);
          digitalWrite(Relay,    HIGH);
          c = 3;
          if ( b == 1 && c == 3)
          {
            digitalWrite(Valvula, LOW);
            c = 4;
            if ( b == 1 && c == 4 && digitalRead(Pres) == HIGH)
            {
              digitalWrite(Valvula, HIGH);
              c = 5;
              if ( b == 1 && c == 5 && digitalRead(Tapa) == HIGH)
              {
                digitalWrite(Lavar,    HIGH);
                digitalWrite(Exprimir, LOW);
                digitalWrite(Relay,    LOW);
                delay(4000);
                digitalWrite(Lavar,    HIGH);
                digitalWrite(Exprimir, HIGH);
                digitalWrite(Relay,    HIGH);
                digitalWrite(Buzz,     LOW);
                delay(500);
                digitalWrite(Buzz,     HIGH);
                b = 0;
                c = 0;
              }
            }
          }
        }
      }
    }
  }

  else
  {
    a = 0;
    b = 0;
    c = 0;
    digitalWrite(Lavar,     HIGH);
    digitalWrite(Exprimir,  HIGH);
    digitalWrite(Buzz,      HIGH);
    digitalWrite(Valvula,   HIGH);
    digitalWrite(Relay,     HIGH);
  }
}

Thanks a lot to anyone that reads this post and try to help.
I tried a lot but I might not know how to solve this one.

Also, sorry if I do a mistake posting this. It is my first time posting.

Your code looks very complicated for what sounds (from your description) like a simple requirement.

Because you have all those meaningless variable names (such as 'b' and 'c' it would a very long time to figure it out. Also single character variable names make debugging very difficult. Try searching for all the occurrences of the variable 'b' :slight_smile:

I think you should do some reading about the concept of a State Machine. Don't be put off by the grand name. It is just a system for using one or more variables to keep track of progress through a series of stages.

...R
Planning and Implementing a Program

Robin2, Thank you su much.

I think I will try to do it with switch case
But do you know it is possible to read my pressure sensor as a condition to fill the washer machine in just one of the cases?
Because I think each button is a case, am I wrong?
And I just want to fill the washer in the washing cycle case

EduardoFlagrante:
I think I will try to do it with switch case
But do you know it is possible to read my pressure sensor as a condition to fill the washer machine in just one of the cases?
Because I think each button is a case, am I wrong?
And I just want to fill the washer in the washing cycle case

There is not enough detail in your comments to enable me to respond.

You obviously have a lot of stuff in your head that you have not told us.

...R

EduardoFlagrante:
But do you know it is possible to read my pressure sensor as a condition to fill the washer machine in just one of the cases?
Because I think each button is a case, am I wrong?
And I just want to fill the washer in the washing cycle case

The gist of the state machine is:

State 1: turn on/off one or more outputs (fill the washer)
Test: the outputs controlled above will cause some variable or sensor to (hopefully) reach a desired state. When the desired condition is achieved if(pressure switch == true) state is changed to 2.

State 2: Some of the outputs that were on before may stay on, some may turn off (stop adding water). Usually new outputs come into play (start agitation).
Test: the conditions for the next step are tested (have we agitated for ten minutes? If so, change state to 3). They're different than the one before, else we wouldn't need to change them.

State 3: Stop agitation, start spin.
Test: Spin timer done?

etc., etc.

Your loop() function should look like:

void loop()
{
   if(lidIsClosed())
   {
      if(needsToStart())
      {
         Fill();
         Wash();
         Drain();
         Rinse();
         Spin();
       }
    }
}

Develop functions to do all the "hard" stuff.

If you break your program down like this, you'll discover that there really aren't any hard parts. There are just parts that need more refinement.

Can you guess what each function should do, what its return type should be, and how to do them?

If not, feel free to ask. If you do, implement the function (by moving the code you have now into the proper function).

You'll quickly see where you have made wrong assumptions, etc.