Leaf Switch Dimmer Programming Help.

Hello, I am really bad at programming, and I need a helping hand. I am trying to make a sketch that when a leaf switch is not pressed, an led on pin 0 of an attiny85 fades in and stays on indefinitely. When the switch is closed, I would like the led to fade out and remain off until the switch is released, thus turning the light back on. I tried to merge the default button sketch and the fade sketch into one, and it works… sort of. The problem is that the led fades in. turns off and fades in again indefinitely. I tried using an if statement to allow the fade in code to run once, but now the led won’t shut off and still has the same problem. I spent hours trying to fix this, and got nothing but a headache. Am I trying to over complicate this placing another “if statement” inside an “if statement” and using an “int”?
Any help would greatly be appreciated.
Also, please don’t beat me up too badly.

Here is the poorly modified code below:

/*
 Button

Turns on and off a light emitting diode(LED) connected to digital
pin 13, when pressing a pushbutton attached to pin 2.


The circuit:
* LED attached from pin 13 to ground
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground

* Note: on most Arduinos there is already an LED on the board
attached to pin 13.


created 2005
by DojoDave <http://www.0j0.org>
modified 30 Aug 2011
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/Button
*/

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 4;     // the number of the pushbutton pin
const int ledPin =  0;      // the number of the LED pin


// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by
int intro = 0;
void setup() {
 // initialize the LED pin as an output:
 pinMode(ledPin, OUTPUT);
 // initialize the pushbutton pin as an input:
 pinMode(buttonPin, INPUT);
}

void loop() {
 // read the state of the pushbutton value:
 buttonState = digitalRead(buttonPin);
 if (buttonState == HIGH) {
 }
 if (intro == 0){
analogWrite(ledPin, brightness);

 // change the brightness for next time through the loop:
 brightness = brightness + fadeAmount;

 // reverse the direction of the fading at the ends of the fade:
 if (brightness == 255 || brightness == 255) {
   intro == 1 ;
 }
 // wait for 30 milliseconds to see the dimming effect
 delay(30);
 }
 else {
   // turn LED off:
   digitalWrite(ledPin, LOW);
 }
}

Ok, first beat up, lol. Please put your code in code tags, so we can read it easier. the icon at top left </>

After I originally made this post, I realized I forgot to mark the code as code. I started to panic, it took me a couple minutes to find the edit button. It's all fixed now. Thanks for not hitting me too hard, Haha.

if (buttonState == HIGH) {
 }

There is nothing inside that if statement to be run.

Have you got the famous “blink led on pin 13” to work already? Previously?

Your sketch has four states.

enum State { off=0; on=1; fading_in=2; fading_out=3 };

State state;
int brightness;

void loop() {
    int sense = readDigital(whatever);
    switch(state) {
    case off:
        if(the switch is released) {
          state = fading_in;
        }
        else {
          // do nothing. stay off.
        }
        break;
    case fading_out:
        if(the switch is released) {
          state = fading_in;
        }
        else if(we have hit minimum brightness value) {
          state = off;
        }
        else {
          lower the brightness of the led;
          // remain in fading_out state
        }
        break;

    case on: 
      do the opposite of off;
      break;

    case fading_in: 
      do the opposite of fading_out;
      break;

    }
}

I tried a rewrite of the code using enum State. The coding is not yet compete when it comes to fading, but I am getting an "else without previous if" error. I'm not sure how to get past this error.

const int buttonPin = 4;     // the number of the pushbutton pin
const int ledPin =  0;      // the number of the LED pin


// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by
int ledBrightness;
int state;
void setup() {
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  // initialize the LED pin as an output:

}

void loop() {
    int sense = digitalRead(buttonPin);
    switch(state) {
      enum State {
  off=0,
  on=1,
  fading_in=2,
  fading_out=3, };
    case off:
        if(sense == LOW) { // if the switch is released
          state = fading_in;
        }
        else {
          // do nothing. stay off.
        }
        break;
    case fading_out:
        if(sense == LOW) //if the switch is released
          state = fading_in;
        }
        else if(we have hit minimum brightness value) { // I get an "else without previous if error"
          state = off;
        }
        else {
          lower the brightness of the led;
          // remain in fading_out state
        }
        break;

    case on:
    //  do the opposite of off;
      break;

    case fading_in:
       brightness = brightness + fadeAmount;
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(30);
      break;

    }
    
}
    case fading_out:
        if(sense == LOW) //if the switch is released
          state = fading_in;
        }
        else if(we have hit minimum brightness value) { // I get an "else without previous if error"
          state = off;
        }

You need to move the } up above state= fading_in;

Thank You Everyone Who Tried To Help, It really means a lot. I have been really busy lately, but the other day I figured it out. My Coding is sloppy, but it works. I thought I would upload the code to share with others. The led brightness has been decreased as well, but can be modified as well.

/*
 The circuit:
 * LED attached from pin 0 to ground (I used a TIP31C Transistor To Turn on 12v Warm White Strip Lighting)
 * Leafswitch attached to pin 4 from +5V
 * 10K resistor attached to pin 4 from ground
 created 2005
 by DojoDave <http://www.0j0.org>
 modified 30 Aug 2011
 by Tom Igoe

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/Button

* Code Modified By Tristan Kuhns For Led Dimming Functions
 
 */

// set pin numbers:
const int buttonPin = 4;     // the number of the pushbutton pin
const int ledPin =  0;      // the number of the LED pin
int ledStatus = 0;
int cycle = 0;
int ledOff = 0;
int finish = 0;

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == LOW && ledStatus == 0) {
    cycle += 1;
    ledOff = 0;
    finish = 0;
    // turn LED on:
     for (int fadeValue = 0 ; fadeValue <= 20; fadeValue += 1) if (cycle == 20) { (ledStatus = 1);
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
    // wait in milliseconds to see the dimming effect
    
    delay(80);
    
  }
  }
  else {
    if (buttonState == LOW && cycle == 20){
    // turn LED off:
    ledOff = 0;
    finish = 0;
    analogWrite(ledPin, 20);
  }
  else{
    if (buttonState == HIGH && ledOff == 0) {
      finish += 1;
      for (int fadeValue = 20 ; fadeValue >= 0; fadeValue -= 1) if (finish == 20) { (ledOff = 1);
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
    // wait in milliseconds to see the dimming effect
    delay(30);
      
    }
  }
  else {
    if (buttonState == HIGH && finish == 20){
      cycle = 0;
      ledStatus = 0;
    // turn LED off:
    digitalWrite(ledPin, LOW);
    
}
  }
}
  }
}