Pages: [1]   Go Down
Author Topic: Sparkfun 2x2 led matrix button board help  (Read 667 times)
0 Members and 1 Guest are viewing this topic.
Wisconsin
Offline Offline
Edison Member
*
Karma: 4
Posts: 1001
I LOVE THIS STUFF!!!!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Code:
/*
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);
}

I know that my problem lies in the fact that I've never really worked with buttons that have only 2 connections.  I've only ever worked with the pushbuttons that have 4 pins.  Can somebody help me out on getting this thing working?
Logged

Accelerate to 88 miles per hour.

Pages: [1]   Go Up
Jump to: