Button matrix with serial communication code errors

Hi all,
Very new to arduino and coding so please bear with me. I’ve started a project build that has codes supplied (still very new to programming), however not all the codes compile, so things are at a standstill. Wondering if anyone can shed any light on how these errors could be resolved. Any help/ideas would be much appreciated. From my little knowledge, it seems there’s an error within this last section with undeclared variables and incorrect comparators… (?)

It doesn’t state there are any library files attached, so they’ve forgotten to delcare ledstate, and ledy somewhere? Any ideas on how to get this working? It’s supposed to light up a button (in a 4x4 led button matrix) when pressed and send serial data out for each button press. Button matrix is connected and powered via shift registers to the arduino. The hardware side is working fine, just can’t get any serial data out from the buttons.

for (k=0;k<4;k++){
buttonCheck(i,k);
if (buttonEvent*<> 1) & 3;*

  • byte ledx = (ledByte >> 3) & 3;*
  • if (ledstate){*
  • ledData[ledy] |= 8 >> ledx;*
  • }*
  • else{*
  • ledData[ledy] &= ~ (8 >> ledx);*
  • }*
  • }//end if serial available*
  • }//end do*
    while (Serial.available() > 8);
    *} *
    void loop() {
  • shift();//updates leds and receives data from buttons[/color]*
    ```
    *//BUTTON TEST w/ 74HC595 and 74HC165 and serial communication
    //by Amanda Ghassaei
    //June 2012

/*

  • This program is free software; you can redistribute it and/or modify
  • it under the terms of the GNU General Public License as published by
  • the Free Software Foundation; either version 2 of the License, or
  • (at your option) any later version.

*/

//this firmware will send data back and forth with the maxmsp patch “beat slicer”

//pin connections
int ledLatchPin = 6;
int ledClockPin = 5;
int ledDataPin = 7;
int buttonLatchPin = 4;
int buttonClockPin = 3;
int buttonDataPin = 2;

//looping variables
byte i;
byte j;
byte k;
byte ledByte;

//storage for led states, 4 bytes
byte ledData = {0, 0, 0, 0};
//storage for buttons, 4 bytes
byte buttonCurrent = {0,0,0,0};
byte buttonLast = {0,0,0,0};
byte buttonEvent = {0,0,0,0};
byte buttonState = {0,0,0,0};
//button debounce counter- 16 bytes
byte buttonDebounceCounter[4][4];

void setup() {
 DDRD = 0xFA;//set pins D7-D4 as output, D2 as input
 
 Serial.begin(57600);
 
 cli();//stop interrupts

//set timer2 interrupt every 128us
 TCCR2A = 0;// set entire TCCR2A register to 0
 TCCR2B = 0;// same for TCCR2B
 TCNT2  = 0;//initialize counter value to 0
 // set compare match register for 7.8khz increments
 OCR2A = 255;// = (1610^6) / (7812.58) - 1 (must be <256)
 // turn on CTC mode
 TCCR2A |= (1 << WGM21);
 // Set CS11 bit for 8 prescaler
 TCCR2B |= (1 << CS11);  
 // enable timer compare interrupt
 TIMSK2 |= (1 << OCIE2A);
 
 sei();//allow interrupts
 
}

// buttonCheck - checks the state of a given button.
//this buttoncheck function is largely copied from the monome 40h firmware by brian crabtree and joe lake
void buttonCheck(byte row, byte index)
{
 if (((buttonCurrent[row] ^ buttonLast[row]) & (1 << index)) &&   // if the current physical button state is different from the
 ((buttonCurrent[row] ^ buttonState[row]) & (1 << index))) {  // last physical button state AND the current debounced state

if (buttonCurrent[row] & (1 << index)) {                      // if the current physical button state is depressed
     buttonEvent[row] = 1 << index;              // queue up a new button event immediately
     buttonState[row] |= (1 << index);                         // and set the debounced state to down.
 }
   else{
     buttonDebounceCounter[row][index] = 12;
   }  // otherwise the button was previously depressed and now
   // has been released so we set our debounce counter.
 }
 else if (((buttonCurrent[row] ^ buttonLast[row]) & (1 << index)) == 0 &&  // if the current physical button state is the same as
 (buttonCurrent[row] ^ buttonState[row]) & (1 << index)) {        // the last physical button state but the current physical
   // button state is different from the current debounce
   // state…
   if (buttonDebounceCounter[row][index] > 0 && --buttonDebounceCounter[row][index] == 0) {  // if the the debounce counter has
     // been decremented to 0 (meaning the
     // the button has been up for
     // kButtonUpDefaultDebounceCount
     // iterations///

buttonEvent[row] = 1 << index;    // queue up a button state change event

if (buttonCurrent[row] & (1 << index)){          // and toggle the buttons debounce state.
       buttonState[row] |= (1 << index);
     }
     else{
       buttonState[row] &= ~(1 << index);
     }
   }
 }
}

void shift(){
 
 for (i=0;i<4;i++){
   
   buttonLast[i] = buttonCurrent[i];
   
   byte dataToSend = (1 << (i+4)) | (15 & ~ledData[i]);
     
   // set latch pin low so the LEDs don’t change while sending in bits
   digitalWrite(ledLatchPin, LOW);
   // shift out the bits of dataToSend
   shiftOut(ledDataPin, ledClockPin, LSBFIRST, dataToSend);  
   //set latch pin high so the LEDs will receive new data
   digitalWrite(ledLatchPin, HIGH);
     
   //once one row has been set high, receive data from buttons
   //set latch pin high
   digitalWrite(buttonLatchPin, HIGH);
   //shift in data
   buttonCurrent[i] = shiftIn(buttonDataPin, buttonClockPin, LSBFIRST) >> 3;
   //latchpin low
   digitalWrite(buttonLatchPin, LOW);
   
[color=red]    for (k=0;k<4;k++){
     buttonCheck(i,k);
     if (buttonEvent[i]<> 1) & 3;
     byte ledx = (ledByte >> 3) & 3;
     if (ledstate){
       ledData[ledy] |= 8 >> ledx;
     }
     else{
       ledData[ledy] &= ~ (8 >> ledx);
     }
   }//end if serial available
 }//end do
while (Serial.available() > 8);
}

void loop() {
 shift();//updates leds and receives data from buttons[/color]
}*
```

The problem statement is not clear. can you put schematic and what your are trying todo here.

You want to check which button being pressed right or wrong??? also try to share datasheet for product used.

      if (buttonEvent[i]<> 1) & 3;

Try explaining in English what you think you are doing here.

I didn't write this code - I'm new to arduino.

I am recreating someone elses build with their codes, but several codes they have provided have errors, this being one of them.

This is the button matrix I am using https://www.sparkfun.com/products/8033 Shift register setup http://cdn.instructables.com/FBV/DOQ4/HLFUY2MQ/FBVDOQ4HLFUY2MQ.MEDIUM.jpg http://cdn.instructables.com/FXG/YH57/HLFUY2MN/FXGYH57HLFUY2MN.LARGE.jpg

The code is supposed to light up a button when pressed, and send serial data out for each button press. It is the latter that doesn't work.

This is the complete project link http://www.instructables.com/id/Sugarcube-MIDI-Controller/?ALLSTEPS

I am recreating someone elses build with their codes, but several codes they have provided have errors, this being one of them.

Well, don't. Ditch that crap, and write your own code.

I see how to delcare ledy, and ledstate

byte ledy;
byte ledstate;

if (buttonEvent[i]<> 1) & 3;

should it be less than 1 ?

 if (buttonEvent[i] << 1);

No idea what the function of this line is. Something to do wiht the state of the buttons declared earlier?

PaulS - Sure, if I had months and months I would. Unfortunately, I’ve started this build as a university project, and I’m nearing the deadline. It’s (supposed to be) a midi controller for a live music performance this week. This is my last attempt to get it working before I need to ditch it and go with a less impressive plan B. Thanks anyway.

should it be less than 1 ?

How should I know. It's you code.

An if statement that ends in a semicolon is pretty much useless, so I'd just delete the whole line.

Performing a bitwise and on the result of an if statement seems pointless.

I can't even imagine what you thought you (or some other addle-pated twit) were doing with that bit of code, so I can't imagine what the code should look like.

I've started this build as a university project, and I'm nearing the deadline. It's (supposed to be) a midi controller for a live music performance this week

This sounds all too familiar I am afraid. The programming is left until the last minute because it will be either

(a) Easy to do. or (b) someone else will have done it before and you can use their code.

As you have discovered neither of these is true. If you need help please share your requirments and circuit diagram here.

Cheers UKHeliBob, appreciate any help you can offer.

Schematics

Button matrix > to shift registers > to arduino (hand drawn- sorry). Is this sufficient?
http://cdn.instructables.com/FBV/DOQ4/HLFUY2MQ/FBVDOQ4HLFUY2MQ.MEDIUM.jpg
http://cdn.instructables.com/FXG/YH57/HLFUY2MN/FXGYH57HLFUY2MN.LARGE.jpg

Requirement: Arduino send (and receive) info serially ie. when a button is pressed (and have the corresponding led light up (if possible)). Code includes “a timer interrupt to keep the Arduino responsive to incoming serial messages”.

PS. With the serial communication sections extracted, the code succeeds in lighting up the button that’s pressed.

//BUTTON TEST w/ 74HC595 and 74HC165 and serial communication
//by Amanda Ghassaei
//June 2012

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
*/

//this firmware will send data back and forth with the maxmsp patch "beat slicer"

//pin connections
int ledLatchPin = 6;
int ledClockPin = 5;
int ledDataPin = 7;
int buttonLatchPin = 4;
int buttonClockPin = 3;
int buttonDataPin = 2;

//looping variables
byte i;
byte j;
byte k;
byte ledByte;

//storage for led states, 4 bytes
byte ledData[] = {0, 0, 0, 0};
//storage for buttons, 4 bytes
byte buttonCurrent[] = {0,0,0,0};
byte buttonLast[] = {0,0,0,0};
byte buttonEvent[] = {0,0,0,0};
byte buttonState[] = {0,0,0,0};
//button debounce counter- 16 bytes
byte buttonDebounceCounter[4][4];


void setup() {
  DDRD = 0xFA;//set pins D7-D4 as output, D2 as input
  
  Serial.begin(57600);
  
  cli();//stop interrupts

  //set timer2 interrupt every 128us
  TCCR2A = 0;// set entire TCCR2A register to 0
  TCCR2B = 0;// same for TCCR2B
  TCNT2  = 0;//initialize counter value to 0
  // set compare match register for 7.8khz increments
  OCR2A = 255;// = (16*10^6) / (7812.5*8) - 1 (must be <256)
  // turn on CTC mode
  TCCR2A |= (1 << WGM21);
  // Set CS11 bit for 8 prescaler
  TCCR2B |= (1 << CS11);   
  // enable timer compare interrupt
  TIMSK2 |= (1 << OCIE2A);
  
  sei();//allow interrupts
  
}

// buttonCheck - checks the state of a given button.
//this buttoncheck function is largely copied from the monome 40h firmware by brian crabtree and joe lake
void buttonCheck(byte row, byte index)
{
  if (((buttonCurrent[row] ^ buttonLast[row]) & (1 << index)) &&   // if the current physical button state is different from the
  ((buttonCurrent[row] ^ buttonState[row]) & (1 << index))) {  // last physical button state AND the current debounced state

    if (buttonCurrent[row] & (1 << index)) {                      // if the current physical button state is depressed
      buttonEvent[row] = 1 << index;              // queue up a new button event immediately
      buttonState[row] |= (1 << index);                         // and set the debounced state to down.
  }
    else{
      buttonDebounceCounter[row][index] = 12;
    }  // otherwise the button was previously depressed and now
    // has been released so we set our debounce counter.
  }
  else if (((buttonCurrent[row] ^ buttonLast[row]) & (1 << index)) == 0 &&  // if the current physical button state is the same as
  (buttonCurrent[row] ^ buttonState[row]) & (1 << index)) {        // the last physical button state but the current physical
    // button state is different from the current debounce 
    // state...
    if (buttonDebounceCounter[row][index] > 0 && --buttonDebounceCounter[row][index] == 0) {  // if the the debounce counter has
      // been decremented to 0 (meaning the
      // the button has been up for 
      // kButtonUpDefaultDebounceCount 
      // iterations///

      buttonEvent[row] = 1 << index;    // queue up a button state change event

      if (buttonCurrent[row] & (1 << index)){          // and toggle the buttons debounce state.
        buttonState[row] |= (1 << index);
      }
      else{
        buttonState[row] &= ~(1 << index);
      }
    }
  }
}


void shift(){
  
  for (i=0;i<4;i++){
    
    buttonLast[i] = buttonCurrent[i];
    
    byte dataToSend = (1 << (i+4)) | (15 & ~ledData[i]);
      
    // set latch pin low so the LEDs don't change while sending in bits
    digitalWrite(ledLatchPin, LOW);
    // shift out the bits of dataToSend
    shiftOut(ledDataPin, ledClockPin, LSBFIRST, dataToSend);  
    //set latch pin high so the LEDs will receive new data
    digitalWrite(ledLatchPin, HIGH);
      
    //once one row has been set high, receive data from buttons
    //set latch pin high
    digitalWrite(buttonLatchPin, HIGH);
    //shift in data
    buttonCurrent[i] = shiftIn(buttonDataPin, buttonClockPin, LSBFIRST) >> 3;
    //latchpin low
    digitalWrite(buttonLatchPin, LOW);
    
    byte ledstate;
    byte ledy;
    
 for (k=0;k<4;k++){
      buttonCheck(i,k);
      if (buttonEvent[i]<> 1) & 3;
      byte ledx = (ledByte >> 3) & 3;
      if (ledstate){
        ledData[ledy] |= 8 >> ledx;
      }
      else{
        ledData[ledy] &= ~ (8 >> ledx);
      }
    }//end if serial available
  }//end do
 while (Serial.available() > 8);
}    


void loop() {
  shift();//updates leds and receives data from buttons
}

timer interrupt section

  Serial.begin(57600);
  
  cli();//stop interrupts

  //set timer2 interrupt every 128us
  TCCR2A = 0;// set entire TCCR2A register to 0
  TCCR2B = 0;// same for TCCR2B
  TCNT2  = 0;//initialize counter value to 0
  // set compare match register for 7.8khz increments
  OCR2A = 255;// = (16*10^6) / (7812.5*8) - 1 (must be <256)
  // turn on CTC mode
  TCCR2A |= (1 << WGM21);
  // Set CS11 bit for 8 prescaler
  TCCR2B |= (1 << CS11);   
  // enable timer compare interrupt
  TIMSK2 |= (1 << OCIE2A);
  
  sei();//allow interrupts
  
}

