Structure problem

I'm working to make a windshield wiper mechanism that works like this. When the push button is release the motor start clock wise until hits switchOpenStop and stops. When the push button is pushed and hold the motor start counter clock wise until hits switchCloseStop and stops.

This is the code that I have and works fine.

What I'm trying to implement now is like a safety feature that works like this.

When the push button is realese the motor start clock wise normally, BUT if I push and hold the push button the motor start going CCW until hits the switchCloseStop.

I have been working with this for the last 4 days and can't make it work. Can anybody help me with the code?

Thanks

while (digitalRead (pushbutton) == HIGH )  { //waits for the push button to be realese
Serial.println("stand by "); 
  }
  
while (digitalRead(switchOpenStop) == LOW)  { //wait for switch 

Serial.println("CW"); //motor close wise
  }

while (digitalRead(pushbutton) == LOW)  { //wait for push button to be pushed and hold
Serial.println("stop");
  }
while (digitalRead(switchCloseStop) == LOW )  {// wait for 2nd stop switch
Serial.println("CCW"); // motor counter clock wise
  }





  } // void setup end

Your description is not very clear. You have to complete a table such as this example, then you may be able to use a simple state machine solution.

current state       button action              comment
-------------       -------------              --------
wiper stopped       none                       no activity
wiper stopped       button pushed and held     wiper counter clockwise to switchCloseStop
wiper clockwise     button release             wiper clockwise to switchOpenStop

Include the situation where the wiper has reached the end of travel because the wiper could presumably be stopped at either end.
Is it a simple push to make button ie normally open ?
You have note posted all your code.

Those while statements block execution of the rest of the code. Change that to if statements and you can actually react to various inputs independently.

When you start working on this as a state machine, there's a whole lot more states than the 3 suggested above. The Arduino doesn't know about "pushed and held" unless you give it specific times. If the button is released less than 1000 milliseconds after it was first pushed then you could consider it a short press. The trick is detecting that "first pushed" and starting the timer. That makes another state.

The code posted is incomplete but it appears from the last line that you may have put all your code into setup(). The main state machine needs to be in loop(). Think of loop() running thousands of times per second, checking all the buttons over and over. Depending on what it finds and its memory of what went before, it can make decisions. Depending on those decisions, it may call the motor functions to go forwards or backwards or stop.

Thanks guys for the replay.

You can think like a dump truck mechanism if you want. I release the push button and start lifting until stop and vice versa, but if I push the push button the motor stop and reverse. Let's say a "abort" command.

I attached the whole code and a table. Hope this helps.

thanks again

//button action         switchOpenStop  switchCloseStop   current state       comment
//switchLatch depress   open            close             motor stop          wait for switchLatch to be release
//switchLatch release   open            open              motor going cc      BUT WHILE MOVING CC, IF I DEPRESS switchLatch, MOTOR STOP AND START GOING CCW
//switchLatch release   close           open              motor stop          waiting for switchLatch to be depress
//switchLatch depress   open            open              motor going ccw     BUT WHILE MOVING CCW, IF I RELEASE switchLatch, MOTOR STOP AND START GOING CC 
//switchLatch depress   open            close             motor stop          wait for switchLatch to be release




const int switchLatch = 2;       // switch on/off
const int switchOpenStop = 3;   //switch open stop
const int switchCloseStop = 4;  //switch close stop



void setup() {
Serial.begin(9600);
  
  
  // initialize the pushbutton pin as an input:
  pinMode(switchLatch, INPUT);
  pinMode(switchOpenStop, INPUT);
  pinMode(switchCloseStop, INPUT);
  
}

void loop() {
  
while (digitalRead (switchLatch) == HIGH )  { //waits for the push button to be realese
Serial.println("stand by "); 
  }
  
while (digitalRead(switchOpenStop) == LOW)  { //wait for switch 

Serial.println("CW"); //motor close wise
  }

while (digitalRead(switchLatch) == LOW)  { //wait for push button to be pushed and hold
Serial.println("stop");
  }
while (digitalRead(switchCloseStop) == LOW )  {// wait for 2nd stop switch
Serial.println("CCW"); // motor counter clock wise
  }





  } // void setup end

I've attempted to interpret and translate your table into code and update your sketch. I've assumed that the buttons are wired between the arduino pin and ground and set pullup resistors.

//button action         switchOpenStop  switchCloseStop   current state       comment
//switchLatch depress   open            close             motor stop          wait for switchLatch to be release
//switchLatch release   open            open              motor going cc      BUT WHILE MOVING CC, IF I DEPRESS switchLatch, MOTOR STOP AND START GOING CCW
//switchLatch release   close           open              motor stop          waiting for switchLatch to be depress
//switchLatch depress   open            open              motor going ccw     BUT WHILE MOVING CCW, IF I RELEASE switchLatch, MOTOR STOP AND START GOING CC
//switchLatch depress   open            close             motor stop          wait for switchLatch to be release

const int switchLatch = 2;       // switch on/off
const int switchOpenStop = 3;   //switch open stop
const int switchCloseStop = 4;  //switch close stop

enum {  STOPPED , CW , CCW } motorState, oldMotorState ;

void setup() {
  Serial.begin(9600);

  // initialize the pushbutton pin as an input:
  pinMode(switchLatch, INPUT_PULLUP);      //assume button wired low side.
  pinMode(switchOpenStop, INPUT_PULLUP);  //assume button wired low side.
  pinMode(switchCloseStop, INPUT_PULLUP);  //assume button wired low side.
  
  oldMotorState = STOPPED ;
  motorState = STOPPED;
}

void loop () {
  bool atOpenStop = digitalRead(switchOpenStop)  == LOW ;  //assume button wired low side.
  bool atCloseStop = digitalRead(switchCloseStop)  == LOW ; //assume button wired low side.
  bool switchDepressed = digitalRead(switchLatch)  == LOW ; //assume button wired low side.

  // set state

  if ( switchDepressed && ! atCloseStop )  {
    motorState = CCW ;
  }

  if ( switchDepressed &&  atCloseStop )  {
    motorState = STOPPED ;
  }

  if ( !switchDepressed && ! atOpenStop )  {
    motorState = CW ;
  }

  if ( !switchDepressed && atOpenStop )  {
    motorState = STOPPED  ;
  }


  // state transtions

  if ( motorState == STOPPED && oldMotorState != STOPPED )   Serial.println( "Motor Stopped" ) ;
  if ( motorState == CW && oldMotorState != CW )   Serial.println( "Motor CW" ) ;
  if ( motorState == CCW && oldMotorState != CCW )   Serial.println( "Motor CCW" ) ;

  oldMotorState = motorState ;
  delay (250 ) ; // crude button debounce
}

You have not explained how you wiper works mechanically. I do not know modern systems but traditionally they are unidirectional motors in use with the reverse motion accomplished by mechanical means. The direction is not reversed electrically

Thank you so much 6v6gt !! I really appreciate your time and effort.

Thanks again!!