SFE 2x2 RGB LED Button Pad Control v2

/*
2x2 RGB LED Control v2
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)

v1 - Oct 27, 2010
Original code

v2 - Oct 29, 2010
Added use of EventFuse and MsTimer2 libraries.

This code is in the public domain.
*/

// My site is parsing out the brackets and I'm too lazy to change things
#include EventFuse.h  // Put triangular brackets around EventFuse.h
#include MsTimer2.h   // Put triangular brackets aroud MsTimer2.h

// Button Grounds
const int buttonMatrix1 = 12;
const int buttonMatrix2 = 7;
const int buttonMatrix3 = 13;
const int buttonMatrix4 = 11;
// LED Grounds
const int ledGnd1 = 2;
const int ledGnd2 = 3;
const int ledGnd3 = 8;
const int ledGnd4 = 5;
// RGB pins
const int redLED = 9;
const int greenLED = 10;
const int blueLED = 6;

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

// Fuse flags (has fuse ended?)
boolean fuse1IsOff = false;
boolean fuse2IsOff = false;
boolean fuse3IsOff = false;
boolean fuse4IsOff = false;

// Flag to indicate if the start up has finished
// and the main loops have started
boolean randyStarted = false;

// Colour max values
int redMax = 0;
int greenMax = 0;
int blueMax = 0;
// Counters for colour fading in randyColoursOut()
int fadeCount = 0;
int ledCount = 0;

// Fuse creation
EventFuse eventFuse1 = EventFuse();
EventFuse eventFuse2 = EventFuse();
EventFuse eventFuse3 = EventFuse();
EventFuse eventFuse4 = EventFuse();

 

void setup()
{
// Switch Grounds
// These are your input pins for your buttons
pinMode(buttonMatrix1, INPUT);
pinMode(buttonMatrix2, INPUT);
pinMode(buttonMatrix3, INPUT);
pinMode(buttonMatrix4, INPUT);

// Led grounds
// These control turning your LEDs on and off
pinMode(ledGnd1, OUTPUT);
pinMode(ledGnd2, OUTPUT);
pinMode(ledGnd3, OUTPUT);
pinMode(ledGnd4, OUTPUT);
// Start the sketch with the LEDs ready to go
digitalWrite(ledGnd1, HIGH);
digitalWrite(ledGnd2, HIGH);
digitalWrite(ledGnd3, HIGH);
digitalWrite(ledGnd4, HIGH);

// RGB pins
// Output pins for the RGB LEDs
// These control what colour your LED will be
// when you turn it on
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(blueLED, OUTPUT);

/*
// Cool fuses
// Just a couple ideas on how to play with the fuses

// Slow then fast then slow then fast strobing. (1 light)
eventFuse.newFuse(110, INF_REPEAT, ledIgnite1);
eventFuse.newFuse(111, INF_REPEAT, ledIgnite1);

// Confused strobing (2 lights)
eventFuse.newFuse(110, INF_REPEAT, ledIgnite1);
eventFuse.newFuse(111, INF_REPEAT, ledIgnite1);

eventFuse.newFuse(115, INF_REPEAT, ledIgnite2);
eventFuse.newFuse(116, INF_REPEAT, ledIgnite2);

*/

// Setting up our initial fuses
// ( fuse length, repeat count, callback )
eventFuse1.newFuse(random(211,911), 20, ledIgnite1);
eventFuse2.newFuse(random(312,912), 20, ledIgnite2);
eventFuse3.newFuse(random(112,1000), 8, ledIgnite3);
eventFuse4.newFuse(random(1112,1300), 5, ledIgnite4);

// Starting the timer
// One tick per millisecond
MsTimer2::set(1, tick);
MsTimer2::start();

// 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 to see if all of the fuses have ended
if(fuse1IsOff & fuse2IsOff & fuse3IsOff & fuse4IsOff)
{
// If they have, create a new fuse to run once
// New fuse calls randyColoursOut()
if(!randyStarted)
{
eventFuse.newFuse(1, 1, randyColoursOut);
}
// Set flag to indicate start sequence is over
randyStarted = true;
}

/*
// Check for button presses and output states
// Enable this if you would like 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));
*/
}

