Help with 2x2 matrix button board

Hello,

I have been working with sparkfuns 2x2 led matrix button board recently and I've gotten the lights to light up on their own, but as for the buttons, no dice. I'm sure its my code, could somebody take a peek?

/*
2x2 RGB LED Control
This is for control of Sparkfun's 2x2 RGB Button Pad.
http://www.sparkfun.com/commerce/product_info.php?products_id=9277

Original: Aaron Goselin (Nakor) Oct 27, 2010

Editor: Shannon Strutz Jan 28th, 2012

This code is in the public domain.
*/

// Button Grounds
const int buttonMatrix1 = A1;
const int buttonMatrix2 = A2;
const int buttonMatrix3 = A3;
const int buttonMatrix4 = A4;
// LED Grounds
const int ledGnd1 = 0;
const int ledGnd2 = 1;
const int ledGnd3 = 2;
const int ledGnd4 = 3;
// RGB pins
const int redLED = 13;
const int greenLED = 12;
const int blueLED = 11;
const int SwitchPin = 10;

// Colour definitions
int red[] = {255, 0, 0};
int green[] = {0, 255, 0};
int blue[] = {0, 0, 255};
int purple[] = {255, 0, 150};
int yellow[] = {255, 255, 0};
int dark[] = {0, 0, 0};

void setup() 
{ 
// Switch Grounds
pinMode(buttonMatrix1, INPUT);
pinMode(buttonMatrix2, INPUT);
pinMode(buttonMatrix3, INPUT);
pinMode(buttonMatrix4, INPUT);

// Led grounds
pinMode(ledGnd1, OUTPUT);
pinMode(ledGnd2, OUTPUT);
pinMode(ledGnd3, OUTPUT);
pinMode(ledGnd4, OUTPUT);

// RGB pins
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(blueLED, OUTPUT);

pinMode(SwitchPin, OUTPUT);

// Serial if you need it 
//Serial.begin(115200);
}

void loop() {
  
digitalWrite(SwitchPin, HIGH);
  
// Pass in the colour of your buttons 1, 2, 3, and 4
if (buttonMatrix1 >=analogRead(900))
{
  ledColour(blue,blue,blue,blue);
}
else if(buttonMatrix2 >= analogRead(900))
{
  ledColour(yellow,yellow,yellow,yellow);
}

else if(buttonMatrix3 >= analogRead(900))
{
  ledColour(red,red,red,red);
}

else if(buttonMatrix4 >= analogRead(900))
{
  ledColour(green,green,green,green);
}

else {
  ledColour(purple, blue, green, red);
}

// Uniform colour. Pass in the colour array of your choice.
//ledColourU(purple);

// Dark is just off. Use dark to turn any LED off.
//ledColour(dark, dark, dark, dark);


// Check for button presses and output states
// Enable if you want to test your buttons
Serial.print(analogRead(buttonMatrix1));
Serial.print("\t");
Serial.print(analogRead(buttonMatrix2));
Serial.print("\t");
Serial.print(analogRead(buttonMatrix3));
Serial.print("\t");
Serial.println(analogRead(buttonMatrix4));


}

// Control individual LEDs
// Pass in a colour array for each LED
void ledColour(int led1[], int led2[], int led3[], int led4[])
{
analogWrite(redLED, led1[0]);
analogWrite(greenLED, led1[1]);
analogWrite(blueLED, led1[2]);
// Flicker control
delay(2);
digitalWrite(ledGnd1, LOW);
// Flicker control
delayMicroseconds(1100);
digitalWrite(ledGnd1, HIGH);

analogWrite(redLED, led2[0]);
analogWrite(greenLED, led2[1]);
analogWrite(blueLED, led2[2]);
// Flicker control
delay(2);
digitalWrite(ledGnd2, LOW);
// Flicker control
delayMicroseconds(1100);
digitalWrite(ledGnd2, HIGH);

analogWrite(redLED, led3[0]);
analogWrite(greenLED, led3[1]);
analogWrite(blueLED, led3[2]);
// Flicker control
delay(2);
digitalWrite(ledGnd3, LOW);
// Flicker control
delayMicroseconds(1100);
digitalWrite(ledGnd3, HIGH);

analogWrite(redLED, led4[0]);
analogWrite(greenLED, led4[1]);
analogWrite(blueLED, led4[2]);
// Flicker control
delay(2);
digitalWrite(ledGnd4, LOW);
// Flicker control
delayMicroseconds(1100);
digitalWrite(ledGnd4, HIGH);

}

