LED brightness control with one push button

Hello! I'm new in the arduino world and I want to start my first project. I want to make a push button to be a toggle for the led with a single press and if I hold it down for more than 2 seconds to start increasing the brightness with PWM as with potentiometer. And if I release the button and hold it again to switch the fading direction. Do you have any suggestions? And my second question is how can I make the program with Web interface to control it either with a push button and remote with a samrtphone/tablet?

I want to make a push button

Probably be far simpler to just buy one.

to be a toggle for the led with a single press and if I hold it down for more than 2 seconds to start increasing the brightness with PWM as with potentiometer.

Whoa, there, Nelly, slow down. You’ll bust a gut if you don’t breathe now and then.

Do you have any suggestions?

Look at the state change detection example. Examine how it determines that the switch has gone from not pressed to pressed, or from not pressed to pressed.

Those are events of interest. You’ll need to record when the change of interest happens. Then, you can make decisions based on how long between the events.

You have not described what you want to do in enough detail for anyone else to actually write the code. It’s one thing if the LED should toggle when the switch is pressed and released less than two seconds later, or should start fading if the switch is pressed and released more than two seconds later. It’s another thing if the LED should toggle when the switch is pressed, and should start fading if the release happens more than two seconds later. It’s a third thing if the LED should toggle when the switch is pressed, and fading should happen 2 seconds later if the switch is still pressed.

So, which of the three scenarios are you wanting to see happen?

And my second question is how can I make the program with Web interface to control it either with a push button and remote with a samrtphone/tablet?

Do you have an Ethernet (or WiFi) shield? If so, one way makes sense. If not, another approach is needed.

Building a web interface is NOT a beginner project.

@PaulS, thanks for your answer! I'm a begginer in Arduino world but I forgot to mention that I can write on C. My first thought was that when i press the button the Led to toggle and if I hold it for more than 2 seconds to start increasing its brightness. Maybe this is not for begginers so now I'm thinking of something that it might be more easy. I want to have three ports - one for the switch, one for PWM and one to toggle with the switch/push button. Later I will post my code.

My first thought was that when i press the button the Led to toggle and if I hold it for more than 2 seconds to start increasing its brightness.

So, let's think about that. You press the switch. The LED comes on. You release the switch in 1/4 of a second, so the LED is simply on (full brightness). You press the switch again, and the LED goes off. You release the switch 2.5 seconds later, and the LED is supposed to get brighter?

What happens when the LED gets to full brightness?

Suppose the LED is fading up, and you press the switch again, for less than two seconds. Should the LED go off? Or should it go to fully on? Suppose the LED is fading up, and you press the switch again, for more than two seconds. Should the LED start fading down from 255? Or from where it is? What should happen when the value gets to 0?

How long between steps? How big should the steps be?

See what I mean about needing to define all the requirements, and to think about the various use cases?

And this part is the easy part.

What happens when web control is added, and the web user and physical user are fighting over what the LED should do?

The Led's brightness is increasing as long as I am holding the button. If i release it it stops to increase and stays ?t where it stopped increasing. When the LED reaches full brightness it changes the direction of fading and the LED's brightness is being decreased. Also when I stop increasing/decreasing the brightness and toggle the LED to go off, in the next toggle ON I need to save the last brightness it was.

The Led's brightness is increasing as long as I am holding the button. If i release it it stops to increase and stays ?t where it stopped increasing. When the LED reaches full brightness it changes the direction of fading and the LED's brightness is being decreased. Also when I stop increasing/decreasing the brightness and toggle the LED to go off, in the next toggle ON I need to save the last brightness it was.

Is that what you want? If not, you need to post the code.

PaulS:

The Led’s brightness is increasing as long as I am holding the button. If i release it it stops to increase and stays ?t where it stopped increasing. When the LED reaches full brightness it changes the direction of fading and the LED’s brightness is being decreased. Also when I stop increasing/decreasing the brightness and toggle the LED to go off, in the next toggle ON I need to save the last brightness it was.

Is that what you want? If not, you need to post the code.

const int buttonPin = 2;           // number of the button pin
const int ledPin = 9;              // number of the LED pin
 