// Controls button 1
// Called by fuse for initial sequence
void ledIgnite1(FuseID fuse, int userData)
{
analogWrite(redLED, red[0]);
analogWrite(greenLED, red[1]);
analogWrite(blueLED, red[2]);
digitalWrite(ledGnd1, LOW);
delayMicroseconds(2100); //Accounts for flickering
digitalWrite(ledGnd1, HIGH);

// Check to see if the fuse that called
// the function has ended
if(eventFuse1[fuse].repeatCount == 1)
{
// If it has, set flag
fuse1IsOff = true;
}
}

// Controls button 2
// Called by fuse for initial sequence
void ledIgnite2(FuseID fuse, int userData)
{
analogWrite(redLED, blue[0]);
analogWrite(greenLED, blue[1]);
analogWrite(blueLED, blue[2]);
digitalWrite(ledGnd2, LOW);
delayMicroseconds(2100); //Accounts for flickering
digitalWrite(ledGnd2, HIGH);

// Check to see if the fuse that called
// the function has ended
if(eventFuse2[fuse].repeatCount == 1)
{
// If it has, set flag
fuse2IsOff = true;
}
}

// Controls button 3
// Called by fuse for initial sequence
void ledIgnite3(FuseID fuse, int userData)
{
analogWrite(redLED, green[0]);
analogWrite(greenLED, green[1]);
analogWrite(blueLED, green[2]);
digitalWrite(ledGnd3, LOW);
delayMicroseconds(2100); //Accounts for flickering
digitalWrite(ledGnd3, HIGH);

// Check to see if the fuse that called
// the function has ended
if(eventFuse3[fuse].repeatCount == 1)
{
// If it has, set flag
fuse3IsOff = true;
}
}

// Controls button 4
// Called by fuse for initial sequence
void ledIgnite4(FuseID fuse, int userData)
{
analogWrite(redLED, purple[0]);
analogWrite(greenLED, purple[1]);
analogWrite(blueLED, purple[2]);
digitalWrite(ledGnd4, LOW);
delayMicroseconds(2100); //Accounts for flickering
digitalWrite(ledGnd4, HIGH);

// Check to see if the fuse that called
// the function has ended
if(eventFuse4[fuse].repeatCount == 1)
{
// If it has, set flag
fuse4IsOff = true;
}
}

// tick() is called once per millisecond
// burn(1) decreases fuse counters by 1
void tick()
{
eventFuse.burn(1);
eventFuse1.burn(1);
eventFuse2.burn(1);
eventFuse3.burn(1);
eventFuse4.burn(1);
}

// Control individual LEDs
// Pass in a colour array for each LED
void ledColour(int led1[], int led2[], int led3[], int led4[])
{
// Set RGB pins
analogWrite(redLED, led1[0]);
analogWrite(greenLED, led1[1]);
analogWrite(blueLED, led1[2]);
// Flicker control (wait before turning LED on)
delay(2);
// Turn on the LED
digitalWrite(ledGnd1, LOW);
// Flicker control (wait before turning LED off)
delayMicroseconds(1200);
// Turn LED off (so you can control another LED)
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(1200);
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(1200);
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(1200);
digitalWrite(ledGnd4, HIGH);
}

// Uniform colour
// This doesn't appear to work well with some 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);
}

void randyColours()
{
redMax = random(0,255);
greenMax = random(0,255);
blueMax = random(0,255);
}

