Skip to content

Instantly share code, notes, and snippets.

@carlosdelfino
Last active January 7, 2020 21:38
Show Gist options
  • Save carlosdelfino/94f951a7819193e08c19d3d3cb129851 to your computer and use it in GitHub Desktop.
Save carlosdelfino/94f951a7819193e08c19d3d3cb129851 to your computer and use it in GitHub Desktop.
Exemplo de Algorítimo para uso com shields que usam botões pela porta analógica (https://www.autocorerobotica.com.br/display-lcd-shield-com-teclado)
/**
* Shield indicado: https://www.autocorerobotica.com.br/display-lcd-shield-com-teclado
*/
byte readButton() {
int botao = analogRead (BUTTON_PORT);
if (botao < BUTTON_LIMIAR_DIREITA) {
return BUTTON_RIGHT;
} else if (botao < BUTTON_LIMIAR_CIMA) {
return BUTTON_UP;
} else if (botao < BUTTON_LIMIAR_BAIXO) {
return BUTTON_DOWN;
} else if (botao < BUTTON_LIMIAR_ESQUERDA) {
return BUTTON_LEFT;
} else if (botao < BUTTON_LIMIAR_SELECIONA) {
return BUTTON_SELECT;
}
return BUTTON_NULL;
}
void checkButton() {
static byte lastButton = BUTTON_NULL;
byte actualButton = readButton();
lastButton = actButton(actualButton, lastButton);
}
byte actButton(byte actualButton, byte lastButton) {
static double last = 0;
if (actualButton != BUTTON_NULL && lastButton == actualButton ) {
if ( millis() - last > BUTTON_TIME) {
switch (lastButton ) {
case BUTTON_RIGHT:
action1();
break;
case BUTTON_LEFT:
action2();
break;
case BUTTON_UP:
action3();
break;
case BUTTON_DOWN:
action4();
}
last = 0;
}
} else if (actualButton == BUTTON_NULL && lastButton != BUTTON_NULL) {
switch (lastButton ) {
case BUTTON_RIGHT:
action5();
break;
case BUTTON_LEFT:
action6();
break;
case BUTTON_UP:
action7();
break;
case BUTTON_DOWN:
action8();
break;
case BUTTON_SELECT:
action9();
break;
}
last = 0;
}
else last = millis();
return actualButton;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment