|
| 1 | +#include <Adafruit_NeoPixel.h> |
| 2 | +#include "Adafruit_Keypad.h" |
| 3 | + |
| 4 | +#define ROWS 5 // rows |
| 5 | +#define COLS 6 // columns |
| 6 | + |
| 7 | +#define NEOPIXEL_PIN 7 |
| 8 | +#define NUM_PIXELS (ROWS * COLS) |
| 9 | + |
| 10 | +Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_PIXELS, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800); |
| 11 | + |
| 12 | + |
| 13 | +//define the symbols on the buttons of the keypads |
| 14 | +char keys[ROWS][COLS] = { |
| 15 | + {'1','2','3','4','5','6'}, |
| 16 | + {'7','8','9','A','B','C'}, |
| 17 | + {'D','E','F','G','H','I'}, |
| 18 | + {'J','K','L','M','N','O'}, |
| 19 | + {'P','Q','R','S','T','U'} |
| 20 | +}; |
| 21 | + |
| 22 | +uint8_t rowPins[ROWS] = {6, 29, 28, 27, 26}; //connect to the row pinouts of the keypad |
| 23 | +uint8_t colPins[COLS] = {13, 12, 11, 10, 9, 8}; //connect to the column pinouts of the keypad |
| 24 | + |
| 25 | +//initialize an instance of class NewKeypad |
| 26 | +Adafruit_Keypad customKeypad = Adafruit_Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS); |
| 27 | + |
| 28 | +bool lit[ROWS*COLS] = {0}; |
| 29 | + |
| 30 | +void setup() { |
| 31 | + Serial.begin(115200); |
| 32 | + //while (!Serial); |
| 33 | + Serial.println("Ortho 5x6 keypad demo"); |
| 34 | + strip.begin(); |
| 35 | + strip.setBrightness(40); |
| 36 | + strip.show(); // Initialize all pixels to 'off' |
| 37 | + |
| 38 | + customKeypad.begin(); |
| 39 | + for (int i=0; i<ROWS*COLS; i++) { |
| 40 | + lit[i] = false; |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +uint8_t j=0; // color ticker |
| 45 | + |
| 46 | +void loop() { |
| 47 | + //Serial.println("Test NeoPixels"); |
| 48 | + customKeypad.tick(); |
| 49 | + |
| 50 | + while(customKeypad.available()){ |
| 51 | + keypadEvent e = customKeypad.read(); |
| 52 | + Serial.print((char)e.bit.KEY); |
| 53 | + if (e.bit.EVENT == KEY_JUST_PRESSED) { |
| 54 | + Serial.println(" pressed"); |
| 55 | + uint8_t row = e.bit.ROW; |
| 56 | + uint8_t col = e.bit.COL; |
| 57 | + Serial.print("Row: "); Serial.print(row); |
| 58 | + Serial.print(" col: "); Serial.print(col); |
| 59 | + Serial.print(" -> "); |
| 60 | + uint16_t keynum; |
| 61 | + if (row % 2 == 0) { // even row |
| 62 | + keynum = row * COLS + col; |
| 63 | + } else { // odd row the neopixels go BACKWARDS! |
| 64 | + keynum = row * COLS + (5 - col); |
| 65 | + } |
| 66 | + Serial.println(keynum); |
| 67 | + lit[keynum] = !lit[keynum]; // invert neopixel status |
| 68 | + } |
| 69 | + else if(e.bit.EVENT == KEY_JUST_RELEASED) { |
| 70 | + Serial.println(" released"); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + for(int i=0; i< strip.numPixels(); i++) { |
| 75 | + if (lit[i]) { |
| 76 | + strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255)); |
| 77 | + } else { |
| 78 | + strip.setPixelColor(i, 0); |
| 79 | + } |
| 80 | + } |
| 81 | + strip.show(); |
| 82 | + j++; |
| 83 | + |
| 84 | + delay(10); |
| 85 | +} |
| 86 | + |
| 87 | + |
| 88 | +// Input a value 0 to 255 to get a color value. |
| 89 | +// The colours are a transition r - g - b - back to r. |
| 90 | +uint32_t Wheel(byte WheelPos) { |
| 91 | + if(WheelPos < 85) { |
| 92 | + return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); |
| 93 | + } else if(WheelPos < 170) { |
| 94 | + WheelPos -= 85; |
| 95 | + return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); |
| 96 | + } else { |
| 97 | + WheelPos -= 170; |
| 98 | + return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); |
| 99 | + } |
| 100 | +} |
0 commit comments