// Fades random colours in
void randyColoursOut(FuseID fuse, int userData)
{
// Checks to see if the fuse has run out (which it has the first time)
if(eventFuse[fuse].repeatCount == 1)
{
// Makes a call to randyColours() to set the random max values for colours
randyColours();

// Checks to see which colour got the highest number and sets the new fuse
// to run that many times
if(redMax >= greenMax && redMax >= blueMax)
{
// Resetting the number of fade run throughs to zero
fadeCount = 0;

// Check to see what LED we are on and reset if needed
if(ledCount == 4){ ledCount = 0; }
// Increment the LED counter
else{ ledCount++; }

// Setting the fuse
// For example:
// If randyColours() set redMax = 244, greenMax = 198, blueMax = 200
// then redMax would be chosen as highest
// This if would be run and set the fuse just below
// This fuse is set to run every 10 ticks,
// and redMax times (which in this example would be 244 times)
eventFuse.resetFuse(fuse, 10, redMax, randyColoursOut);
}
else if(greenMax >= redMax && greenMax >= blueMax)
{
fadeCount = 0;

if(ledCount == 4){ ledCount = 0; }
else{ ledCount++; }

Serial.println(ledCount);
eventFuse.resetFuse(fuse, 10, greenMax, randyColoursOut);
}
else if(blueMax >= redMax && blueMax >= greenMax)
{
fadeCount = 0;

if(ledCount == 4){ ledCount = 0; }
else{ ledCount++; }

Serial.println(ledCount);
eventFuse.resetFuse(fuse, 10, blueMax, randyColoursOut);
}
else
{
fadeCount = 0;

if(ledCount == 4){ ledCount = 0; }
else{ ledCount++; }

Serial.println(ledCount);
eventFuse.resetFuse(fuse, 10, 255, randyColoursOut);
}
}

// Incrementing the fade counter
fadeCount++;

// Setting base colours
// These work the same as they do in ledColour()
// except that I was lazy and am not passing in colour arrays
analogWrite(redLED, red[0]);
analogWrite(greenLED, red[1]);
analogWrite(blueLED, red[2]);
delay(2);
digitalWrite(ledGnd1, LOW);
delayMicroseconds(1200);
digitalWrite(ledGnd1, HIGH);

analogWrite(redLED, blue[0]);
analogWrite(greenLED, blue[1]);
analogWrite(blueLED, blue[2]);
delay(2);
digitalWrite(ledGnd2, LOW);
delayMicroseconds(1200);
digitalWrite(ledGnd2, HIGH);

analogWrite(redLED, green[0]);
analogWrite(greenLED, green[1]);
analogWrite(blueLED, green[2]);
delay(2);
digitalWrite(ledGnd3, LOW);
delayMicroseconds(1200);
digitalWrite(ledGnd3, HIGH);

analogWrite(redLED, yellow[0]);
analogWrite(greenLED, yellow[1]);
analogWrite(blueLED, yellow[2]);
delay(2);
digitalWrite(ledGnd4, LOW);
delayMicroseconds(1200);
digitalWrite(ledGnd4, HIGH);

// For each colour make sure the max hasn't been reached
// and then set the pin
if(fadeCount <= redMax){ analogWrite(redLED, fadeCount); }
if(fadeCount <= greenMax){ analogWrite(greenLED, fadeCount); }
if(fadeCount <= blueMax){ analogWrite(blueLED, fadeCount); }
delay(2);

// Check which LED we are on and control the ground
// (turn it on and off)
if(ledCount == 1)
{
digitalWrite(ledGnd1, LOW);
delayMicroseconds(3200);
digitalWrite(ledGnd1, HIGH);
}
if(ledCount == 2)
{
digitalWrite(ledGnd2, LOW);
delayMicroseconds(3200);
digitalWrite(ledGnd2, HIGH);
}
if(ledCount == 3)
{
digitalWrite(ledGnd3, LOW);
delayMicroseconds(3200);
digitalWrite(ledGnd3, HIGH);
}
if(ledCount == 4)
{
digitalWrite(ledGnd4, LOW);
delayMicroseconds(3200);
digitalWrite(ledGnd4, HIGH);

}
}


Aaron Goselin
Written on Monday, 01 November 2010 13:46 by Aaron Goselin

Viewed 617 times so far.
Like this? Tweet it to your followers!
blog comments powered by Disqus

Site Search

Latest Tweets

Basic authentication is not supported

Random Links

Adobe Kuler

Adobe Kuler

This is Adobe's free online colour schemer.  It
has some great features such as generating your
colours by examining a picture that you like
(from Flickr or from your computer).  You can
also work out your colours manually.

The site has tons of users already and if you
don't want to work out your colours on your
own then you can look through the thousands
of already created schemes.

Sound Cloud

Sound Cloud

A little bit like YouTube for audio.  Musicians (or
producers, DJs, rappers, whatever) can sign up
(for free or paid premium) and upload their
music.  This is a great way to find new artists
and some of the work on here is absolutely
excellent.

If you like Dubstep I recommend you look up
Inofaith.

My music can also be found on this site.  My
account is located here.

Aviary

Aviary

Aviary is an online image suite.  Basically it is
Adobe Creative Suite in your browser, for
free.  Keep in mind that that's me saying
that and not them.  They could probably be
sued for saying that.

This is great for those times when you are at a
friend's house and need to do some image
editing, only to find out he has nothing of use
on his (or her) computer.  Enter Aviary.

Either that or you are just bored :)

Loop Labs

 

Loop Labs

A great little site for making music when you
don't have access to a proper DAW.  The
web apps on this site are loop sequencers
and they work fairly well.

I've made a couple quick songs on this site
when I needed to amuse myself.