I’m using the I2C bus to read the keys in from the bass pedal. The breadboard layout is shown in an earlier post. To make sure that everything is working properly with the chips I have set up a piece of code that will read the sixteen pins in I2C chips and display their output via the Arduino serial monitor.
The code is here:
// MultiButtonTest.ino
// Driving MIDI using a Multiple Buttons
// Rob Ives 2012
// This code is released into the Public Domain.
#include <MIDI.h>
#include <Wire.h>
int keyispressed[16]; //Variable. Is the key currently pressed?
int noteisplaying[16]; //Variable. Is the Note currently playing?
unsigned char data1; //data from chip 1
unsigned char data2; //data from chip 2
void setup() //The Setup Loop
{
Wire.begin(); // setup the I2C bus
Serial.begin(9600); // serial set up for debugging
for (unsigned int i = 0; i < 16; i++) {
keyispressed[i] = 1; //clear the keys array (High is off)
noteisplaying[i] = 0; //no notes are playing
}
//MIDI.begin(); //initialise midi library
}
//---------------------------------------------
void loop() //the main loop
{
readkeys();
displaykeys();
}
//-------------------------------------
void readkeys() {
Wire.requestFrom(0x38, 1); // read the data from chip 1 into data1
if (Wire.available()){
data1 = Wire.read();
}
Wire.requestFrom(0x39, 1); // read the data freom chip 2 into data2
if (Wire.available()){
data2 = Wire.read();
}
for (unsigned char i = 0; i < 8; i++) {// puts data bits from chip 1 into keys array
keyispressed[i] = ((data1 >> i) & 1); // set the key variable to the current state. chip 1
keyispressed[i + 8] = ((data2 >> i) & 1); //chip 2
}
}
//-------------------------------------
void displaykeys() { //Display the keys presssed for debugging purposes
for (unsigned char i = 0; i < 16; i++) {
Serial.print(keyispressed[i]); //print out each value from the array on one line.
}
Serial.println("<<");
}
Wire up the chip and run the sketch.
In the Arduino environment on your computer go to Tools -> Serial Monitor
where you should see a display spitting out rows of sixteen ones over and over.
Try connecting each of the data pins on the I2C chips to ground and if all is well, the corresponding one will turn to a zero.