Using One Pushbutton as Toggle Switch and dimmer

Hello Folks!

I am new here and a beginner to Arduino programming.

I am stuck at a project and after trying different methods to execute my program which I failed, i sought to take help at this forum.

My program is to use a single pushbutton to Turn off and on LED but also dimming or brightening the LED with the same Pushbutton.
If the Pushbutton is pressed only once if acts as on/off switch and turns LED on if it is off in current state and vice versa. While the LED is turned on , if the button is pressed long it dims the LED light and next time the button is pressed long it brightens the led and vice-versa.

Here is my code, all help is greatly appreciated. Thanks in advance :slight_smile:

//Creating a dimmable Led Lamp using one Pushbutton.
//When button is pressed once if works as on/off switch
// and when it is pressed for long, it will either dims or brightens LED.
//Pressing pushbutton once, turns LED either ON or OFF; whereas keeping pushbutton pressed long with either dim or brighten the LED as per the last action.

int ledState=0; //indicates LED is turned off when program starts
int buttonOld=1; // button State at the start of program
int buttonNew; //button State when pushbutton is pressed everytime.
int ledPin=3; // Assigning Led to Arduino Pin no.3
int buttonPin=10; //Assigning button to pin no. 10

int dt=100;

void setup() {
Serial.begin(9600);
pinMode(ledPin,OUTPUT); //Declaring LED pin is OUTPUT
pinMode(buttonPin, INPUT); //Declaring button Pin is INPUT

}

void loop() {

buttonNew=digitalRead(buttonPin);
if(buttonOld==0 && buttonNew==1)//when buttonState changes from 0 to 1,make the switch
{if(ledState==0){
digitalWrite(ledPin,HIGH);
ledState=1;
}
else{
digitalWrite(ledPin,LOW);
ledState=0;
}
}
buttonOld=buttonNew;
delay(dt);
int ledBright; //making a dimmable LED
int buttonVal=digitalRead(buttonPin);
if(buttonVal==0 && ledState==1) //Means if pushbutton is pressed long and while LED is ON, brighten the LED by value of 15
{ledBright=ledBright+15;}
if(ledBright>255){ledBright=255;

analogWrite(ledPin,ledBright);
}

if(buttonVal==0 && ledState==1) //Means next time when pushbutton is pressed long and while LED is On, it will dim the LED brightness by value of 15.
{ledBright=ledBright-15;}
if(ledBright<0){ledBright=0;

analogWrite(ledPin,ledBright);
}

Serial.print(buttonNew);
Serial.print(",");
Serial.print(ledBright);
Serial.print(",");
Serial.println(buttonVal);
delay(dt);

}

“ I am stuck at a project and after trying different methods to execute my program which I failed,”

Tell us more.

How long is a ON/OFF switch press ?

What length of time is the trigger for a 'long' switch press ?


Show us a good schematic of your circuit.
Show us a good image of your wiring.
Give links to components.
Posting images:


Use CTRL T to format your code.
Attach your ‘complete’ sketch between code tags, use the </> icon in the posting menu.

[code]Paste your sketch here[/code]


//https://forum.arduino.cc/index.php?topic=672444.msg4526560#msg4526560

//Version   YY/MM/DD
// 1.00     18/03/23   Running code

#define PUSHED          LOW
#define NOTPUSHED       HIGH

#define LEDon           HIGH
#define LEDoff          LOW

boolean switchFlag      = false;  //true means we will allow switch hold timing checking
boolean fastFlag        = false;  //false means fast switch operation is enabled

byte ledState           = 0;
byte buttonOld          = 0;
byte buttonNew;
byte LEDstate           = 0;

const byte ledPin       = 3;
const byte buttonPin    = 10;
const byte heartBeatLED = 13;           //LED that flashes at 1Hz to show things are non blocking

int brightness;

//timing stuff
const unsigned long longPress = 1000;  //time before level adjustment start being made
const unsigned long speed     = 250;   //time between each level change

unsigned long heartBeatMillis;
unsigned long switchMillis;
unsigned long longMillis;
unsigned long timePUSHED;

//****************************************************************************************
void setup()
{
  Serial.begin(9600);

  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LEDstate);

  pinMode(heartBeatLED, OUTPUT);

  pinMode(buttonPin, INPUT_PULLUP);

} //END of setup()

