SX1509 with three buttons and three LEDs

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)

You wrote this code?
I think you copied it from some one without understanding anything about it.
Try and read the code.
See the three lines that define where the LEDs are?
Add another three using a different name and different pins.

Then do the same where you see the LEDs mentioned again in the setup function.

Do the same for the three push buttons.

You will also need to change your loop function to use these new buttons and LEDs.

Note that you shouldn’t have the same number of pin for the LED and push buttons.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.