From what I can ascertain, this is the last section of the serial comm portion (with errors/incomplete). Those I’ve asked can’t seem to make any sense of it…

byte ledy;
byte ledstate;

for (k=0;k<4;k++){
      buttonCheck(i,k);
      if (buttonEvent[i]<> 1) & 3;
      byte ledx = (ledByte >> 3) & 3;
      if (ledstate){
        ledData[ledy] |= 8 >> ledx;
      }
      else{
        ledData[ledy] &= ~ (8 >> ledx);
      }
    }//end if serial available
  }//end do
 while (Serial.available() > 8);
}    


void loop() {
  shift();//updates leds and receives data from buttons
}

This is a simpler, working version of the code above - buttons light up when pressed but no serial output. Apologies if it’s not relevant.

//BUTTON TEST w/ 74HC595 and 74HC165
//by Amanda Ghassaei 2012

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
*/

//this firmware will cause the buttons to light up momentarily while they are pressed.

//pin connections
int ledLatchPin = 6;
int ledClockPin = 5;
int ledDataPin = 7;
int buttonLatchPin = 4;
int buttonClockPin = 3;
int buttonDataPin = 2;

//looping variables
byte i;
byte j;
byte k;

//storage for led states, 4 bytes
byte ledData[] = {0, 0, 0, 0};
//storage for buttons, 4 bytes
byte buttonCurrent[] = {0,0,0,0};
byte buttonLast[] = {0,0,0,0};
byte buttonEvent[] = {0,0,0,0};
byte buttonState[] = {0,0,0,0};
//button debounce counter- 16 bytes
byte buttonDebounceCounter[4][4];


