Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
roafan
/
Skeemitehnika
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
033f7091
authored
Dec 02, 2023
by
roafan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Kood
parents
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
120 additions
and
0 deletions
Arduino kood
Arduino kood
0 → 100644
View file @
033f7091
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment