// #inlcude "PinChangeInterrupt.h"
#include <PinChangeInterrupt.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int buttonPin1 = A3; // Pin del primer botón
const int buttonPin2 = A2; // Pin del segundo botón
const int buttonPin3 = A1; // Pin del tercer botón
const int buttonPin4 = A0; // Pin del cuarto botón
volatile int buttonState1 = 0; // Estado del primer botón
volatile int buttonState2 = 0; // Estado del segundo botón
volatile int buttonState3 = 0; // Estado del tercer botón
volatile int buttonState4 = 0; // Estado del cuarto botón
#define DELAY_BTN 500 // 500ms
int i = 0;
int btnPress = 0;
long lastTime = 0;
int selectedKart = 1;
#define ENCODER_CLK 5
#define ENCODER_DT 6
#define ENCODER_SW 7
int counter = 0;
bool enviarFlag = false;
void setup() {
Serial.begin(115200);
// Initialize LCD
lcd.init();
lcd.backlight();
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
pinMode(buttonPin4, INPUT_PULLUP);
// Initialize encoder pins
pinMode(ENCODER_CLK, INPUT_PULLUP);
pinMode(ENCODER_DT, INPUT_PULLUP);
pinMode(ENCODER_SW, INPUT_PULLUP);
// attachInterrupt(digitalPinToInterrupt(ENCODER_CLK), readEncoder, FALLING);
attachPCINT(digitalPinToPCINT(ENCODER_CLK), readEncoder, FALLING);
attachPCINT(digitalPinToPCINT(ENCODER_SW), resetCounter, FALLING);
// // Hardware Interrupts just Pin 2 and Pin 3
// attachInterrupt(digitalPinToInterrupt(buttonPin1), buttonISR, RISING);
// attachInterrupt(digitalPinToInterrupt(buttonPin2), buttonISR, RISING);
// attachInterrupt(digitalPinToInterrupt(buttonPin3), buttonISR, RISING);
// attachInterrupt(digitalPinToInterrupt(buttonPin4), buttonISR, RISING);
// Pin change Interriupts Any Pin
attachPCINT(digitalPinToPCINT(buttonPin1), pinisr, FALLING);
attachPCINT(digitalPinToPCINT(buttonPin2), pinisr, FALLING);
attachPCINT(digitalPinToPCINT(buttonPin3), pinisr, FALLING);
attachPCINT(digitalPinToPCINT(buttonPin4), pinisr, FALLING);
}
void loop() {
// Tu código principal va aquí
// Serial.println(btnPress);
if (btnPress)
{
if (millis()-lastTime > DELAY_BTN)
{
Serial.print("¡Interrupción Kart ");
Serial.print(btnPress);
Serial.println("!");
selectedKart = btnPress;
lastTime = millis();
}
btnPress = 0;
}
Serial.println(i);
i++;
delay(200);
// lcd.setCursor(3, 0);
// lcd.print("Counter:");
// lcd.setCursor(7, 1);
// lcd.print(getCounter());
// lcd.print(" ");
lcd.setCursor(3, 0);
lcd.print("Kart ");
lcd.print(selectedKart);
lcd.print(" :");
lcd.setCursor(0, 1);
lcd.print("Motor Pw: ");
lcd.print(getCounter());
lcd.print("% ");
if (enviarFlag) enviandoTx();
// if (digitalRead(ENCODER_SW) == LOW) {
// resetCounter();
// }
}
void pinisr() {
if (!digitalRead(buttonPin1)) btnPress = 1;
else if (!digitalRead(buttonPin2)) btnPress = 2;
else if (!digitalRead(buttonPin3)) btnPress = 3;
else if (!digitalRead(buttonPin4)) btnPress = 4;
else btnPress = 0;
}
void buttonISR() {
if (!digitalRead(buttonPin1)) btnPress = 1;
else if (!digitalRead(buttonPin2)) btnPress = 2;
else if (!digitalRead(buttonPin3)) btnPress = 3;
else if (!digitalRead(buttonPin4)) btnPress = 4;
else btnPress = 0;
Serial.println(btnPress);
}
void readEncoder() {
int dtValue = digitalRead(ENCODER_DT);
if (dtValue == HIGH) {
counter++; // Clockwise
if (counter >= 100) counter = 100;
}
if (dtValue == LOW) {
counter--; // Counterclockwise
if (counter <= 0) counter = 0;
}
}
// Get the counter value, disabling interrupts.
// This make sure readEncoder() doesn't change the value
// while we're reading it.
int getCounter() {
int result;
noInterrupts();
result = counter;
interrupts();
return result;
}
void resetCounter() {
noInterrupts();
counter = 0;
enviarFlag = true;
interrupts();
}
void enviandoTx()
{
lcd.setCursor(0, 1);
lcd.print("Enviando . ");
delay(100);
lcd.setCursor(0, 1);
lcd.print("Enviando .. ");
delay(100);
lcd.setCursor(0, 1);
lcd.print("Enviando ... ");
enviarFlag = false;
}