1Slap up , 1 slap down.

Sorry for the translation, writing from Spain , because here I can not find a solution to my problem.
I’m working with Arduino UNO + Sound sensor having these connections (GND , Vcc , D0, A0 ) .
I have an engine installed in a window and I need you by slapping up and down.
I’m working with this code:

int sensorPin = A0;   // Entrada Analogica Sensor sonido 

int UPMotor = 2;    // Salida 
int DOWNMotor =3; 
int sensorValue = 0;  // variable para almacenar el valor del sensor

int OnOff=0;




void setup() {

  // declarar la relayPin como salida:

  pinMode(UPMotor, OUTPUT);  
 pinMode (DOWNMotor,OUTPUT);
  

  digitalWrite(UPMotor, LOW); 
digitalWrite (DOWNMotor ,LOW);
  Serial.begin(9600);

}




void loop() {

  // leer el valor del sensor:

  sensorValue = analogRead(sensorPin);    




 long startTime = millis();

 while(millis() - startTime < 2000){ // Ajuste tiempo entre palmada y palmada

      sensorValue = analogRead(sensorPin); 

       if (sensorValue > 800)// Ajuste humbral nivel ruido 

       {

            if(OnOff==0){
          OnOff=1;
            }
      else
          OnOff=0;
        
        
       } 
 }

  if (OnOff == 0){
  
      
      digitalWrite(UPMotor, HIGH); 
      delay(10000);
      digitalWrite (UPMotor,LOW);
 

  }
  
  else{
      
      digitalWrite(BajarMotor, HIGH);
      delay (10000);
      digitalWrite (DOWNMotor,LOW);

  } 

}

But what I need is something else, I need to work like this:

-Give 1 slap and the motor up the window for 10 sec . and stops.
-Give 1 slap and the motor down the window for 10sec. and stops.
-…

In each slap to do a different action . ( 1 slap up for "10 " sec . And stops. Next 1 slap down for "10 " sec . And stops. )

I hope my problem and can help me understand .

thank you !

Separate the "slap" detection code from the motor movement code.

Arrange for the slap detection to change a variable - perhaps call it motorMove and it can have the values 'U' or 'D'

You can test this without any motor by printing the value to Serial Monitor.

Then make another function that causes the motor to move depending on the value in that variable. The motor function also needs a variable to record whether the motor is fully UP or DOWN so it does not try to move it too far.

...R
Planning and Implementing a Program

I have not understood very well . ( stupid translator! >:( )
You could help by making your code ?

Moderator edit: Mind your language.

I will go as far as pseudo code - writing a full program would be too much

void loop() {
   detectSlap();
   moveMotor();
}

void detectSlap() (
    // code to detect sound - (I don't know how you are doing that)
    if (soundDetected == true) {
       if (motorMove == 'D') {
           motorMove = 'U';
       }
       else {
            motorMove = 'D'
       }
    }
}

void operateMotor() {
     if (motorMove == 'U' && motorPos != 'T') { // T for top
        // code to move motor up
        // when it is up set motorPos = 'T'
     }
     if (motorMove == 'B' && motorPos != 'B') { // B for bottom
        // code to move motor down
        // when it is down set motorPos = 'B'
     }
}

There is a Spanish section of the Forum.

...R