Led and push button with sensor

I am using a sensor with push button and a relay module what I want to do is when I press the push button the readings I have are held and held until the sensor senses . Unless the relay module is not on

Please do not post in "Uncategorized"; see the sticky topics in https://forum.arduino.cc/c/using-arduino/uncategorized/184.

Please do not post screenshots of code; read How to get the best out of this forum and pay attention to the section about code tags.

const int BUTTON1 = 8;
const int Relay1 =2;
const int Sensor1 = 3;

bool out1=true,out2=true;
const int dbounce = 50;

void setup() {
pinMode(Relay1,OUTPUT);
pinMode(Sensor1,INPUT);
pinMode(BUTTON1, INPUT_PULLUP);

}

void loop() {
if(digitalRead(BUTTON1)==LOW){
delay(dbounce);
if(digitalRead(BUTTON1)==LOW)
{

if(digitalRead(Sensor1)==LOW)
{
     out1 = false;
  }
}

}
digitalWrite(Relay1, out1);
}

When you use code tags <code> your code will look like this when you post it on the forum. Much easier for everyone to read

const int BUTTON1 = 8;
const int Relay1 = 2;
const int Sensor1 = 3;

bool out1 = true, out2 = true;
const int dbounce = 50;

void setup() {
  pinMode(Relay1, OUTPUT);
  pinMode(Sensor1, INPUT);
  pinMode(BUTTON1, INPUT_PULLUP);

}

void loop() {
  if (digitalRead(BUTTON1) == LOW) {
    delay(dbounce);
    if (digitalRead(BUTTON1) == LOW)
    {

      if (digitalRead(Sensor1) == LOW)
      {
        out1 = false;
      }
    }

  }
  digitalWrite(Relay1, out1);
}

thanx sir

Readings from what?

for example when i upload the code and reading on serial monitor is 0 when i press the push bottom reading is 1 and unless my proximity sensor is not active reading is hold on 1 means high state

i have used 2 push buttons, 2 proximity sensors and 2 relay modules with arduino condition is when i will press 1st push button sensor1 is active and relay1 is also active but when sensor1 is sense then relay1 will give output like buzzer sound etc.
when i will press second push button sensor1 is off and relay1 is also off state and sensor2 is active and relay2 is also active after that if sensor2 is sense then relay2 gives output

The button press is "bouncing" so the relay is commanded to switch on and off rapidly.
https://www.circuitbasics.com/how-to-use-switch-debouncing-on-the-arduino/

Look for "arduino debounce button"

when i press the pushbutton reading is hold relay is giving output until i don't press the second pushbutton this is doing work.

but i need when i press the 1st push button reading is hold and relay do not give output unless sensor is not detect to object and then i press the second push button previous sensor and relay is off and second reading is hold second relay do not give output unless second sensor is not sense.

That was shown to you in a previous post.

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