Programming my desired function to my Arduino

I want to have a program that when I press the push button my LED should turn on and if I press the button again twice my LED should turn off, can someone help me create this program on my arduino?

The State Change Detect example in the ide (file > examples > 2 digital) and here would be a good place to start. It counts button presses... You could probably adapt it so the press #1 turns on the led, and #3 turns it off. Then reset the counter so the 4th press is a new #1.

Have a think about that.

@newbie19, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

You will need to study the state change detection example that comes with the IDE. When a button becomes pressed, you switch the led on, when it becomes pressed again it switches off.

It's a little more complicated than above as you want two presses to switch it off so you need to keep a counter that you set to 0 when the LED is switched on, increment when the LED is on and the state change is detected and only switch the LED of if the count reaches 2.

sterretje and I provided similar suggestions. But you also need to clarify if "press the button again twice" means 2 presses a week apart of if you mean the more traditional double-click like on a mouse. In the latter case you need to start a timer so that if the double click is too slow it doesn't register as a double click. There are a number of libraries that take care of that under the hood.

Given that this is clearly a bit of homework, I don't expect anyone to give you working code for this. But you can post your attempts if you get stuck for help.

Hello
Check out this sketch. The sketch is using an array to collect all relevant information like pin addresses, states and times. Inside the loop() a button debouncing, state change detection and led action will take place on this information.

/* BLOCK COMMENT
  ATTENTION: This Sketch contains elements of C++.
  https://www.learncpp.com/cpp-tutorial/
  https://forum.arduino.cc/t/programming-my-desired-function-to-my-arduino/927984
*/
#define ProjectName "Programming my desired function to my Arduino"
// HARDWARE SETTINGS
// YOU MAY NEED TO CHANGE THESE CONSTANTS TO YOUR HARDWARE AND NEEDS
constexpr byte ButtonPin {A0};      // portPin o---|button|---GND
constexpr byte Led1Pin {3};         // portPin o---|220|---|LED|---GND
#define OutPutTest
// CONSTANT DEFINITION
enum {Button_};
enum {Led1};
enum {One};
constexpr byte Input_[] {ButtonPin};
constexpr byte Output_[] {Led1Pin};
constexpr  unsigned long OutPutTestTime {1000};
// VARIABLE DECLARATION AND DEFINITION
unsigned long currentTime;
struct TOOGLEBUTTON {
  byte pin;
  bool statusQuo;
  byte led ;
  unsigned long stamp;
  unsigned long duration;
} toogleButton {Input_[Button_], false, Output_[Led1], 0, 20};
// -------------------------------------------------------------------
void setup() {
  Serial.begin(9600);
  Serial.println(F("."));
  Serial.print(F("File   : ")), Serial.println(__FILE__);
  Serial.print(F("Date   : ")), Serial.println(__DATE__);
  Serial.print(F("Project: ")), Serial.println(ProjectName);
  pinMode (LED_BUILTIN, OUTPUT);  // used as heartbeat indicator
  //  https://www.learncpp.com/cpp-tutorial/for-each-loops/
  for (auto Input : Input_) pinMode(Input, INPUT_PULLUP);
  for (auto Output : Output_) pinMode(Output, OUTPUT);
#ifdef OutPutTest
  // check outputs
  for (auto Output : Output_) digitalWrite(Output, HIGH), delay(OutPutTestTime);
  for (auto Output : Output_) digitalWrite(Output, LOW), delay(OutPutTestTime);
#endif
}
void loop () {
  currentTime = millis();
  digitalWrite(LED_BUILTIN, (currentTime / 500) % 2);
  // debounce button
  if (currentTime - toogleButton.stamp >= toogleButton.duration) {
    toogleButton.stamp = currentTime;
    // check state change
    bool stateNew = !digitalRead(toogleButton.pin);
    if (toogleButton.statusQuo != stateNew) {
      toogleButton.statusQuo = stateNew;
      // take action on change
      if (stateNew) {
        digitalWrite(toogleButton.led, !digitalRead(toogleButton.led));
      }
    }
  }
}

Have a nice day and enjoy coding in C++.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.