// Uniform colour
// This doesn't appear to work well with mixed colours.
void ledColourU(int colour[])
{
analogWrite(redLED, colour[0]);
analogWrite(greenLED, colour[1]);
analogWrite(blueLED, colour[2]);
digitalWrite(ledGnd1, LOW);
digitalWrite(ledGnd2, LOW);
digitalWrite(ledGnd3, LOW);
digitalWrite(ledGnd4, LOW); 
}

What is on pin 900?

I took a quick look and might suggest that in setup() you might want to turn ON the internal pull-up resistors with:

pinMode(buttonMatrix1, INPUT);
digitalWrite(buttonMatrix1, HIGH);
pinMode(buttonMatrix2, INPUT);
digitalWrite(buttonMatrix2, HIGH);
pinMode(buttonMatrix3, INPUT);
digitalWrite(buttonMatrix3, HIGH);
pinMode(buttonMatrix4, INPUT);
digitalWrite(buttonMatrix4, HIGH);

thus, an unpressed button reads HIGH, and when pressed it would read LOW

Just a thought...

Oh whoa, I was totally doing my analogRead wrong.

Okay so I fixed it that, changed them to analogRead(//respectivebutton) >= 900 and it works

I did try the pull-up, checking for LOW approach and it didn't work, so I changed it back to what I had going before.

Now I have a problem, even though I have the diodes soldered in to isolate the switches, whenever I press a button (doesn't matter which), I still get a response as if button 1 was pressed.

Any help with that?

Are you talking about the response of the LEDs? -- Or the response on your Serial Monitor.

response to the leds. stopped the serial monitor.
as you can see when a button is pressed, its supposed to correspond to a color on all 4

I don't understand what you're doing. Can you post your updated code? I'm assuming the buttonmatrix variables are the buttons, no? If so then what are you comparing them to when you do your analogRead? I don't know how your buttons are wired but if they are like regular buttons you'd want to do something like if(buttonMatrix1 == HIGH) But then again maybe I'm completely misunderstanding how your buttons work.

You are right, the buttonMatrix variables are the buttons. The analogRead is comparing them to an arbitrary value, 900. If the analogRead is greater than or equal to 900, then it will execute the command in the if statement for that button.

I think you maybe misunderstanding the circuit board itself.
Button Pad 2x2 - Breakout PCB - COM-09277 - SparkFun Electronics?
This is the board. Take a look at the board layout or schematic. The thing is the fact that they are essentially 2-pin buttons, soooo Its funky. I mean it seems more intuitive, but my mind isn't working properly

My updated code:

/*
Original: Aaron Goselin (Nakor) Oct 27, 2010

Editor: Shannon Strutz Jan 28th, 2012

This code is in the public domain.
*/

// Button Grounds
const int buttonMatrix1 = A1;
const int buttonMatrix2 = A2;
const int buttonMatrix3 = A3;
const int buttonMatrix4 = A4;
// LED Grounds
const int ledGnd1 = 0;
const int ledGnd2 = 1;
const int ledGnd3 = 2;
const int ledGnd4 = 3;
// RGB pins
const int redLED = 13;
const int greenLED = 12;
const int blueLED = 11;
const int SwitchPin = 10;

// Colour definitions
int red[] = {255, 0, 0};
int green[] = {0, 255, 0};
int blue[] = {0, 0, 255};
int purple[] = {255, 0, 150};
int yellow[] = {255, 255, 0};
int dark[] = {0, 0, 0};

void setup() 
{ 
// Switch Grounds
pinMode(buttonMatrix1, INPUT);
pinMode(buttonMatrix2, INPUT);
pinMode(buttonMatrix3, INPUT);
pinMode(buttonMatrix4, INPUT);

// Led grounds
pinMode(ledGnd1, OUTPUT);
pinMode(ledGnd2, OUTPUT);
pinMode(ledGnd3, OUTPUT);
pinMode(ledGnd4, OUTPUT);

// RGB pins
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(blueLED, OUTPUT);

pinMode(SwitchPin, INPUT);
digitalWrite(SwitchPin, HIGH);

// Serial if you need it 
//Serial.begin(115200);
}

void loop() {  
// Pass in the colour of your buttons 1, 2, 3, and 4
if (analogRead(buttonMatrix1) >= 900)
{
  ledColour(blue,blue,blue,blue);
}
else if(analogRead(buttonMatrix2) >= 900)
{
  ledColour(yellow,yellow,yellow,yellow);
}

else if(analogRead(buttonMatrix3) >= 900)
{
  ledColour(red,red,red,red);
}

else if(analogRead(buttonMatrix4) >= 900)
{
  ledColour(green,green,green,green);
}

else {
  ledColour(purple, blue, green, red);
}

// Uniform colour. Pass in the colour array of your choice.
//ledColourU(purple);

// Dark is just off. Use dark to turn any LED off.
//ledColour(dark, dark, dark, dark);


// Check for button presses and output states
// Enable if you want to test your buttons
/*
Serial.print(analogRead(buttonMatrix1));
Serial.print("\t");
Serial.print(analogRead(buttonMatrix2));
Serial.print("\t");
Serial.print(analogRead(buttonMatrix3));
Serial.print("\t");
Serial.println(analogRead(buttonMatrix4));
*/

}

// Control individual LEDs
// Pass in a colour array for each LED
void ledColour(int led1[], int led2[], int led3[], int led4[])
{
analogWrite(redLED, led1[0]);
analogWrite(greenLED, led1[1]);
analogWrite(blueLED, led1[2]);
// Flicker control
delay(2);
digitalWrite(ledGnd1, LOW);
// Flicker control
delayMicroseconds(1000);
digitalWrite(ledGnd1, HIGH);

analogWrite(redLED, led2[0]);
analogWrite(greenLED, led2[1]);
analogWrite(blueLED, led2[2]);
// Flicker control
delay(2);
digitalWrite(ledGnd2, LOW);
// Flicker control
delayMicroseconds(1000);
digitalWrite(ledGnd2, HIGH);

analogWrite(redLED, led3[0]);
analogWrite(greenLED, led3[1]);
analogWrite(blueLED, led3[2]);
// Flicker control
delay(2);
digitalWrite(ledGnd3, LOW);
// Flicker control
delayMicroseconds(1000);
digitalWrite(ledGnd3, HIGH);

analogWrite(redLED, led4[0]);
analogWrite(greenLED, led4[1]);
analogWrite(blueLED, led4[2]);
// Flicker control
delay(2);
digitalWrite(ledGnd4, LOW);
// Flicker control
delayMicroseconds(1000);
digitalWrite(ledGnd4, HIGH);

}

// Uniform colour
// This doesn't appear to work well with mixed colours.
void ledColourU(int colour[])
{
analogWrite(redLED, colour[0]);
analogWrite(greenLED, colour[1]);
analogWrite(blueLED, colour[2]);
digitalWrite(ledGnd1, LOW);
digitalWrite(ledGnd2, LOW);
digitalWrite(ledGnd3, LOW);
digitalWrite(ledGnd4, LOW); 
}

funkyguy4000:
stopped the serial monitor.

Serial needs pins 0 and 1, can you use different ones for your LEDs?

Then you would be able to see what your analogReads are returning...

Cheers,
John

Wow, I've been gone from the IDE for too long.
Changed that up and activated the monitor but I won't be able to test this until I get home in about 9 hours

Alright so I fixed those around and activated the serial monitor again and I'm still getting the same result, no matter which button I press, they all turn blue.

Updated Code

/*
Original: Aaron Goselin (Nakor) Oct 27, 2010

Editor: Shannon Strutz Jan 28th, 2012

This code is in the public domain.
*/

// Button Grounds
const int buttonMatrix1 = A1;
const int buttonMatrix2 = A2;
const int buttonMatrix3 = A3;
const int buttonMatrix4 = A4;
// LED Grounds
const int ledGnd1 = 4;
const int ledGnd2 = 5;
const int ledGnd3 = 2;
const int ledGnd4 = 3;
// RGB pins
const int redLED = 13;
const int greenLED = 12;
const int blueLED = 11;
const int SwitchPin = 10;

// Colour definitions
int red[] = {255, 0, 0};
int green[] = {0, 255, 0};
int blue[] = {0, 0, 255};
int purple[] = {255, 0, 150};
int yellow[] = {255, 255, 0};
int dark[] = {0, 0, 0};

void setup() 
{ 
// Switch Grounds
pinMode(buttonMatrix1, INPUT);
pinMode(buttonMatrix2, INPUT);
pinMode(buttonMatrix3, INPUT);
pinMode(buttonMatrix4, INPUT);

// Led grounds
pinMode(ledGnd1, OUTPUT);
pinMode(ledGnd2, OUTPUT);
pinMode(ledGnd3, OUTPUT);
pinMode(ledGnd4, OUTPUT);

// RGB pins
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(blueLED, OUTPUT);

pinMode(SwitchPin, INPUT);
digitalWrite(SwitchPin, HIGH);

// Serial if you need it 
Serial.begin(115200);
}

void loop() {  
// Pass in the colour of your buttons 1, 2, 3, and 4
if (analogRead(buttonMatrix1) >= 900)
{
  ledColour(blue,blue,blue,blue);
}
else if(analogRead(buttonMatrix2) >= 900)
{
  ledColour(yellow,yellow,yellow,yellow);
}

else if(analogRead(buttonMatrix3) >= 900)
{
  ledColour(red,red,red,red);
}

else if(analogRead(buttonMatrix4) >= 900)
{
  ledColour(green,green,green,green);
}

else {
  ledColour(purple, blue, green, red);
}

// Uniform colour. Pass in the colour array of your choice.
//ledColourU(purple);

// Dark is just off. Use dark to turn any LED off.
//ledColour(dark, dark, dark, dark);


// Check for button presses and output states
// Enable if you want to test your buttons

Serial.print(analogRead(buttonMatrix1));
Serial.print("\t");
Serial.print(analogRead(buttonMatrix2));
Serial.print("\t");
Serial.print(analogRead(buttonMatrix3));
Serial.print("\t");
Serial.println(analogRead(buttonMatrix4));


}

// Control individual LEDs
// Pass in a colour array for each LED
void ledColour(int led1[], int led2[], int led3[], int led4[])
{
analogWrite(redLED, led1[0]);
analogWrite(greenLED, led1[1]);
analogWrite(blueLED, led1[2]);
// Flicker control
delay(2);
digitalWrite(ledGnd1, LOW);
// Flicker control
delayMicroseconds(1000);
digitalWrite(ledGnd1, HIGH);

analogWrite(redLED, led2[0]);
analogWrite(greenLED, led2[1]);
analogWrite(blueLED, led2[2]);
// Flicker control
delay(2);
digitalWrite(ledGnd2, LOW);
// Flicker control
delayMicroseconds(1000);
digitalWrite(ledGnd2, HIGH);

analogWrite(redLED, led3[0]);
analogWrite(greenLED, led3[1]);
analogWrite(blueLED, led3[2]);
// Flicker control
delay(2);
digitalWrite(ledGnd3, LOW);
// Flicker control
delayMicroseconds(1000);
digitalWrite(ledGnd3, HIGH);

analogWrite(redLED, led4[0]);
analogWrite(greenLED, led4[1]);
analogWrite(blueLED, led4[2]);
// Flicker control
delay(2);
digitalWrite(ledGnd4, LOW);
// Flicker control
delayMicroseconds(1000);
digitalWrite(ledGnd4, HIGH);

}

// Uniform colour
// This doesn't appear to work well with mixed colours.
void ledColourU(int colour[])
{
analogWrite(redLED, colour[0]);
analogWrite(greenLED, colour[1]);
analogWrite(blueLED, colour[2]);
digitalWrite(ledGnd1, LOW);
digitalWrite(ledGnd2, LOW);
digitalWrite(ledGnd3, LOW);
digitalWrite(ledGnd4, LOW); 
}

And what is the output of your Serial Monitor?

And you may want to consider:

// Sketch 1
void loop()[
  // see if and how the switches are working
  Serial.print(analogRead(pinA);
  Serial.print(analogRead(pinB);
  ...
  delay(250); // n.b. delays allowed in test harness code
}
// Sketch 2
void loop()[
  // see if and how the leds are working
  ledColour(red,red,red,red);
  delay(500); 
  ledColour(yellow,yellow,yellow,yellow);  
  delay(500);
  ledColour(purple, blue, green, red);
  delay(500);...
}

Well when its sitting there, I get an output like this:

96	92	97	91
99	91	92	93
91	83	78	85
82	78	72	77
85	84	80	80
94	93	92	89
102	98	102	98
102	94	94	96
91	85	80	86
84	81	75	80
89	88	84	83
97	97	96	93
104	101	103	101
103	97	95	98
92	87	82	88
87	85	79	83
93	93	89	87
100	100	100	96
106	103	105	103
104	98	96	100
94	89	84	90
89	88	82	86
97	97	94	92
104	104	104	101
108	106	107	106
105	100	98	101
95	91	85	92
90	88	82	86
97	97	94	92
104	105	104	100
108	107	108	106
106	101	99	102
95	92	86	92

And obviously when I press a button, they all go up to a high signal like this :

941	987	973	936
904	981	957	920
886	980	952	911
977	987	978	945
943	986	977	937
929	983	985	952
914	982	973	943
901	980	959	924
897	980	959	920

Ok, so there is a problem with the switches.

I think:

switchPin should be OUTPUT and digitalWrite( switchPin, LOW);

buttonmatrixN are INPUT_PULLUP; (or INPUT and then digitalWrite( buttonmatrixN, HIGH);

Try that and check the Serial Monitor output again?

Okay I changed that and the serial monitor now is like this. Even when I press the buttons, nothing changes

1018	1020	1020	1021
1018	1019	1020	1021
1019	1020	1020	1021
1018	1019	1020	1021
1018	1019	1020	1021
1018	1019	1020	1021
1018	1019	1020	1021
1018	1019	1020	1021
1019	1019	1020	1021
1018	1019	1020	1021
1018	1019	1020	1021
1019	1019	1020	1021
1018	1019	1020	1021
1018	1019	1020	1021
1018	1019	1020	1021
1018	1019	1020	1021
1018	1019	1020	1021
1018	1019	1020	1021
1018	1019	1020	1021
1018	1019	1020	1021
1018	1019	1020	1021
1018	1019	1020	1021
1019	1019	1020	1021
1018	1019	1020	1021
1018	1019	1020	1021
1018	1019	1020	1021
1018	1019	1020	1021
1018	1019	1020	1021
1018	1019	1020	1021
1018	1019	1020	1021
1018	1019	1020	1021
1018	1019	1020	1020
1018	1019	1020	1021
1018	1019	1020	1020
1019	1019	1020	1021
1018	1019	1020	1021

Time to post again the entire code with your changes :slight_smile:

We are working on just the buttons right now, we will leave the LEDS for later.

If your version has the so-called "isolation diodes" then we must reverse the polarity of the sense:

void setup() 
{	
	pinMode(buttonMatrix1, INPUT);   // Button sense
	pinMode(buttonMatrix2, INPUT);   // external pull-down may be required
	pinMode(buttonMatrix3, INPUT);
	pinMode(buttonMatrix4, INPUT);

	pinMode(SwitchPin, OUTPUT);     // Button Common  
	digitalWrite(SwitchPin, HIGH);
}
void loop() 
{
	Serial.print(analogRead(buttonMatrix1));
	Serial.print("\t");
	Serial.print(analogRead(buttonMatrix2));
	Serial.print("\t");
	Serial.print(analogRead(buttonMatrix3));
	Serial.print("\t");
	Serial.println(analogRead(buttonMatrix4));
	delay(200);
}

Cheers,
John

I'm always amazed at people that take an action based on one reading, then take another reading and print it, and don't understand that the two readings can be different.

Those appear to be regular digital switches on that board. Why are you using analogRead() to read digital switches?

How about posting a picture showing how you have connected that to the Arduino?

I don't get these switches!
They are different, I've never used 2 pin ones.

Picture attached

They are different, I've never used 2 pin ones.

Every switch I've ever seen had just two pins. Have you used a multimeter to see how those switches actually work?