88 lines
1.9 KiB
Arduino
88 lines
1.9 KiB
Arduino
const byte ROWS = 4;
|
|
const byte COLS = 4;
|
|
|
|
char keys[ROWS][COLS] = {
|
|
{'1', '2', '3', 'A'},
|
|
{'4', '5', '6', 'B'},
|
|
{'7', '8', '9', 'C'},
|
|
{'*', '0', '#', 'D'}
|
|
};
|
|
|
|
byte rowPins[ROWS] = {9, 8, 6, 7};
|
|
byte colPins[COLS] = {5, 4, 2, 3};
|
|
const byte RELAY_PIN = 10;
|
|
|
|
const String CODE_ON = "1234";
|
|
const String CODE_OFF = "5678";
|
|
const byte CODE_LEN = 4;
|
|
|
|
String saisie = "";
|
|
bool relaisActif = false;
|
|
|
|
char lastKey = 0;
|
|
unsigned long lastTime = 0;
|
|
const unsigned long DEBOUNCE = 150;
|
|
|
|
char scanKeypad() {
|
|
for (byte r = 0; r < ROWS; r++) {
|
|
for (byte i = 0; i < ROWS; i++) {
|
|
pinMode(rowPins[i], INPUT_PULLUP);
|
|
}
|
|
pinMode(rowPins[r], OUTPUT);
|
|
digitalWrite(rowPins[r], LOW);
|
|
delayMicroseconds(10);
|
|
|
|
for (byte c = 0; c < COLS; c++) {
|
|
pinMode(colPins[c], INPUT_PULLUP);
|
|
delayMicroseconds(10);
|
|
if (digitalRead(colPins[c]) == LOW) {
|
|
while (digitalRead(colPins[c]) == LOW);
|
|
delay(20);
|
|
return keys[r][c];
|
|
}
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
pinMode(RELAY_PIN, OUTPUT);
|
|
digitalWrite(RELAY_PIN, HIGH);
|
|
Serial.println("Prêt.");
|
|
}
|
|
|
|
void loop() {
|
|
char key = scanKeypad();
|
|
if (key != 0) {
|
|
unsigned long now = millis();
|
|
if (key != lastKey || (now - lastTime) > DEBOUNCE) {
|
|
lastKey = key;
|
|
lastTime = now;
|
|
|
|
saisie += key;
|
|
Serial.print("Saisie : ");
|
|
Serial.println(saisie);
|
|
|
|
// Tronque si trop long
|
|
if (saisie.length() > CODE_LEN) {
|
|
saisie = saisie.substring(saisie.length() - CODE_LEN);
|
|
}
|
|
|
|
if (saisie == CODE_ON) {
|
|
relaisActif = true;
|
|
saisie = "";
|
|
Serial.println("Relais ON permanent.");
|
|
} else if (saisie == CODE_OFF) {
|
|
relaisActif = false;
|
|
saisie = "";
|
|
Serial.println("Relais OFF.");
|
|
}
|
|
}
|
|
} else {
|
|
lastKey = 0;
|
|
}
|
|
|
|
digitalWrite(RELAY_PIN, relaisActif ? LOW : HIGH);
|
|
}
|