/* main.cpp - Main loop for Arduino sketches Copyright (c) 2005-2013 Arduino Team. All right reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include /** * Hello world for Arduino * * Turn the built-in LED on for 500ms, then off for 500ms. * * Mostly used as a lesson in using the arduino-builder and uploading programs * to the Arduino UNO board. */ /** * 1. Set COL to INPUT_PULLUP mode (it reads HIGH by default) * 2. But diode on the row (direction?) [Leave it out for now...] * 3. Program Row to be LOW and OUTPUT, everything else an input * 4. Then column 1 should read LOW when button is pressed (LOW) * * - In sequence, set one row to OUTPUT and LOW, all other rows to INPUT * - check each column * - Any column reading LOW is pressed * - set row back to INPUT and move to next row */ #define DEBOUNCE_TIME 10 const byte ROWS = 2; const byte COLS = 2; const byte TOTAL_KEYS = ROWS * COLS; const byte row_pins[ROWS] = {2, 3}; const byte col_pins[COLS] = {6, 7}; // All the letters you need const char letters[TOTAL_KEYS] = {'A', 'B', 'C', 'D'}; void setup() { byte i; Serial.begin(9600); for (i = 0; i < COLS; i++) pinMode(col_pins[i], INPUT_PULLUP); for (i = 0; i < ROWS; i++) pinMode(row_pins[i], INPUT); } /** * Need structure to track key state: last known key setting and last time it * toggled its state */ unsigned long last_key_time[TOTAL_KEYS]; /** * TODO Expand this to an array-back bitmap (only really relevant when we move * beyond 32 keys) */ unsigned char last_key_setting = 0; /* TODO Remove the counter, just for debugging */ unsigned long counter = 0; void loop() { bool pressed; bool last; byte row, col, k; unsigned long now; /* For debouncing */ now = millis(); k = 0; for (row = 0; row < ROWS; row++) { /* Set pin to output, and set it LOW */ pinMode(row_pins[row], OUTPUT); digitalWrite(row_pins[row], LOW); /* Scan all columns */ for (col = 0; col < COLS; col++) { if (now - last_key_time[k] < DEBOUNCE_TIME) { k++; continue; } /* Read the key */ pressed = digitalRead(col_pins[col]) == LOW; last = (last_key_setting & (1 << k)) != 0; /* Check the k'th bit */ if (pressed != last) { /* Changed */ last_key_time[k] = now; /* Toggle the bit */ last_key_setting ^= (1 << k); if (pressed) { //Serial.print("Key pressed: ("); //Serial.print("") } else { Serial.println(letters[k]); // Serial.print("Key released: ("); // Serial.print("row="); // Serial.print(row); // Serial.print(",col="); // Serial.print(col); // Serial.println(")"); } } /* Consider next key */ k++; } pinMode(row_pins[row], INPUT); } } // Declared weak in Arduino.h to allow user redefinitions. int atexit(void (* /*func*/ )()) { return 0; } // Weak empty variant initialization function. // May be redefined by variant files. void initVariant() __attribute__((weak)); void initVariant() { } void setupUSB() __attribute__((weak)); void setupUSB() { } int main(void) { init(); initVariant(); #if defined(USBCON) USBDevice.attach(); #endif setup(); for (;;) { loop(); if (serialEventRun) serialEventRun(); } return 0; }