I am trying to get the Button Pad 2x2 - Breakout PCB - COM-09277 - SparkFun Electronics working on an Arduino Micro using the following 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
Aaron Goselin (Nakor)
Oct 27, 2010
This code is in the public domain.
*/
// Button Grounds
const int buttonMatrix1 = 12;
const int buttonMatrix2 = 7;
const int buttonMatrix3 = 13;
const int buttonMatrix4 = 11;
// LED Grounds
const int ledGnd1 = 10;
const int ledGnd2 = 11;
const int ledGnd3 = 12;
const int ledGnd4 = 13;
// RGB pins
const int redLED = 4;
const int greenLED = 5;
const int blueLED = 6;
// 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);
// Serial if you need it
//Serial.begin(115200);
}
void loop() {
// Pass in the colour of your buttons 1, 2, 3, and 4.
ledColour(yellow, red, purple, green);
// 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(digitalRead(buttonMatrix1));
Serial.print("\t");
Serial.print(digitalRead(buttonMatrix2));
Serial.print("\t");
Serial.print(digitalRead(buttonMatrix3));
Serial.print("\t");
Serial.println(digitalRead(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);
}
(modified for the pins that I am using.) The issue is that I want the LEDs to start in the setup as red, green, blue and yellow. I dont want the colors to change. I cant seem to make the buttons stay lit. I am thinking since I dont need any change in the color of the LEDs that by setting the output in void setup I can let the following code look less crappy and inefficient than it already is.
const int buttonPin = A3; // pin to read button
const int mode1 = 2; // relay 1
const int mode2 = 7; // relay 2
const int mode3 = 8; // relay 3\
const int L1 = 1; // relay 1
const int L2 = 0; // relay 2
const int L3 = 9; // relay 3
const int SL = 3; // relay for save/load button
int state = 1; // Which relay is currently on.
void setup()
{
Serial.begin(9600);
const int L1 = 1;
pinMode(buttonPin, INPUT);
pinMode(mode1, OUTPUT);
pinMode(mode2, OUTPUT);
pinMode(mode3, OUTPUT);
pinMode(L1, OUTPUT);
pinMode(L2, OUTPUT);
pinMode(L3, OUTPUT);
pinMode(SL, OUTPUT);
digitalWrite(mode1, HIGH);
digitalWrite(SL, HIGH);
}
void loop()
{
if( analogRead(buttonPin) ) {
const int L1 = 1;
// debounce
delay(200);
if (state == 1) {
digitalWrite(mode3, LOW);
state = 2;
digitalWrite(L2, LOW);
digitalWrite(L3, LOW);
delay(200);
digitalWrite(L1, HIGH);
digitalWrite(mode1, HIGH);
delay(200);
digitalWrite(SL, HIGH);
delay(50);
digitalWrite(SL, LOW);
delay(50);
digitalWrite(SL, HIGH);
delay(50);
digitalWrite(SL, LOW);
delay(50);
digitalWrite(SL, HIGH);
}
else if (state == 2) {
digitalWrite(mode1, LOW);
state = 3;
digitalWrite(L1, LOW);
delay(200);
digitalWrite(L2, HIGH);
digitalWrite(mode2, HIGH);
delay(200);
digitalWrite(SL, HIGH);
delay(50);
digitalWrite(SL, LOW);
delay(50);
digitalWrite(SL, HIGH);
delay(50);
digitalWrite(SL, LOW);
delay(50);
digitalWrite(SL, HIGH);
}
else if (state == 3) {
digitalWrite(mode2, LOW);
state = 1;
digitalWrite(L2, LOW);
delay(200);
digitalWrite(L3, HIGH);
digitalWrite(mode3, HIGH);
delay(200);
digitalWrite(SL, HIGH);
delay(50);
digitalWrite(SL, LOW);
delay(50);
digitalWrite(SL, HIGH);
delay(50);
digitalWrite(SL, LOW);
delay(50);
digitalWrite(SL, HIGH);
}
while(analogRead(buttonPin)); // wait until the button is released
delay(200); // debounce
Serial.println(state);
Serial.print("\t");
Serial.print(mode1);
Serial.print("\t");
Serial.print(mode2);
Serial.print("\t");
Serial.print(mode3);
Serial.print("\t");
Serial.print(L1);
Serial.print("\t");
Serial.print(L2);
Serial.print("\t");
Serial.print(L3);
Serial.print("\t");
Serial.println(SL);
Serial.print("\t");
}
}