controlling a motorized potentiometer with ir remote

The code bellow is used to control a motorized potentiometer ( a 12 v dc motor) through a L298N H-bridge module.
The idea is that when i keep pressing the + or - button, the dc motor will work constantly. - that works
And when i press for a short time one of the buttons, i want the motor to work for a short amount of time (lets say 0,5 seconds). - here i keep going crazy, can't think of a way to program it so it would work.

It also has a input switch(er), that also works fine.

I ask for your help, maybe a much cleared head can think of a better approach, or a solution.

Yes, if have googled the ... out of this subject, haven't managed to find something useful.

#include <IRremote.h>

//pin receptor IR
const int RECV_PIN = 6;
//Pinii pt driver motor
const int enA = 9;
const int in1 = 7;
const int in2 = 8;
//pin buton comutare sursa audio
const int button = 5;
//pini sursa audio
const int out1 = 2;
const int out2 = 3;
const int out3 = 4;
//pin activare tranzistor
const int tranzistor = 10;

//variabile
int output;
int contor = 0;
int k=0;
unsigned long interval_pulsatie;
unsigned long last_pulsatie;

int buttonState = LOW;
long lastDebounceTime = 0;
long debounceDelay = 50;

unsigned long lastCode;

IRrecv irrecv(RECV_PIN);
decode_results results;





void setup() {
  Serial.begin(9600);
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(button, INPUT);
  pinMode(out1, OUTPUT);
  pinMode(out2, OUTPUT);
  pinMode(out3, OUTPUT);
  pinMode(tranzistor, OUTPUT);
  //activam IR receiver
  irrecv.enableIRIn();

  //activare tranzistor si intarziere 12 sec
  //delay(15000);
  digitalWrite(tranzistor, HIGH);
  
  //activam output 1 cu intarziere 1 sec
  //delay(1000);
  digitalWrite(out1, HIGH);
  output = 1;

  analogWrite(enA, 50);
  
}




void loop() {
  if(irrecv.decode(&results)){  // verifica receptionare cod
              Serial.println(results.value, HEX);
              if (results.value == 0xFFE01F || results.value == 0xF076C13B ||
                  results.value == 0xFFA857 || results.value == 0xA3C8EDDB || 
                  results.value == 0xFFFFFFFF){ 
                      if(results.value == 0xFFFFFFFF){
                               if (lastCode == 0xFFE01F || lastCode == 0xF076C13B ||
                                   lastCode == 0xFFA857 || lastCode == 0xA3C8EDDB){
                               // daca se repeta, folosim ultimul cod primit
                               results.value = lastCode;
                               }
                      }
                      // buton volum -
                      if(results.value == 0xFFE01F || results.value == 0xF076C13B){
                               last_pulsatie = interval_pulsatie;
                               interval_pulsatie = millis();
                               //Serial.print(interval_pulsatie - last_pulsatie);
                               lastCode = results.value;
                               digitalWrite(in2, LOW);
                               digitalWrite(in1, HIGH);
                               contor = 1;
                     } 
                     //buton volum +
                    if(results.value == 0xFFA857 || results.value == 0xA3C8EDDB) {
                               last_pulsatie = interval_pulsatie;
                               interval_pulsatie = millis();
                               //Serial.print(interval_pulsatie - last_pulsatie);
                               lastCode = results.value;
                               digitalWrite(in2, HIGH);
                               digitalWrite(in1, LOW);
                               contor = 1;
                    }
              }
  
              // button sursa
              if(results.value == 0xFF906F){ 
                    if(output == 1 && k == 0){
                                           digitalWrite(out1, LOW);
                                           delay(20);
                                           digitalWrite(out2, HIGH);
                                           output = 2;
                                           delay(50);
                                           results.value = 0;
                                           k = 1;
                               }  
                               if(output == 2 && k == 0){
                                           digitalWrite(out2, LOW);
                                           delay(20);
                                           digitalWrite(out3, HIGH);
                                           output = 3;
                                           delay(500);
                                           results.value = 0;
                                           k = 1;
                               }
                               if(output == 3 && k == 0){
                                           digitalWrite(out3, LOW);
                                           delay(20);
                                           digitalWrite(out1, HIGH);
                                           output = 1;
                                           delay(500);
                                           results.value = 0;
                                           k = 1;
                               }
                               k = 0;
                    }

             if (!(results.value == 0xFFE01F || results.value == 0xF076C13B || 
                   results.value == 0xFFA857 || results.value == 0xA3C8EDDB || 
                   results.value == 0xFFFFFFFF || results.value == 0xFF906F )){
                     lastCode = 0;
                     Serial.println("0");
             }
             irrecv.resume();
  }

  
  else{
             if (contor == 1 && millis() - last_pulsatie > 250){
                      //Serial.println("   intrerupe");
                      digitalWrite(in1, LOW);
                      digitalWrite(in2, LOW);
                      contor = 0;
             }
            // else{
              //delay(1000);
              //digitalWrite(in1, LOW);
              //digitalWrite(in2, LOW);
             //}
  }





/*

  if(digitalRead(button) == LOW){
    if(output == 1){
      digitalWrite(out1, LOW);
      digitalWrite(out2, HIGH);
      output = 2;
      delay(1000);
    }
  }

  if(digitalRead(button) == LOW){
    if(output == 2){
      digitalWrite(out2, LOW);
      digitalWrite(out3, HIGH);
      output = 3;
      delay(1000);
    }
  }

  if(digitalRead(button) == LOW){
    Serial.println("error");
    if(output == 3){
      digitalWrite(out3, LOW);
      digitalWrite(out1, HIGH);
      output = 1;
      delay(1000);
    }
  }

*/
}

not sure i understand what you code is doing.

after the comment "button surs" you have a series of if statement, not if/else if, where you first test for output == 1 and change output = 2, followed by an if that tests for output == 2 and sets output = 3, ...

because of the sequential testing and setting of output, each if condition is true and exercised

nd when i press for a short time one of the buttons, i want the motor to work for a short amount of time (lets say 0,5 seconds).

presumably you want the motor to be turned on when you receive a specific button code and turned off after not receiving the code.

when you press a button, you will receive the button code multiple times. I don't know the period between repetitions, but presumably when you receive a button code, the motor needs to be turned on for a minimum of the time between the period between button codes.

i don't understand how your code is handling this

That is the sourche button on the remote. It has a if k = 0 condition, and if the task is performed, the k =1,so it has to complete the loop to get the k =0. That part works, it has nothing to do with the dc motor

It's handling great when receiving a continuous signal, but when i press once the button and get only one reading, it doesn't do anything

Thinking about creating a loop that meassures the minimum time betweem two continuous reading when presing a button, and if it doeas not receive another reading, to execute for half a second the command

i think when you receive a button code, you need to capture the time, millis(), add a on period to it and turn the motor on.

outside of any code looking at button codes, you wait for the current time, millis() to exceed that capture + time to turn the motor of.

of course each time you receive a button code you recalculate to capture+ time delaying when the motor is turned off

i think the logic of those if states using output doesn't make sense, regardless of what it is used for. The multiple if statements are redundant