Hi all, I'm making a project on a Nano that uses an SX1509 to read an array of 32 buttons that play individual sound files. Does the ezButton library support reading these buttons? Below are my two code samples. Program 1 is my code without ezButton. It runs, but the sound stutters if I hold the button down, which is not what I want. Program 2 is a snippet that I made for a prototype using a single button and ezButton. The stuttering effect is gone, but of course the button is hooked directly to a specific pin, which is no longer the case.
In the ezButton examples, there's the code for a button array, however those buttons are hooked directly to pins, which is not my case. So I'm a bit lost. Can I use ezButton to read from an SX1509? Thanks in advance for any guidance.
PROGRAM 1:
#include <Wire.h>
#include <SparkFunSX1509.h>
#include <DFPlayerMini_Fast.h>
// 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
#define KEY_ROWS 8
#define KEY_COLS 4
#if !defined(UBRR1H)
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
#endif
DFPlayerMini_Fast myMP3;
char keyMap[KEY_ROWS][KEY_COLS] = {
{'1', '2', '3', '4'},
{'5', '6', '7', '8'},
{'9', '10', '11', '12'},
{'13', '14', '15', '16'},
{'17', '18', '19', '20'},
{'21', '22', '23', '24'},
{'25', '26', '27', '28'},
{'29', '30', '31', '32'}};
const byte ARDUINO_INTERRUPT_PIN = 2;
unsigned int previousKeyData = 0; // Stores last key pressed
unsigned int releaseCount = 0; // Count durations
const unsigned int releaseCountMax = 100; // Release limit
void setup()
{
Serial.begin(115200);
// Serial.println("SX1509 Example");
Wire.begin();
#if !defined(UBRR1H)
mySerial.begin(9600);
myMP3.begin(mySerial, true);
#else
Serial1.begin(9600);
myMP3.begin(Serial1, true);
#endif
Serial.println("Setting volume to 20");
myMP3.volume(20);
myMP3.EQSelect(2);
// Call io.begin(<address>) to initialize the SX1509. If it successfully communicates, it'll return 1.
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.
}
// Sleep time range: 128 ms - 8192 ms (powers of 2) 0=OFF
byte sleepTime = 0;
// Scan time range: 1-128 ms, powers of 2
byte scanTime = 8; // Scan time per row, in ms
// Debounce time range: 0.5 - 64 ms (powers of 2)
byte debounceTime = 1; // Debounce time
// Scan time must be greater than debounce time!
io.keypad(KEY_ROWS, KEY_COLS,
sleepTime, scanTime, debounceTime);
pinMode(ARDUINO_INTERRUPT_PIN, INPUT_PULLUP);
}
void loop()
{
// If the SX1509 INT pin goes low, a keypad button has been pressed:
if (digitalRead(ARDUINO_INTERRUPT_PIN) == LOW)
{
// Use io.readKeypad() to get the raw keypad row/column
unsigned int keyData = io.readKeypad();
// Then use io.getRow() and io.getCol() to parse that data into row and column values.
byte row = io.getRow(keyData);
byte col = io.getCol(keyData);
// Then plug row and column into keyMap to get which key was pressed.
char key = keyMap[row][col];
// If it's a new key pressed
if (keyData != previousKeyData)
{
Serial.println(String(key)); // Print the key
// OPEN FRET
if (key == '1') // E - open
{
myMP3.play(1);
}
if (key == '2') // A - open
{
myMP3.play(2);
}
if (key == '3') // D - open
{
myMP3.play(3);
}
if (key == '4') // G - open
{
myMP3.play(4);
}
// FRET ONE
if (key == '5') // E -F
{
myMP3.play(5);
}
if (key == '6') // A - Bb
{
myMP3.play(6);
}
if (key == '7') // D - Eb
{
myMP3.play(7);
}
if (key == '8') // G - Ab
{
myMP3.play(8);
}
// FRET TWO
if (key == '9') // E - Gb
{
myMP3.play(9);
}
if (key == '10') // A - B
{
myMP3.play(10);
}
if (key == '11') // D - E
{
myMP3.play(11);
}
if (key == '12') // G - A
{
myMP3.play(12);
}
// FRET THREE
if (key == '13') // E - G
{
myMP3.play(13);
}
if (key == '14') // A - C
{
myMP3.play(14);
}
if (key == '15') // D - F
{
myMP3.play(15);
}
if (key == '16') // G - Bb
{
myMP3.play(16);
}
// FRET FOUR
if (key == '17') // E - Ab
{
myMP3.play(17);
}
if (key == '18') // A - Db
{
myMP3.play(18);
}
if (key == '19') // D - G
{
myMP3.play(19);
}
if (key == '20') // G - B
{
myMP3.play(20);
}
// FRET FIVE
if (key == '21') // E - A
{
myMP3.play(21);
}
if (key == '22') // A - D
{
myMP3.play(22);
}
if (key == '23') // D - G
{
myMP3.play(23);
}
if (key == '24') // G - C
{
myMP3.play(24);
}
// FRET SIX
if (key == '25') // E - Bb
{
myMP3.play(25);
}
if (key == '26') // A - Eb
{
myMP3.play(26);
}
if (key == '27') // D - Ab
{
myMP3.play(27);
}
if (key == '28') // G - Db
{
myMP3.play(28);
}
// FRET SEVEN
if (key == '29') // E - B
{
myMP3.play(29);
}
if (key == '30') // A - E
{
myMP3.play(30);
}
if (key == '31') // D - A
{
myMP3.play(31);
}
if (key == '32') // G - D
{
myMP3.play(32);
}
}
releaseCount = 0; // Clear the releaseCount variable
previousKeyData = keyData; // Update previousKeyData
}
// If no keys have been pressed we'll continuously increment releaseCount,
// eventually creating a release, once the count hits the max.
releaseCount++;
if (releaseCount >= releaseCountMax)
{
releaseCount = 0;
previousKeyData = 0;
}
delay(1); // Gives releaseCountMax a more intuitive unit
}
PROGRAM 2:
#include <DFPlayerMini_Fast.h>
#include <ezButton.h>
ezButton button(4);
int last_button_state = HIGH;
#if !defined(UBRR1H)
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
#endif
DFPlayerMini_Fast myMP3;
void setup()
{
Serial.begin(115200);
#if !defined(UBRR1H)
mySerial.begin(9600);
myMP3.begin(mySerial, true);
#else
Serial1.begin(9600);
myMP3.begin(Serial1, true);
#endif
Serial.println("Setting volume to 20");
myMP3.volume(20);
}
void loop()
{
button.loop();
if (last_button_state == HIGH && button.getStateRaw() == LOW)
{
myMP3.play(1);
last_button_state = LOW;
}
if (button.isReleased())
{
last_button_state = HIGH;
}
}