Switch Matrix inputs

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.

Still not working like I want it.
Already managed, with another sketch, to let both LEDs burn, but not separated :grin:.

What am i'm missing? :frowning: This is the kind of style/setup I wan't to use;

(NOT WORKING)

byte byteRead;
const int ledPin = 10;
const int ledPin2 = 11;

void setup() {                

  Serial.begin(9600);
}

void loop() {

  if (Serial.available()) {

    byteRead = Serial.read();

    if(byteRead==128){
      digitalWrite(ledPin, HIGH);
    }
    
    if(byteRead==129){
      digitalWrite(ledPin2, HIGH);
    }
  }
}

Here is one problem with your code:

Serial.begin(9600);

From the page you linked to:

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.

:grin: I'm officially stupid XD Fixed the 9600 to 57600 8).

Also made the serial-range bigger from == 128 and ==129 to > 1 and >2 . Shield still reading the inputs (led blinks) but no output.

byte byteRead;
const int ledPin = 10;
const int ledPin2 = 11;

void setup() {                

  Serial.begin(57600);
}

void loop() {

  if (Serial.available()) {

    byteRead = Serial.read();

    if(byteRead > 1){
      digitalWrite(ledPin, HIGH);
    }
    
    if(byteRead > 2){
      digitalWrite(ledPin2, HIGH);
    }
  }
}

This is my 2nd main sketch, which looks ok, but just let the 2 leds burn constantly

const int ledPin = 10;
const int ledPin2 = 11;
int buttonState1 = 0;
int buttonState2 = 0;

void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  Serial.begin(57600);
}

volatile uint8_t Button = 0;

void loop()
{
  if (Serial.available() > 0)
    Button = Serial.read();
  buttonState1 = digitalRead(Button==128);
  buttonState2 = digitalRead(Button==129);


  if (buttonState1 == HIGH) {
    digitalWrite(ledPin, HIGH); // Turn LED on
  }
  if (buttonState1 == LOW)
  {
    digitalWrite(ledPin, LOW); // Turn LED off
    
  }
  if (buttonState2 == HIGH) {
    digitalWrite(ledPin2, HIGH); // Turn LED2 on
  }
  if (buttonState2 == LOW)
  {
    digitalWrite(ledPin2, LOW); // Turn LED2 off
  }
}

Both sketches are wrong, in different ways. The first one never turns the LEDs off. The second one makes nonsensical digitalRead calls. Try this:

const int ledPin = 10;
const int ledPin2 = 11;

void setup() {                
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  Serial.begin(57600);
}

void loop() 
{
  if (Serial.available()) 
  {
    byte byteRead = Serial.read();
    switch(byteRead)
    {
    case 1:  // button 1 released
      digitalWrite(ledPin, LOW);
      break;
    case 2:  // button 2 released
      digitalWrite(ledPin2, LOW);
      break;
    case 129:  // button 1 pressed
      digitalWrite(ledPin, HIGH);
      break;
    case 130:  // button 2 pressed
      digitalWrite(ledPin2, HIGH);
      break;
    }
  }
}

U made my day sir!
Looking and puzzeling for this basic code for about a small week now.

One last thing, how would the following look with the case-statements?

void loop()
{
    byte a1State = digitalRead(Pin1) && !digitalRead(Pin2) && !digitalRead(Pin3);
    digitalWrite(ledPin, a1State);
}

Totally wrong, but you get the idea what i'm looking after :grin:

{
     case 129 && case 2 && case 3:  // button 1 pressed and button 2 and 3 released
     digitalWrite(ledPin, HIGH);
}

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).

Back with some problems :grin:

Since I need > 40-50 small lamp outputs, I bought some mcp23017.

  1. 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 ?

  2. 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;
    }
  }
}