My Button64shield from SpikenzieLabs has arrived, but i'm already stuck with my sketch. http://spikenzielabs.com/SpikenzieLabs/Button64Shield.html
On the page u can download some serial/SPI examples, but they are for a voiceshield or a monitor
I've been trying to make something really simple work, but without any result.
I only need the basic 64 switchmatrix-code for inputs, just 1 switch to control a single LED. If I got the basic sketch, I can build and adapt the rest myself.
For the serial mode there is virtually no special B64 code required. Simply have your Arduino handle incoming serial data. The B64’s baud rate is fixed at 57600bps.
To be completely general (i.e. handle all 64 possible switches), there are better ways than using a switch-statement. You could use something like this:
const int ledPin = 10;
const int ledPin2 = 11;
bool buttons[64];
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
Serial.begin(57600);
}
void loop()
{
if (Serial.available())
{
byte byteRead = Serial.read();
if (byteRead >= 1 && byteRead <= 64) // if a button has been released
{
buttons[byteRead - 1] = false;
}
else if (byteRead >= 129 && byteRead <= 192) // if a button has been pressed
{
buttons[byteRead - 129] = true;
}
}
bool a1State = buttons[0] && !buttons[1];
digitalWrite(ledPin, a1State ? HIGH : LOW);
}
[Note: using a 64-byte array of booleans is wasteful of RAM, since you actually need only 64 bits to record the button states; but the above will do unless/until you need to save RAM.]
gunske:
One last thing, how would the following look with the case-statements?
It wouldn't - you have a simple logic expression there producing a single result, and you can't sensibly replace that with a case statement (nor would there be any reason to try).
Since I need > 40-50 small lamp outputs, I bought some mcp23017.
If I use 2 mcp23017, is the output adressing then 0-7 & 8-15 and 16-23 & 24-31
or
Adafruit_MCP23017 mcp; and Adafruit_MCP23017 mcp2;
mcp.pinMode(0, OUTPUT); // led 1 and mcp2.pinMode(0, OUTPUT); // led 17 ?
The following isn't working, what am I missing?
#include <Wire.h>
#include <Adafruit_MCP23017.h>
Adafruit_MCP23017 mcp;
void setup() {
mcp.begin(); // use default address 0
mcp.pinMode(0, OUTPUT); // led 1
mcp.pinMode(1, OUTPUT); // led 2
Serial.begin(57600);
}
void loop()
{
if (Serial.available())
{
byte byteRead = Serial.read();
switch(byteRead)
{
case 1: // button 1 released
mcp.digitalWrite(0, LOW); // led 1 off
break;
case 9: // button 2 released
mcp.digitalWrite(1, LOW); //led 2 off
break;
case 129: // button 1 pressed
mcp.digitalWrite(0, HIGH); // led 1 on
break;
case 137: // button 2 pressed
mcp.digitalWrite(1, HIGH); // led 2 on
break;
}
}
}