I know this is cheeky of me to ask, but if anyone can help me, I'd be hugely grateful!
My problem is concerning a key matrix.
I currently have 29 buttons working through 11 pins on my Arduino Uno. I'm powering my buttons in groups of 6, with the power cycling between the groups of 6 every 10 milliseconds, then reading to see if any keys are being pressed, and if so, playing the right note.
The problem is, for some bizarre reason the Arduino will only output notes on around 90% of the keys, and gets the groups in the wrong order. I've tried powering the groups in all sorts of different orders, but it simply fails to understand, and outputs the groups randomly.
I know the hardware is fine because I have a key-matrix test code I run, which reassures me that it's all wired up correctly.
If any of you have any theories, that would be wonderful.
Below is my code... Sorry for the length!
(It's actually longer than this. The below key matrix is only the first 2 sections, as the forum wouldn't ket me post the entire thing, but in actuality it has 5 sections.
This is the full code: Full Code - Pastebin.com)
// this constant won't change:
const byte buttonPin[30] = {
7,8,9,10,11,12,7,8,9,10,11,12,7,8,9,10,11,12,7,8,9,10,11,12,7,8,9,10,11,12};
const byte Power [5] = {
2,3,4,5,6};
// Variables will change:
byte buttonPushCounter[30] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // counter for the number of button presses
byte buttonState[30] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // Currentrent state of the button
byte lastButtonState[30] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // previous state of the button
int analogValue1 = 0; // value read from the pot;
const byte numReadings = 50;
int inputPin = A0;
byte oldBellowPressurePush = 0;
int BellowPressurePush = 0;
int BellowPressurePull = 0;
byte Old = 0;
byte Current = 0;
byte readings[numReadings]; // the readings from the analog input
byte index = 0; // the index of the Currentrent reading
int total = 0; // the running total
int average = 0; // the average
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin[0], INPUT);
pinMode(buttonPin[1], INPUT);
pinMode(buttonPin[2], INPUT);
pinMode(buttonPin[3], INPUT);
pinMode(buttonPin[4], INPUT);
pinMode(buttonPin[5], INPUT);
pinMode(Power[0], OUTPUT);
pinMode(Power[1], OUTPUT);
pinMode(Power[2], OUTPUT);
pinMode(Power[3], OUTPUT);
pinMode(Power[4], OUTPUT);
// initialize serial communication:
Serial.begin(31250);
}
void loop() {
total= total - readings[index]; // subtract the last reading:
readings[index] = analogRead(inputPin); // read from the sensor:
total= total + readings[index]; // add the reading to the total:
index = index + 1; // advance to the next position in the array:
if (index >= numReadings) // if we're at the end of the array...
index = 0; // ...wrap around to the beginning:
average = total / numReadings; // calculate the average and send it to the computer as ASCII digits
Current = total / numReadings;
BellowPressurePush = 120;
BellowPressurePull = 120;
delay(10);
// read the pushbutton input pin:
buttonState[0] = digitalRead(buttonPin[0]);
buttonState[1] = digitalRead(buttonPin[1]);
buttonState[2] = digitalRead(buttonPin[2]);
buttonState[3] = digitalRead(buttonPin[3]);
buttonState[4] = digitalRead(buttonPin[4]);
buttonState[5] = digitalRead(buttonPin[5]);
digitalWrite (Power[0], HIGH);
digitalWrite (Power[1], LOW);
digitalWrite (Power[2], LOW);
digitalWrite (Power[3], LOW);
digitalWrite (Power[4], LOW);
// compare the buttonState to its previous state
if (buttonState[0] != lastButtonState[0]) {
// if the state has changed, increment the counter
if (buttonState[0] == HIGH) {
if (Current < Old){
SendMidi (0x90, 45, BellowPressurePull); //A
SendMidi (0x80, 42, BellowPressurePush); //Fsharp
}
if (Current >= Old){
SendMidi (0x80, 45, BellowPressurePull); //A
SendMidi (0x90, 42, BellowPressurePush); //Fsharp
}
}
if (buttonState[0] == LOW) {
// if the Currentrent state is LOW then the button
// wend from on to off:
SendMidi (0x80, 45, BellowPressurePush); //A
SendMidi (0x80, 42, BellowPressurePush); //Fsharp
}
}
// save the Currentrent state as the last state,
//for next time through the loop
lastButtonState[0] = buttonState[0];
//HERE
// compare the buttonState to its previous state
if (buttonState[1] != lastButtonState[1]) {
// if the state has changed, increment the counter
if (buttonState[1] == HIGH) {
if (Current < Old){
SendMidi (0x90, 49, BellowPressurePull); //Csharp
SendMidi (0x80, 45, BellowPressurePush); //A
}
if (Current >= Old){
SendMidi (0x80, 49, BellowPressurePull); //Csharp
SendMidi (0x90, 45, BellowPressurePush); //A
}
}
if (buttonState[1] == LOW) {
// if the Currentrent state is LOW then the button
// wend from on to off:
SendMidi (0x80, 49, BellowPressurePush); //Csharp
SendMidi (0x80, 45, BellowPressurePush); //A
}
}
// save the Currentrent state as the last state,
//for next time through the loop
lastButtonState[1] = buttonState[1];
//HERE
// compare the buttonState to its previous state
if (buttonState[2] != lastButtonState[2]) {
// if the state has changed, increment the counter
if (buttonState[2] == HIGH) {
if (Current < Old){
SendMidi (0x90, 52, BellowPressurePull); //E
SendMidi (0x80, 50, BellowPressurePush); //D
}
if (Current >= Old){
SendMidi (0x80, 52, BellowPressurePull); //E
SendMidi (0x90, 50, BellowPressurePush); //D
}
}
if (buttonState[2] == LOW) {
// if the Currentrent state is LOW then the button
// wend from on to off:
SendMidi (0x80, 52, BellowPressurePush); //E
SendMidi (0x80, 50, BellowPressurePush); //D
}
}
// save the Currentrent state as the last state,
//for next time through the loop
lastButtonState[2] = buttonState[2];
//HERE
// compare the buttonState to its previous state
if (buttonState[3] != lastButtonState[3]) {
// if the state has changed, increment the counter
if (buttonState[3] == HIGH) {
if (Current < Old){
SendMidi (0x90, 55, BellowPressurePull); //G
SendMidi (0x80, 54, BellowPressurePush); //Fsharp
}
if (Current >= Old){
SendMidi (0x80, 55, BellowPressurePull); //G
SendMidi (0x90, 54, BellowPressurePush); //Fsharp
}
}
if (buttonState[3] == LOW) {
// if the Currentrent state is LOW then the button
// wend from on to off:
SendMidi (0x80, 55, BellowPressurePush); //G
SendMidi (0x80, 54, BellowPressurePush); //Fsharp
}
}
// save the Currentrent state as the last state,
//for next time through the loop
lastButtonState[3] = buttonState[3];
//HERE
// compare the buttonState to its previous state
if (buttonState[4] != lastButtonState[4]) {
// if the state has changed, increment the counter
if (buttonState[4] == HIGH) {
if (Current < Old){
SendMidi (0x90, 59, BellowPressurePull); //B
SendMidi (0x80, 57, BellowPressurePush); //A
}
if (Current >= Old){
SendMidi (0x80, 59, BellowPressurePull); //B
SendMidi (0x90, 57, BellowPressurePush); //A
}
}
if (buttonState[4] == LOW) {
// if the Currentrent state is LOW then the button
// wend from on to off:
SendMidi (0x80, 59, BellowPressurePush); //B
SendMidi (0x80, 57, BellowPressurePush); //A
}
}
// save the Currentrent state as the last state,
//for next time through the loop
lastButtonState[4] = buttonState[4];
//HERE
// compare the buttonState to its previous state
if (buttonState[5] != lastButtonState[5]) {
// if the state has changed, increment the counter
if (buttonState[5] == HIGH) {
if (Current < Old){
SendMidi (0x90, 61, BellowPressurePull); //Csharp
SendMidi (0x80, 62, BellowPressurePush); //D
}
if (Current >= Old){
SendMidi (0x80, 61, BellowPressurePull); //Csharp
SendMidi (0x90, 62, BellowPressurePush); //D
}
}
if (buttonState[3] == LOW) {
// if the Currentrent state is LOW then the button
// wend from on to off:
SendMidi (0x80, 61, BellowPressurePush); //Csharp
SendMidi (0x80, 62, BellowPressurePush); //D
}
}
// save the Currentrent state as the last state,
//for next time through the loop
lastButtonState[5] = buttonState[5];
delay(10);
Old = Current;
}
void SendMidi (int cmd, int value, int vel) {
Serial.write (cmd);
Serial.write (value);
Serial.write (vel);
}