Description
I tried to merge "keypad example" code with "digitalwrite example". But LEDs are not blinking as expected.
Sample code attached
#include <Wire.h> // Include the I2C library (required)
#include <SparkFunSX1509.h> //Click here for the library: http://librarymanager/All#SparkFun_SX1509
// SX1509 I2C address (set by ADDR1 and ADDR0 (00 by default):
const byte SX1509_ADDRESS = 0x3E; // SX1509 I2C address
SX1509 io; // Create an SX1509 object to be used throughout
const byte SX1509_LED_PIN = 4;
#define KEY_ROWS 4 // Number of rows in the keypad matrix
#define KEY_COLS 2 // Number of columns in the keypad matrix
// keyMap maps row/column combinations to characters:
char keyMap[KEY_ROWS][KEY_COLS] = {
{'1', '2'},
{'4', '5'},
{'7', '8'},
{'*', '0'}};
void setup()
{
Serial.begin(115200);
Serial.println("SX1509 Example");
Wire.begin();
if (io.begin(SX1509_ADDRESS) == false)
{
Serial.println("Failed to communicate. Check wiring and address of SX1509.");
while (1)
; // If we fail to communicate, loop forever.
}
unsigned int sleepTime = 256;
byte scanTime = 2; // Scan time per row, in ms
byte debounceTime = 1; // Debounce time
io.keypad(KEY_ROWS, KEY_COLS, sleepTime, scanTime, debounceTime);
io.pinMode(SX1509_LED_PIN, OUTPUT);
io.pinMode(5, OUTPUT);
}
void loop()
{
io.digitalWrite(SX1509_LED_PIN, HIGH);
io.digitalWrite(5, HIGH);
delay(500); // Delay half-a-second
io.digitalWrite(SX1509_LED_PIN, LOW); // Set the I/O low
io.digitalWrite(5, LOW);
delay(500);
unsigned int keyData = io.readKeypad();
if (keyData != 0) // If a key was pressed:
{
byte row = io.getRow(keyData);
byte col = io.getCol(keyData);
char key = keyMap[row][col];
Serial.println("Row: " + String(row));
Serial.println("Col: " + String(col));
Serial.print("Key: ");
Serial.println(key);
}
}