You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// 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
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);
}
}
The text was updated successfully, but these errors were encountered:
uint16_t tempRegDir = readWord(REG_DIR_B);
if ((0xFFFF ^ tempRegDir) & (1 << pin)) // If the pin is an output, write high/low
{
uint16_t tempRegData = readWord(REG_DATA_B);
if (highLow)
tempRegData |= (1 << pin);
else
tempRegData &= ~(1 << pin);
return writeWord(REG_DATA_B, tempRegData);
}
If we write something to the REG_DATA_B register and immediately call uint16_t tempRegData = readWord(REG_DATA_B);, the register still shows the old values. A delay of 25ms between the write and read solved my issue.
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);
}
}
The text was updated successfully, but these errors were encountered: