#include <Keypad.h>
#define VERT_PIN A0
#define HORZ_PIN A1
#define SEL_PIN 10
#define ENC_PIN A2
#define B1 13
#define B2 12
#define B3 11
#define BT 0
#define LED_PIN A5
byte lastB1State = LOW;
byte lastB2State = LOW;
byte lastB3State = LOW;
byte lastButtonState = LOW;
byte ledState = LOW;
unsigned long debounceDuration = 50; // millis
unsigned long lastTimeB1StateChanged = 0;
unsigned long lastTimeB2StateChanged = 0;
unsigned long lastTimeB3StateChanged = 0;
int x = 0;
int y = 0;
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'a' },
{ '4', '5', '6', 'b' },
{ '7', '8', '9', 'c' },
{ '*', '0', '#', 'd' }
};
uint8_t colPins[COLS] = { 5, 4, 3, 2 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 9, 8, 7, 6 }; // Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(9600);
pinMode(VERT_PIN, INPUT);
pinMode(HORZ_PIN, INPUT);
pinMode(ENC_PIN, INPUT);
pinMode(SEL_PIN, INPUT_PULLUP);
pinMode(B1, INPUT_PULLUP);
pinMode(B2, INPUT_PULLUP);
pinMode(B3, INPUT_PULLUP);
pinMode(BT, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
}
void loop() {
char key = keypad.getKey();
int horz = analogRead(HORZ_PIN);
int vert = analogRead(VERT_PIN);
int enc = analogRead(ENC_PIN);
int depress = digitalRead(SEL_PIN);
int test = digitalRead(BT);
byte buttonState = digitalRead(B1);
if (millis() - lastTimeB1StateChanged > debounceDuration) {
byte b1State = digitalRead(B1);
if (b1State != lastB1State) {
lastTimeB1StateChanged = millis();
lastB1State = b1State;
if (b1State == LOW) {
Serial.println("a/a");
}
}
}
if (millis() - lastTimeB2StateChanged > debounceDuration) {
byte b2State = digitalRead(B2);
if (b2State != lastB2State) {
lastTimeB2StateChanged = millis();
lastB2State = b2State;
if (b2State == LOW) {
Serial.println("a/g");
}
}
}
if (millis() - lastTimeB3StateChanged > debounceDuration) {
byte b3State = digitalRead(B3);
if (b3State != lastB3State) {
lastTimeB3StateChanged = millis();
lastB3State = b3State;
if (b3State == HIGH) {
ledState = (ledState == HIGH) ? LOW : HIGH;
digitalWrite(LED_PIN, ledState);
Serial.println("Toggle arm");
}
}
}
if (horz < 510) {
Serial.println("right");
}
if (horz > 512) {
Serial.println("left");
}
if (vert < 510) {
Serial.println("down");
}
if (vert > 512) {
Serial.println("up");
}
if (depress < 1) {
Serial.println("depress");
delay(250);
}
//keypad as individual buttons
if (key == '1') {
Serial.println("1 press");
}
if (key == '2') {
Serial.println("2 press");
}
if (key == '3') {
Serial.println("3 press");
}
if (key == '4') {
Serial.println("4 press");
}
if (key == '5') {
Serial.println("5 press");
}
if (key == '6') {
Serial.println("6 press");
}
if (key == '7') {
Serial.println("7 press");
}
if (key == '8') {
Serial.println("8 press");
}
if (key == '9') {
Serial.println("9 press");
}
if (key == '0') {
Serial.println("0 press");
}
if (key == '*') {
Serial.println("* press");
}
if (key == '#') {
Serial.println("# press");
}
if (key == 'a') {
Serial.println("a press");
}
if (key == 'b') {
Serial.println("b press");
}
if (key == 'c') {
Serial.println("c press");
}
if (key == 'd') {
Serial.println("d press");
}
}