void setup() {
  DDRD = 0xFA;//set pins D7-D4 as output, D2 as input
}

// buttonCheck - checks the state of a given button.
//this buttoncheck function is largely copied from the monome 40h firmware by brian crabtree and joe lake
void buttonCheck(byte row, byte index)
{
  if (((buttonCurrent[row] ^ buttonLast[row]) & (1 << index)) &&   // if the current physical button state is different from the
  ((buttonCurrent[row] ^ buttonState[row]) & (1 << index))) {  // last physical button state AND the current debounced state

    if (buttonCurrent[row] & (1 << index)) {                      // if the current physical button state is depressed
      buttonEvent[row] = 1 << index;              // queue up a new button event immediately
      buttonState[row] |= (1 << index);                         // and set the debounced state to down.
  }
    else{
      buttonDebounceCounter[row][index] = 12;
    }  // otherwise the button was previously depressed and now
    // has been released so we set our debounce counter.
  }
  else if (((buttonCurrent[row] ^ buttonLast[row]) & (1 << index)) == 0 &&  // if the current physical button state is the same as
  (buttonCurrent[row] ^ buttonState[row]) & (1 << index)) {        // the last physical button state but the current physical
    // button state is different from the current debounce 
    // state...
    if (buttonDebounceCounter[row][index] > 0 && --buttonDebounceCounter[row][index] == 0) {  // if the the debounce counter has
      // been decremented to 0 (meaning the
      // the button has been up for 
      // kButtonUpDefaultDebounceCount 
      // iterations///

      buttonEvent[row] = 1 << index;    // queue up a button state change event

      if (buttonCurrent[row] & (1 << index)){          // and toggle the buttons debounce state.
        buttonState[row] |= (1 << index);
      }
      else{
        buttonState[row] &= ~(1 << index);
      }
    }
  }
}


void shift(){
  
  for (i=0;i<4;i++){
    
    buttonLast[i] = buttonCurrent[i];
    
    byte dataToSend = (1 << (i+4)) | (15 & ~ledData[i]);
      
    // set latch pin low so the LEDs don't change while sending in bits
    digitalWrite(ledLatchPin, LOW);
    // shift out the bits of dataToSend
    shiftOut(ledDataPin, ledClockPin, LSBFIRST, dataToSend);  
    //set latch pin high so the LEDs will receive new data
    digitalWrite(ledLatchPin, HIGH);
      
    //once one row has been set high, receive data from buttons
    //set latch pin high
    digitalWrite(buttonLatchPin, HIGH);
    //shift in data
    buttonCurrent[i] = shiftIn(buttonDataPin, buttonClockPin, LSBFIRST) >> 3;
    //latchpin low
    digitalWrite(buttonLatchPin, LOW);
    
    for (k=0;k<4;k++){
      buttonCheck(i,k);
    }
  }
}

void updateLEDs(){ //update the leds to reflect hte state of the buttons
  for (j=0;j<4;j++){
    ledData[j] = buttonState[j];
  }
}

void loop() {
  shift();
  updateLEDs();
}