// Accent light control program
/* a program to control ambient lighting for my garage workbench that has a few variables and the ouputs are;
a red LED strip for ambient lighting,
a white LED strip for the main lighting
and two narrow "focus" lights, basically two seperate LEDS that have a narrow beam pattern for when i need extra light on a certain portion of the bench.
i have the LED strips powered through the vin pin with a 12v power supply (with a 1200ma max out) since the LED strips need 12v to operate.
they are switched through TIP - 120 Darlington Transistors on outputs 10 and 9 so i can dim them in and out through PWM
basically what i want the program to do is turn the red LED strip on with a switch (so i can leave the program running constantly when no-one is around)
and be able to adjust the brightness through a pot once it is on
the white LED strip will be activated by a button (which will likely be a PIR sensor, the one i have is just a digital 5v input when motion is detected)
when the button is "pressed and held" (the PIR has something within range) it will switch off the red LED strip and fade in the white LED strip
and when the button is released the white LED strip fades out and the red LED strip resumes while still able to be dimmed in and out
the two focus lights are a kind of optional thing, they would be able to turn on and off independently regardless of the state of the button or switch
i can live without the focus lights and just control them through a simple relay circuit without the arduino
I have went through 4 different sketches trying to get this program to work and basically all I have been able to do is dim the red LED strip and control
the white led strip through digitalWrite but the whole point is to be able to dim the white LED strip in and out, otherwise I would just make the whole setup
with 12v relays and my other power supply to just switch between the two sets of LEDs, im pretty sure i'm not asking much of the arduino to accomplish this
so any help whatsoever would be greatly appreciated */
//Constants
const int REDSTRIP = 10; //RED LED strip
const int WHITESTRIP = 9; //WHITE LED strip
const int LEFT_LED = 8; //Left "focus" beam LED
const int RIGHT_LED = 7; //Right "focus" beam LED
const int TBUTTON = 2; //Button for transitioning between red and white LED strips (possibly a PIR sensor as stated above)
const int SWITCH = 3; //Switch to select the state of the red LEDs (on or off)
const int LSWITCH = 12; //button (not momentary) for activating the left "focus" LED
const int RSWITCH = 11; //button (not momentary) for activating the right "focus" LED
//variables
int buttonState = 0; //the position of the button that transition between the white and red LED strips
int switchState = 0; //the position of the switch that activates the red LED strip
int LbuttonState = 0; //the state (1 on 0 off) of the button for the left "focus" beam
int RbuttonState = 0; //the state (1 on 0 off) of the button for the right "focus" beam
int val = 0; //initial value used for referencing the potentiometer input(A0)
int valL = 0; //the initial value of the button for the left "focus" beam for reference
int valR = 0; //the initial value of the button for the right "focus beam for reference
int old_valL = 0; //variable for storing the previous value of valL
int old_valR = 0; //variable for storing the previous value of valR
int fade = 0; //initial value for the fade in or out of the white LED strip
void setup() {
pinMode(REDSTRIP, OUTPUT); //assign the red LED strip as an output
pinMode(WHITESTRIP, OUTPUT); //assign the white LED strip as an output
pinMode(LEFT_LED, OUTPUT); //assign the left "focus" LED as an output
pinMode(RIGHT_LED, OUTPUT); //assign the right "focus" LED as an output
pinMode(TBUTTON, INPUT); //assign the white/red LED transition button as an input
pinMode(SWITCH, INPUT); //assign the red LED strip turn-on switch as an input
pinMode(LSWITCH, INPUT); //assign the left "focus" LED button as an input
pinMode(RSWITCH, INPUT); //assign the right "focus" LED button as an input
}
//Red LED strip (this works to transition between the red and white strips)
void loop() { // begin loop
buttonState = digitalRead(TBUTTON); // define TBUTTON state as buttonState
delay(10);
val = analogRead(0); //define analog input as val
if (buttonState == HIGH) {
analogWrite(REDSTRIP, val/4);
} else {
digitalWrite(REDSTRIP, LOW);
{
//white led strip
buttonState = digitalRead(TBUTTON);
delay(10);
// this work EXCEPT!!! when the "TBUTTON" is held the white LEDs
if (buttonState == HIGH) { // constantly go through a loop of dimming down to 0 and resetting
for (fade = 0; fade == 255; fade++); { // back to 255 and dimming back down to 0 and on and on... (likely because I used some
analogWrite(WHITESTRIP, fade); // of the code from the "dim in and out like a sleeping mac" example
delay(8); // in the "getting started with arduino" guidebook) and what I would like it
} // to do is fade in and stay on when held and fade out and stay off when released
// as well as cancelling out the red LED strip when the white strip is active
} else { // (which is currently working)
for (fade = 255; fade >= 0; fade--) {
analogWrite(WHITESTRIP, fade);
delay(10);
valL = digitalRead(LSWITCH); // read and store the input value of the Left beam button
if ((valL == HIGH) && (old_valL == LOW)) {
LbuttonState = 1 - LbuttonState;
delay(5);
}
old_valL = valL;
if (LbuttonState == 1) {
digitalWrite(LEFT_LED, LOW);
} else {
digitalWrite(LEFT_LED, HIGH);
}
valR = digitalRead(RSWITCH); // read and store the input value of the Right beam button
if ((valR == HIGH) && (old_valR == LOW)) {
RbuttonState = 1 - RbuttonState;
delay(5);
}
old_valR = valR;
if (RbuttonState == 1) {
digitalWrite(RIGHT_LED, LOW);
} else {
digitalWrite(RIGHT_LED, HIGH);
}
}
}
}
}
}