I was able to get three LEDs light blinking from Arduino Nano to SX1509 just fine. But I could not figure out how to write the code to add three buttons. Here are my codes:
#include <Wire.h> // Include the I2C library (required)
#include <SparkFunSX1509.h> // Include SX1509 library
// const byte SX1509_ADDRESS = 0x3E; // SX1509 I2C address
SX1509 io; // Create an SX1509 object
const int SX1509_LED_PIN13 = 13; // LED connected to 15 (source ing current)
const int SX1509_LED_PIN14 = 14; // LED connected to 15 (source ing current)
const int SX1509_LED_PIN15 = 15; // LED connected to 15 (source ing current)
const int SX1509_BTN_PIN5 = 5; // Button connected to 0 (active-low)
const int SX1509_BTN_PIN6 = 6; // Button connected to 0 (active-low)
const int SX1509_BTN_PIN7 = 7; // Button connected to 0 (active-low)
bool ledState = false;
void setup()
{
io.pinMode(SX1509_LED_PIN13, OUTPUT);
io.pinMode(SX1509_LED_PIN14, OUTPUT);
io.pinMode(SX1509_LED_PIN15, OUTPUT);
io.pinMode(SX1509_BTN_PIN5, INPUT_PULLUP);
io.pinMode(SX1509_BTN_PIN6, INPUT_PULLUP);
io.pinMode(SX1509_BTN_PIN7, INPUT_PULLUP);
}
void loop()
{
if (io.digitalRead(SX1509_BTN_PIN5) == LOW)
{
// If the button is pressed toggle the LED:
ledState = !ledState;
io.digitalWrite(SX1509_LED_PIN13, ledState);
while (io.digitalRead(SX1509_BTN_PIN5 ) == LOW)
; // Wait for button to release
}
if (io.digitalRead(SX1509_BTN_PIN6) == LOW)
{
ledState = !ledState;
io.digitalWrite(SX1509_LED_PIN14, ledState);
while (io.digitalRead(SX1509_BTN_PIN6) == LOW);
}
if (io.digitalRead(SX1509_BTN_PIN7) == LOW)
{
ledState = !ledState;
io.digitalWrite(SX1509_LED_PIN15, ledState);
while (io.digitalRead(SX1509_BTN_PIN7) == LOW);
}
}
(Code tags added by Moderator)