//****************************************************************************************
void loop()
{
  //***************************
  //toggle Heartbeat LED every 500ms
  if (millis() - heartBeatMillis > 500)
  {
    //restart timer
    heartBeatMillis = millis();

    //toggle LED
    digitalWrite(heartBeatLED, !digitalRead(heartBeatLED));
  }

  //***************************
  //time to check the switches?
  if (millis() - switchMillis > 50)
  {
    //restart timer
    switchMillis = millis();

    checkSwitches();
  }

  //***************************
  //has the switch been held long enough?
  if (switchFlag == true && millis() - longMillis > longPress)
  {
    //is it time to go to the next level?
    if (digitalRead(buttonPin) == PUSHED && millis() - timePUSHED > speed)
    {
      //restart timing
      timePUSHED = millis();

      //disable fast switch operation
      fastFlag = true;

      //adjust level
      if (LEDstate == false)
      {
        analogWrite(ledPin, brightness);

        brightness = brightness - 15;

        if (brightness < 0)
        {
          brightness = 0;

          LEDstate = false;
          switchFlag = false;
        }
      }

      else
      {
        analogWrite(ledPin, brightness);

        brightness = brightness + 15;

        if (brightness > 255)
        {
          brightness = 255;

          LEDstate = true;
          switchFlag = false;
        }
      }
    }
  }

} //END of loop()

//****************************************************************************************
void checkSwitches()
{
  //***************************
  buttonNew = digitalRead(buttonPin);

  //has the switch changed state?
  if (buttonOld != buttonNew)
  {
    //update to the new state
    buttonOld = buttonNew;

    //***********************
    //was the switch pushed/closed?
    if (buttonNew == PUSHED)
    {
      //start timing
      timePUSHED = millis();
      longMillis = millis();

      switchFlag = true;
    }

    //***********************
    //was the switch released/open?
    else if (fastFlag == false && buttonNew == NOTPUSHED)
    {
      //disable switch 'hold' timing
      switchFlag = false;

      //toggle LED
      digitalWrite(ledPin, LEDstate);

      LEDstate = !LEDstate;

      if (LEDstate == LOW)
      {
        brightness = 255;
      }

      else
      {
        brightness = 0;
      }

    }

    //allow fast switch operation
    fastFlag = false;

  }

} //END of checkSwitches()

//****************************************************************************************

To Larryd

Here are a few Pictures of the wiring.

I do not know how to create a schematic diagram through an App but i have attached an image of it drawn on paper.

The time duration for which a switch should be pressed long to act as a dimmer should be more than 250ms else if the duration is less, it acts as ON/OFF button.

<
//Creating a dimmable Led Lamp using one Pushbutton.
//When button is pressed once if works as on/off switch
// and when it is pressed for long, it will either dims or brightens LED respectively.
//Pressing pushbutton once, turns LED either ON or OFF; whereas keeping pushbutton pressed long with either dim or brighten the LED as per the last action.

int ledState = 0; //indicates LED is turned off when program starts
int buttonOld = 1; // button State at the start of program
int buttonNew; //button State when pushbutton is pressed everytime.
int ledPin = 3; // Assigning Led to Arduino Pin no.3
int buttonPin = 10; //Assigning button to pin no. 10

int dt = 100;

void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT); //Declaring LED pin is OUTPUT
pinMode(buttonPin, INPUT); //Declaring button Pin is INPUT

}

void loop() {

buttonNew = digitalRead(buttonPin);
if (buttonOld == 0 && buttonNew == 1) //when buttonState changes from 0 to 1,make the switch
{ if (ledState == 0) {
digitalWrite(ledPin, HIGH);
ledState = 1;
}
else {
digitalWrite(ledPin, LOW);
ledState = 0;
}
}
buttonOld = buttonNew;
delay(dt);
int ledBright; //making a dimmable LED
int buttonVal = digitalRead(buttonPin);
if (buttonVal == 0 && ledState == 1) //Means if pushbutton is pressed long and while LED is ON, brighten the LED by value of 15
{
ledBright = ledBright + 15;
}
if (ledBright > 255) {
ledBright = 255;

analogWrite(ledPin, ledBright);
}

if (buttonVal == 0 && ledState == 1) //Means next time when pushbutton is pressed long and while LED is On, it will dim the LED brightness by value of 15.
{
ledBright = ledBright - 15;
}
if (ledBright < 0) {
ledBright = 0;

analogWrite(ledPin, ledBright);
}

Serial.print(buttonNew);
Serial.print(",");
Serial.print(ledBright);
Serial.print(",");
Serial.println(buttonVal);
delay(dt);

}

You have posted code without using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don’t do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons. The “Code: [Select]” feature allows someone to select the entire sketch so it can be easily copied and pasted into the IDE for testing.
If you have already posted without using code tags, open both your messages and select “modify” from the pull down menu labelled, “More”, at the lower right corner of the message. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button. Code tags can also be inserted manually in the forum text using the code and /code metatags.


Did you try the code posted in reply #1 above.

It will give you some ideas on what can be done.