I'm having trouble doing exactly what the title says. I need an led strip to fade on when a switch is turned on and then when the switch its turned off I need the led strip to turn off. I'm new to Arduino and would like some help with code. What I have so far makes the led strip fade on and then goes off and fades back on. I can get it to turn off by toggling the switch, though. I need it to fade on and stay on until the switch is hit, then turn off. Thanks. P.S. I know the code is a mess, I combined other people's code because I had no idea how to do it myself.
const int buttonPin = 2;
int led = 9; // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
int buttonState = 0;
int fadeValue = 0;
int run;
// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
run = 0;
}
// the loop routine runs over and over again forever:
void loop() {
if (digitalRead(buttonPin) == LOW) //funcitons based off of button pulling input pin LOW
{
if (run == 0)
{
run = 255;
}
else
{
run = 0;
}
}
if (run > 0)
{
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
// fade in from min to max in increments of 5 points:
for (fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
// sets the value (range from 0 to 255):
analogWrite(led, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
} else {
// turn LED off:
digitalWrite(led, LOW);
brightness = 0;
fadeValue = 0;
}
}
}
what are you trying to achieve with your run variable?
what you need to do is remember once you've done the for (fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) loop to not do that again until the button is released
const int buttonPin = 2;
const int led = 9; Â Â Â // the PWM pin the LED is attached to
int brightness = 0; Â Â // how bright the LED is
const int fadeAmount = 5; Â Â // how many points to fade the LED by
// the setup routine runs once when you press reset:
void setup() {
 // declare pin 9 to be an output:
 pinMode(led, OUTPUT);
 pinMode(buttonPin, INPUT_PULLUP);
}
// the loop routine runs over and over again forever:
void loop() {
 if (digitalRead(buttonPin) == LOW) {
  // Switch is ON
  brightness += fadeAmount;
  brightness = constrain(brightness, 0, 255);
 } else {
  // Switch is OFF
  brightness = 0;
 }
 analogWrite(led, brightness);
 delay(30);
}
Not sure what I did but it works now. Thanks guys!
const int buttonPin = 2;
int led = 9; // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
int buttonState = 0;
int fadeValue = 0;
int run;
// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(13, OUTPUT);
pinMode(11, OUTPUT);
run = 0;
}
// the loop routine runs over and over again forever:
void loop() {
if (digitalRead(buttonPin) == LOW) //funcitons based off of button pulling input pin LOW
{
if (run == 0)
{
run = 255;
}
else
{
run = 0;
}
}
if (run > 0)
{
}
buttonState = digitalRead(buttonPin);
if (buttonState == LOW ) {
delay(1000);
digitalWrite(11, HIGH);
delay(100);
// fade in from min to max in increments of 5 points:
for (fadeValue = 0 ; fadeValue <= 255; fadeValue += 3) {
// sets the value (range from 0 to 255):
analogWrite(led, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
if (fadeValue > 254 && digitalRead(buttonPin) == LOW) {
while (digitalRead(2) == LOW) {
// Do nothing
}
delay(100);