Arduino Forum

Community => Exhibition / Gallery => Topic started by: aarondc on May 31, 2013, 05:35 pm

Title: Freetronics Experimenters Kit Project 7 - RGB LED Enhancement
Post by: aarondc on May 31, 2013, 05:35 pm
I bought the kit and have been trying the different projects in it. My enhancement idea for project #7 was to add 3 push buttons and change the individual RGB values for the LED via the trim pot, only when the corresponding button was pushed down.

When you first press a button, the sketch grabs the current Pot value for the pressed button's RGB value for the LED. Holding the button down and then turning the pot updates this value, reflected in the LED's colouring.

I also wanted to:
* develop / test a debugging library and serial monitor replacement I had in mind
* try and draw the layout diagram as implemented, so downloaded Fritzing and gave that a go too.

The PCB layout process is going to take some getting used to, clearly. Will give that a go in Eagle next.

Layout:
(http://img716.imageshack.us/img716/3377/breadboard.png)

Code:
Code: [Select]

const int pinPot = A0;
const int iHysteresis = 4;
int iValue = 0;
int iLastValue = 0;

byte buttonRGB[] = {7, 6, 5}; // digital pins tied to buttons
byte buttonStateRGB[] = {LOW, LOW, LOW}; // state of the buttons
byte buttonDownRGB[] = {false, false, false}; // track when the button is initially down

byte levelRGB[] = {0, 0, 0}; // level of the RGB output levels
byte pinRGB[] = {11, 10, 9}; // pins for the RGB outputs

void setup() {
Serial.begin(9600);

pinMode(pinPot, INPUT);

for (int i = 0; i<3; i++) {
pinMode(buttonRGB[i], INPUT);
pinMode(pinRGB[i], OUTPUT);
}

// turn off the LED on pin 13
pinMode(13, OUTPUT);
analogWrite(13, LOW);
}



void loop() {
int tmpChanged = valueChanged();
if ((buttonDown()) && (tmpChanged)) { // if any button is pressed down and the pot has changed
for (int i = 0; i<3; i++) { // check each button
if (buttonStateRGB[i]) { // if it's down (pin state is HIGH)
levelRGB[i] = map(iValue, 0, 1023, 0, 25); // save the pot --> led level value
analogWrite(pinRGB[i], levelRGB[i]); // write the led level value to the LED via the PWM pins
}
}
}
}

int valueChanged() {
iValue = analogRead(pinPot);
if ((iValue > iLastValue + iHysteresis) || (iValue < iLastValue - iHysteresis)) {
iLastValue = iValue;
return 1;
}
return 0;
}

int buttonDown() {
int Result = 0; // Delphi developer was here >:^'

for (int i = 0; i<3; i++) {
buttonStateRGB[i] = digitalRead(buttonRGB[i]);
Result |= buttonStateRGB[i];
if (buttonStateRGB[i]) {
if (!buttonDownRGB[i]) {
levelRGB[i] = map(analogRead(pinPot), 0, 1023, 0, 25);
analogWrite(pinRGB[i], levelRGB[i]);
buttonDownRGB[i] = true;
}
}
else buttonDownRGB[i] = false;
}
return Result;
}
Title: Re: Freetronics Experimenters Kit Project 7 - RGB LED Enhancement
Post by: johnft on Jun 02, 2013, 08:17 am
Well done, glad to see you're enjoying the kit.
http://www.freetronics.com/products/experimenters-kit-for-arduino