single button led on/off and fade

single button led on/off and fade

Features are to be accompanied by two of example you want to sample!
Council to ask what method?

fade led button

#define BUTTON 7
#define LED 9

int dir = 1;
int lum = 0;
boolean toggle = false;

void setup() {
  pinMode(BUTTON, INPUT);
  pinMode(LED, OUTPUT);
}

void loop() {
  if (digitalRead(BUTTON) == HIGH) {
    toggle = true;
    lum += dir;
    if ((lum >= 0) && (lum < 256))
      analogWrite(LED, lum);
    delay(20);
  } else { // toggle direction
    if (toggle) {
      dir = -1 * dir;
      toggle = false;
    }
  }
}

led on/off

int LED = 9;
int BUTTON = 7;
int lastButtonState = HIGH;
int ledState = HIGH;

void setup()
{
  pinMode(LED, OUTPUT);
  pinMode(BUTTON, INPUT);
}

void loop()
{
 
  int buttonState = digitalRead(BUTTON);
  
  if (buttonState==LOW && buttonState!=lastButtonState)
  {
    
    if (ledState==HIGH)
    {
      ledState = LOW;
    }
    else
    {
      ledState = HIGH;
    }
  }
  digitalWrite(LED, ledState);
  
  lastButtonState = buttonState;
  
  delay(20);
}

Is there a question here or is it just example code?

dpsslaser:
Two codes assemble but how?

See this:-
http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

But depending on how you want the final thing to work you might have to do this:-
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html

dpsslaser:
It's all too complicated for me!
could it help?

I don't think your language skills are up to being helped.
If that simplified method is too complex for you then you need to learn more about how to program before you can attempt this.
Try some tutorials and the examples in the arduino IDE, look at the blnk without delay example.
Learn to stand before trying to run.