Interfaz para Ambiloop looper con Arduino / controlar click, short hold y long hold sin debounce

Interfaz para ambiloop con Arduino

Dos botones. Se detectan click, short hold, long hold, controlando debounce.

Click del boton1: cambia de track
Click del boton2: comienza / detiene grabación en el track actual
Short hold del boton2: borra el track actual
Long hold del boton2: borra todos los tracks, y selecciona el track 1


#define CLICK 1
#define SHORT 2
#define LONG 3
#define IDLE 0

const int buttonPin = 4;    
int buttonState;           
int lastButtonState = LOW; 

const int button2Pin = 5;    
int button2State;           
int lastButton2State = LOW; 

int estadoBoton1;
int estadoBoton2;

long lastDebounceTime = 0;
long debounceDelay = 30;  
long tiempoIni, tiempoFin;
bool boton1ON, short1ON, long1ON;

long lastDebounceTime2 = 0;
long debounceDelay2 = 30;  
long tiempoIni2, tiempoFin2;
bool boton2ON, short2ON, long2ON;

String tracks[8] = {"1", "2", "3", "4", "5", "6", "7", "8"};
int trackActual = 0;

void setup() {
  pinMode(buttonPin, INPUT);
  boton1ON = false;
  short1ON = false;
  long1ON = false;    
  estadoBoton1 = IDLE;
  estadoBoton2 = IDLE;
}

void loop() {
  bool boton1 = ValidarBoton1();
  bool boton2 = ValidarBoton2();
  
  switch (estadoBoton1)
  {
    case CLICK: 
      trackActual++;
      if (trackActual > 8)
        trackActual = 1;
        
      Keyboard.print(tracks[trackActual-1]);
      estadoBoton1 = IDLE;
      break;
  }

  switch (estadoBoton2)
  {
    case CLICK: 
      Keyboard.print("r"); 
      estadoBoton2 = IDLE; 
      break;
    case SHORT: 
      Keyboard.print("e"); 
      estadoBoton2 = IDLE;
      break;
    case LONG: 
      Keyboard.print("1e2e3e4e5e6e7e8e1 ");
      trackActual = 1;
      estadoBoton2 = IDLE;
      break;   
  }  
}

bool ValidarBoton1()
{
  int reading = digitalRead(buttonPin);

  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  } 
  
  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
      buttonState = reading;

      if (buttonState == HIGH) {          
        tiempoIni = millis();

        while (buttonState == HIGH)
        {
          buttonState = digitalRead(buttonPin);
          delay(1);
        }
        tiempoFin = millis();

        if (tiempoFin - tiempoIni > 1500)
          estadoBoton1 = LONG;  
        else if (tiempoFin - tiempoIni > 500)
          estadoBoton1 = SHORT;
        else 
          estadoBoton1 = CLICK;
      }
    }
  }
  
  lastButtonState = reading;
}  

bool ValidarBoton2()
{
  int reading2 = digitalRead(button2Pin);

  if (reading2 != lastButton2State) {
    lastDebounceTime2 = millis();
  } 
  
  if ((millis() - lastDebounceTime2) > debounceDelay2) {
    if (reading2 != button2State) {
      button2State = reading2;

      if (button2State == HIGH) {          
        tiempoIni2 = millis();

        while (button2State == HIGH)
        {
          button2State = digitalRead(button2Pin);
          delay(1);
        }
        tiempoFin2 = millis();
        
        if (tiempoFin2 - tiempoIni2 > 1500)
          estadoBoton2 = LONG;  
        else if (tiempoFin2 - tiempoIni2 > 500)
          estadoBoton2 = SHORT;
        else 
          estadoBoton2 = CLICK;
      }
    }
  }
  
  lastButton2State = reading2;
}  

No hay comentarios:

Publicar un comentario