Commit 033f7091 by roafan

Kood

parents
Showing with 120 additions and 0 deletions
const int rowPins[2] = {9, 10};
const int colPins[2] = {11, 12};
const int rows[2] = {4, 3};
const int cols[2] = {6, 5};
const int numRows = 2;
const int numCols = 2;
const int rowCount = 2;
const int colCount = 2;
const int debounceInterval = 50;
unsigned long lastDebounceTime = 0;
int lastButtonStates[2][2] = {HIGH, HIGH, HIGH, HIGH};
byte keys[colCount][rowCount];
int LedStates[numRows][numCols] = {1, 1, 1, 1};
void setup() {
Serial.begin(115200);
for (int x = 0; x < rowCount; x++) {
Serial.print(rows[x]);
Serial.println(" as input");
pinMode(rows[x], INPUT);
}
for (int x = 0; x < colCount; x++) {
Serial.print(cols[x]);
Serial.println(" as input-pullup");
pinMode(cols[x], INPUT_PULLUP);
}
for (int i = 0; i < numRows; i++) {
pinMode(rowPins[i], OUTPUT);
}
for (int j = 0; j < numCols; j++) {
pinMode(colPins[j], OUTPUT);
}
}
void readMatrix() {
unsigned long currentMillis = millis();
for (int colIndex = 0; colIndex < colCount; colIndex++) {
byte curCol = cols[colIndex];
pinMode(curCol, OUTPUT);
digitalWrite(curCol, LOW);
for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
byte rowCol = rows[rowIndex];
pinMode(rowCol, INPUT_PULLUP);
int buttonState = digitalRead(rowCol);
if (buttonState == LOW && lastButtonStates[colIndex][rowIndex] == HIGH &&
currentMillis - lastDebounceTime > debounceInterval) {
LedStates[colIndex][rowIndex] = !LedStates[colIndex][rowIndex];
lastDebounceTime = currentMillis;
}
lastButtonStates[colIndex][rowIndex] = buttonState;
pinMode(rowCol, INPUT);
}
pinMode(curCol, INPUT);
}
}
void printMatrix() {
for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
if (rowIndex < 10)
Serial.print(F("0"));
Serial.print(rowIndex);
Serial.print(F(": "));
for (int colIndex = 0; colIndex < colCount; colIndex++) {
Serial.print(keys[colIndex][rowIndex]);
if (colIndex < colCount)
Serial.print(F(", "));
}
Serial.println("");
}
Serial.println("");
}
void manageLed() {
for (int row = 0; row < numRows; row++) {
for (int col = 0; col < numCols; col++) {
for (int r = 0; r < numRows; r++) {
digitalWrite(rowPins[r], LOW);
}
for (int c = 0; c < numCols; c++) {
digitalWrite(colPins[c], LOW);
}
if (LedStates[row][col] == 0) {
digitalWrite(rowPins[row], HIGH);
digitalWrite(colPins[col], HIGH);
}
delay(1);
}
}
}
void loop() {
readMatrix();
if (Serial.available() > 0) {
char inputChar = Serial.read();
if (inputChar == '!') {
printMatrix();
}
}
manageLed();
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment