Hi there,
as I am new at forums and with arduino, I have next issue with the 64 button shield for arduino from spiekenzelabs:
they have a example code for up to 64 buttons handled by the shield in in SPI mode(there is a jumper that lets switch between SPI and Serial communication):
CODE:
//*******************************************************************************************************************
// VARIABLE INITS
//*******************************************************************************************************************
volatile uint8_t Button = 0; // Required for 64 Button Shield (SPI Only)
//*******************************************************************************************************************
// Set-Up
//*******************************************************************************************************************
void setup()
{
Serial.begin(115200); // Only used to send data to PC for this DEMO
delay(1000);
attachInterrupt(0, SPI64B, FALLING); // Required for 64 Button Shield (SPI Only)
}
//*******************************************************************************************************************
// Main Loop
//*******************************************************************************************************************
void loop()
{
if(Button > 0) // If Button is > 0, then it was pressed or released (SPI only)
{
Serial.print("Button: ");
if(Button > 128) // Example of how to decode the button press
{
Button = Button - 128; // A pressd button is the button number + 128
Serial.print(Button, DEC);
Serial.print(" - Pressed");
}else
{
Serial.print(Button, DEC); // A released button is from 1 to 64
Serial.print(" - Released");
}
Serial.println(" ");
Button = 0;
}
}
//*******************************************************************************************************************
// Required for 64 Button Shield (SPI Only) Functions & Subroutines
//*******************************************************************************************************************
//
// This void is called if the Interrupt 0 is triggered (digital pin 2).
//
void SPI64B()
{
Button = 0;
volatile uint8_t val = 0;
volatile uint8_t clk = 0;
#define DataInPin 3
#define ClkPin 4
clk = digitalRead(ClkPin);
while(clk == LOW)
{
clk = digitalRead(ClkPin);
}
for(volatile int i =0; i<8;i++)
{
val = digitalRead(DataInPin);
clk = digitalRead(ClkPin);
while(clk == HIGH)
{
clk = digitalRead(ClkPin);
}
if(val == HIGH)
{
Button = Button +1;
}
if(i != 7)
{
Button = Button << 1;
}
else
{
break;
}
clk = digitalRead(ClkPin);
while(clk == LOW)
{
clk = digitalRead(ClkPin);
}
}
} // End of SPI64B void
// -------------------------------------------------------------------------------------------------------------
What I do not understand is whay if I attacht this shield to an arduino UNO smd edition, works as expected(if you press a button on the 64 button matrix you are able to read the printed number of each button in the serial monitor), but if I do the same with my Arduino Mega 2560 rev 2, when I press a button I can't see anything on the serial monitor.
I made some modification to the code in order to watch what was happening:
//*******************************************************************************************************************
// VARIABLE INITS
//*******************************************************************************************************************
volatile uint8_t Button= 0;// Required for 64 Button Shield (SPI Only)
//*******************************************************************************************************************
// Set-Up
//*******************************************************************************************************************
void setup()
{
Serial.begin(115200); // Only used to send data to PC for this DEMO
delay(1000);
attachInterrupt(0, SPI64B, FALLING); // Required for 64 Button Shield (SPI Only)
}
//*******************************************************************************************************************
// Main Loop
//*******************************************************************************************************************
void loop()
{
if(Button > 0) // If Button is > 0, then it was pressed or released (SPI only)
{
Serial.print("Button: ");
if(Button > 127) // Example of how to decode the button press
{
Button = Button - 128; // A pressd button is the button number + 128
Serial.print(Button, DEC);
Serial.print(" - Pressed");
}else
{
Serial.print(Button, DEC); // A released button is from 1 to 64
Serial.print(" - Released");
}
Serial.println(" ");
Button = 0;
}
else{
Serial.print(Button); //Here I can see if it is not getting inside the if statement.
Serial.println(" ");
delay(1000);
}
}
//*******************************************************************************************************************
// Required for 64 Button Shield (SPI Only) Functions & Subroutines
//*******************************************************************************************************************
//
// This void is called if the Interrupt 0 is triggered (digital pin 2).
//
void SPI64B()
{
Button = 0;
volatile uint8_t val = 0;
volatile uint8_t clk = 0;
#define DataInPin 3
#define ClkPin 4
clk = digitalRead(ClkPin);
while(clk == LOW)
{
clk = digitalRead(ClkPin);
}
for(volatile int i =0; i<8;i++)
{
val = digitalRead(DataInPin);
clk = digitalRead(ClkPin);
while(clk == HIGH)
{
clk = digitalRead(ClkPin);
}
if(val == HIGH)
{
Button = Button +1;
}
if(i != 7)
{
Button = Button << 1;
}
else
{
break;
}
clk = digitalRead(ClkPin);
while(clk == LOW)
{
clk = digitalRead(ClkPin);
}
}
} // End of SPI64B void
// -------------------------------------------------------------------------------------------------------------
So only number 0 was displayed again and again on the serial monitor, but I can't understand why, Any ideas?
Here are the links from official site:
http://spikenzielabs.com/SpikenzieLabs/Button64Shield.html
Some more info:
http://shieldlist.org/spikenzie/b64
Something I am missing that I can't get it run as it should.
Connections are as they where supposed to be, the shield just plugged in top of Arduino Mega or Arduino UNO, only 64buttons matrix attached (with well connected diodes scheme).