Skip to content

Keypad and Led Blink not working simultanously #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
rvalexeda opened this issue Mar 3, 2025 · 1 comment
Open

Keypad and Led Blink not working simultanously #30

rvalexeda opened this issue Mar 3, 2025 · 1 comment

Comments

@rvalexeda
Copy link

rvalexeda commented Mar 3, 2025

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);
}
}

@rvalexeda
Copy link
Author

Got the issue.

bool SX1509::writePin(uint8_t pin, uint8_t highLow)
{

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant