Hi All
So basically, i was hoping for one button to switch between functions, and the other button to just turn everything off.
Right now its just really buggy, and the lights blink really fast sometimes, other times it just goofs up entirely.
Below is a picture of my project, my code, and my little plan for my finished
//LED Mood Light
//Edited By Billho
//Control RGB Led's, smoothly transition between colours
//Switch functions with a button
//Set lights off with another button
//Control lights with potentiometers
int maxBright = 255; //Max Brightness the pins will reach
int delayTime = 20; //Delay Time, large delay time for longer transitions
int buttonPin = 1; //Function button
int buttonPinOff = 2; //Off Button
//Which pin for which colour
int redPin = 9;
int greenPin = 10;
int bluePin = 11;
//Pot pins
int knobvalueRed;
int potRed = 0;
int knobvalueGreen;
int potGreen = 1;
int knobvalueBlue;
int potBlue = 2;
//Initial colour value for each pin
int red = 0;
int green = 170;
int blue = 170;
//For setting the lights to off
int redOff = 0;
int greenOff = 0;
int blueOff = 0;
// Indicates whether a colour is incrementing (1) or decrementing (0).
int incR = 1;
int incG = 1;
int incB = 0;
//Button Pin1
int state = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin
//Button Pin Off
int state2 = HIGH; // the current state of the output pin
int reading2; // the current reading from the input pin
int previous2 = LOW; // the previous reading from the input pin
// the follow variables are unsigned long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
unsigned long time = 0; // the last time the output pin was toggled
unsigned long debounce = 200; // the debounce time, increase if the output flicker
//Function for pot control of LEDs
void fader() {
knobvalueRed = analogRead(potRed);
knobvalueRed = map(knobvalueRed, 0, 1023, 0, 255);
knobvalueGreen = analogRead(potGreen);
knobvalueGreen = map(knobvalueGreen, 0, 1023, 0, 255);
knobvalueBlue = analogRead(potBlue);
knobvalueBlue = map(knobvalueBlue, 0, 1023, 0, 255);
analogWrite(9, knobvalueRed);
analogWrite(10, knobvalueGreen);
analogWrite(11, knobvalueBlue);
}
//Function for the blending of lights
void transition() {
if (red >= maxBright)
incR = 0;
else if (red <= 0)
incR = 1;
if (green >= maxBright)
incG = 0;
else if (green <= 0)
incG = 1;
if (blue >= maxBright)
incB = 0;
else if (blue <= 0)
incB = 1;
if (incR)
red++;
else
red--;
if(incG)
green++;
else
green--;
if(incB)
blue++;
else
blue--;
}
//Sets colours to start of loop
void setColour() {
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
//Sets LEDs to off
void setOff() {
analogWrite(redPin, redOff);
analogWrite(greenPin, greenOff);
analogWrite(bluePin, blueOff);
}
void setup()
{
pinMode(buttonPin, INPUT);
pinMode(buttonPinOff, INPUT);
}
void loop(){
// read the pushbutton input pin:
reading = digitalRead(buttonPin);
reading2 = digitalRead(buttonPinOff);
// compare the reading to its previous state
if (reading != previous) {
if (reading == HIGH) {
state = !state; //toggle state
}
previous = reading;
}
// if the state has changed, increment the counter
if (state == HIGH) {
// if the current state is HIGH then the button
// went from off to on:
transition();
setColour();
delay(delayTime);
}
else {
// if the current state is LOW then the button
// went from on to off:
fader();
if (reading2 != previous2) {
if (reading2 == HIGH) {
state2 = !state2; //toggle state
}
previous2 = reading2;
}
// if the state has changed, increment the counter
if (state2 == HIGH) {
// if the current state is HIGH then the button
// went from off to on:
// do nothing
}
else {
// if the current state is LOW then the button
// went from on to off:
setOff();
}
}
}/code]