float brightness = 0;              // brightness of LED starts at 0
int fadeAmount = 51;               // fadeAmount is equal to 20% of 255(max LED brightness)
float buttonPressedCounter = 0.0;  // timer set to 0 whe depressing button
int buttonState = 0; 

 
void setup() {
  // set button as input
  pinMode(buttonPin, INPUT);
  // set LED as output
  pinMode(ledPin, OUTPUT);
  // init serialize communication at 9600 bits per second
  Serial.begin(9600);
}
 
void loop() {
  // activate when button is being held down
  if (buttonState == HIGH) {
    // on press, when counter is greater than 33, assume button is being held down
    if (buttonPressedCounter > 33) {
      // button was held down for longer than 33
      if (brightness < 255) {
        Serial.print("increasing brightness: ");
        Serial.println(brightness);
        brightness = brightness + 5;
        analogWrite(ledPin, brightness);
      }
    } else {
      buttonPressedCounter ++;
      Serial.print("holding: ");
      Serial.println(buttonPressedCounter);
    }
  } else if (buttonState == LOW) {
    // on release if button pressed counter was less than 33, assume it was a short press
    if ((buttonPressedCounter < 33) & (buttonPressedCounter != 0)) {
      Serial.print("lower LED: ");
      if (brightness - fadeAmount < 0.0) {
        brightness = 0;
      } else {
        brightness = brightness - fadeAmount;
      }
      Serial.println(brightness);
      analogWrite(ledPin, brightness);
    }
    buttonPressedCounter = 0;
  }
}

That is my code. It compiles but it doesn’t make what I want. Actually with this code nothing happens. I want to do the thing I described in my last post.

  // activate when button is being held down
  if (buttonState == HIGH) {

You didn't look at the state change detection example, did you?

You need to detect when the switch BECOMES pressed or BECOMES released. The current state of the switch is almost immaterial. But, that is ALL that this code cares about.

PaulS:   // activate when button is being held down   if (buttonState == HIGH) {

You didn't look at the state change detection example, did you?

You need to detect when the switch BECOMES pressed or BECOMES released. The current state of the switch is almost immaterial. But, that is ALL that this code cares about.

How to do this? What function should I use for this?

How to do this?

I'm starting to sound like a broken record this morning. Look at the state change detection example, to see how to detect that the switch has changed states (from pressed to released or from released to pressed).

Well. Ive had same project for dimm my led strip under the bed with two pushbuttons wired to one input. Output drives a MOSFET driver for LED strip so its connected directlly.

This is the code that works fine;

const int led = 11;
const int sw = 2;
int level=0,pt=80,rampa=20,lastst=0,triggered=0,direct=0;
volatile byte swstate=LOW;
unsigned long current=0;
unsigned long push=0;

void setup() {
  pinMode (sw,INPUT);
  pinMode (led,OUTPUT);
  digitalWrite(led,LOW);
  attachInterrupt(digitalPinToInterrupt(sw),Button,RISING);
}

void loop() {
       if(swstate==1){
      current=millis();
      while(current+pt>millis()){
      }
      lastst=digitalRead (sw);
       }
     while(swstate==1&&current+1000>millis()&&lastst==1){
            if (swstate==1){
            Toggle();
            }
            else{
            break;
            }
          }
      while (digitalRead(sw)==HIGH&&swstate==0&&current+1000<millis()){
        Brightness();
      }
}

int Brightness(){
  triggered=1;
  if(digitalRead(led)==HIGH){
    direct=1;
    level=255;
  }
  if(digitalRead(led)==LOW){
    direct=0;
    level=0;
  }
  while(digitalRead(sw)==HIGH){
    
    while(digitalRead(sw)==HIGH&&direct==0){
      analogWrite(led,level);
      level++;
      delay(rampa);
      if(level==255){
        direct=1;
      }
    }
  while(digitalRead(sw)==HIGH&&direct==1){
      analogWrite(led,level);
      level--;
      delay(rampa);
      if(level==0){
        direct=0;
      }
    }
  }
}
int Toggle(){
              if(triggered==1){
                digitalWrite(led,LOW);
                triggered=0;
              }
              else{
              digitalWrite(led,!digitalRead(led));
              }
              swstate=0;
            }
void Button(){
              swstate=1;
              current=0;
             }
     while(swstate==1&&current+1000>millis()&&lastst==1){

Isyourspacekeybroken?

What’s
up
with
the
crappy
indenting?