When I long press the push button then the led is continually blinking that shod not happen it shod take only on

If take am Arduino a push button and an led

When I push the button the light is turning on.
When I push the button again the is turning off.

But the problem is when I continually pressing for long time the led is continually turning on off

This is my code

#define SwitchPin1 4 
#define SwitchPin2 16
#define RelayPin1 33
#define RelayPin2 25

int toggleState_1 = 0; 
int toggleState_2 = 0;

void setup()
{ 
  pinMode(RelayPin1, OUTPUT); 
  pinMode(RelayPin2, OUTPUT); 
  pinMode(SwitchPin1, INPUT_PULLUP);
  pinMode(SwitchPin2, INPUT_PULLUP);
  digitalWrite(RelayPin1, toggleState_1);
  digitalWrite(RelayPin2, toggleState_2);
}


void loop() 
{  
 if (digitalRead(SwitchPin1) == HIGH)
    {
      delay (300);
      relayOnOff(1);      
      toggleState_1;   // Update Button Widget
    }

 if (digitalRead(SwitchPin2) == HIGH)
    {
      delay (300);
      relayOnOff(2);      
      toggleState_2;   // Update Button Widget
    }
}

void relayOnOff(int relay){

    switch(relay){
      case 1: 
             if(toggleState_1 == 0){
              digitalWrite(RelayPin1, HIGH); // turn on relay 1
              toggleState_1 = 1;
              Serial.println("Device1 ON");
              }
             else{
              digitalWrite(RelayPin1, LOW); // turn off relay 1
              toggleState_1 = 0;
              Serial.println("Device1 OFF");
              
              }
      break;
      case 2: 
             if(toggleState_2 == 0){
              digitalWrite(RelayPin2, HIGH); // turn on relay 2
              toggleState_2 = 1;
              Serial.println("Device2 ON");
              }
             else{
              digitalWrite(RelayPin2, LOW); // turn off relay 2
              toggleState_2 = 0;
              Serial.println("Device2 OFF");
              
              }
      break;
      default : break;      
      }  
}

ezgif.com-gif-maker (1)

In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the </> icon from the ‘reply menu’ to attach the copied sketch.

1 Like

You will probably have to fix your code. Hint: post it (and use code tags as described in How to get the best out of this forum).

Which led?

Hello a1b23456a
Take a look into the IDE examples to find a proper solution for your project.
Have a nice day and enjoy coding in C++.

#define SwitchPin2 16
#define RelayPin1 33
#define RelayPin2 25

int toggleState_1 = 0; 
int toggleState_2 = 0;

void setup()
{ 
  pinMode(RelayPin1, OUTPUT); 
  pinMode(RelayPin2, OUTPUT); 
  pinMode(SwitchPin1, INPUT_PULLUP);
  pinMode(SwitchPin2, INPUT_PULLUP);
  digitalWrite(RelayPin1, toggleState_1);
  digitalWrite(RelayPin2, toggleState_2);
}


void loop() 
{  
 if (digitalRead(SwitchPin1) == HIGH)
    {
      delay (300);
      relayOnOff(1);      
      toggleState_1;  
    }

 if (digitalRead(SwitchPin2) == HIGH)
    {
      delay (300);
      relayOnOff(2);      
      toggleState_2;   
    }
}

void relayOnOff(int relay){

    switch(relay){
      case 1: 
             if(toggleState_1 == 0){
              digitalWrite(RelayPin1, HIGH); 
              toggleState_1 = 1;
              Serial.println("Device1 ON");
              }
             else{
              digitalWrite(RelayPin1, LOW); 
              toggleState_1 = 0;
              Serial.println("Device1 OFF");
              
              }
      break;
      case 2: 
             if(toggleState_2 == 0){
              digitalWrite(RelayPin2, HIGH); 
              toggleState_2 = 1;
              Serial.println("Device2 ON");
              }
             else{
              digitalWrite(RelayPin2, LOW); 
              toggleState_2 = 0;
              Serial.println("Device2 OFF");
              
              }
      break;
      default : break;      
      }  
}```

A line like toggleState_1; does absolutely nothing.

You don't want to react on the fact that a button is HIGH, you want to react on the fact that the button changes state from LOW to HIGH. Look at the state change detection example that comes with the IDE.

Can send an example code

That´s a nice idea, please do it.

Here is an Arduino example that will toggle the LED between ON and OFF each time you press the button.



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

// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);

  // set initial LED state
  digitalWrite(ledPin, ledState);
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH), and you've waited long enough
  // since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }

  // set the LED:
  digitalWrite(ledPin, ledState);

  // save the reading. Next time through the loop, it'll be the lastButtonState:
  lastButtonState = reading;
}

Link for the Arduino simulation: sketch.ino - Wokwi Arduino and ESP32 Simulator

I hope you can build the required logic for two buttons and two LEDs (relays) in the same way. If you have doubts, you are welcome to ask

TQ :blush: TQ very much for ur help

//When I long press the push button then the led is continually blinking that should not happen it should take only on

#define SwitchPin1 4 
#define SwitchPin2 16
#define RelayPin1 33
#define RelayPin2 25

int toggleState_1 = 0; 
int toggleState_2 = 0;

bool SwitchPin1Released = true;
bool SwitchPin2Released = true;

void setup()
{ 
  Serial.begin(115200);
  pinMode(RelayPin1, OUTPUT); 
  pinMode(RelayPin2, OUTPUT); 
  pinMode(SwitchPin1, INPUT);
  pinMode(SwitchPin2, INPUT);
  digitalWrite(RelayPin1, toggleState_1);
  digitalWrite(RelayPin2, toggleState_2);
}


void loop() 
{  

if (digitalRead(SwitchPin1) == LOW) {
   SwitchPin1Released = true;
   delay (10);
}
if (digitalRead(SwitchPin2) == LOW) {
   SwitchPin2Released = true;
   delay (10);
}


if (SwitchPin1Released && digitalRead(SwitchPin1) == HIGH)
    {
      delay (300);
      SwitchPin1Released = false;
      relayOnOff(1);      
      toggleState_1;  
    }

 if (SwitchPin2Released && digitalRead(SwitchPin2) == HIGH)
    {
      delay (300);
      SwitchPin2Released = false;
      relayOnOff(2);      
      toggleState_2;   
    }
  
}

void relayOnOff(int relay){

    switch(relay){
      case 1: 
             if(toggleState_1 == 0){
              digitalWrite(RelayPin1, HIGH); 
              toggleState_1 = 1;
              Serial.println("Device1 ON");
              }
             else{
              digitalWrite(RelayPin1, LOW); 
              toggleState_1 = 0;
              Serial.println("Device1 OFF");
              
              }
      break;
      case 2: 
             if(toggleState_2 == 0){
              digitalWrite(RelayPin2, HIGH); 
              toggleState_2 = 1;
              Serial.println("Device2 ON");
              }
             else{
              digitalWrite(RelayPin2, LOW); 
              toggleState_2 = 0;
              Serial.println("Device2 OFF");
              
              }
      break;
      default : break;      
      }  
}
1 Like

How to add serial communication from one Arduino to another.
This program should work as master and the another Arduino should work as slave is used for only led notification

Pls modifi the program

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