arduinologicledproximity

How do i make an ultrasonic sensor trigger an LED to stay on until reset?


So, I know it's probably SUPER simple, but I'm new to arduino and I'm just drawing a blank. I'm making a motion detector with an HC-SRO4 ultrasonic sensor. Right now I have it set so whenever it senses an object within 60 cm, it turns on an LED light, but when the object disappears, the LED turns off. What I would like to happen is that it would stay on until I press a button to reset it. Any help is really appreciated and i thank you in advance.

void setup() {
#define LED 8
#define trigPin 12
#define echoPin 13
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LED, OUTPUT);

}



void loop() {
 int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1



;if (distance >= 60 || distance <= 0){
Serial.println("no object detected");
digitalWrite(LED, LOW);}

else {
Serial.println("object detected");
digitalWrite(LED, HIGH);
}}

Solution

  • for this you have make low pushbutton pin while pressing.once the oobject came near the flag will set once the set it will move to while loop and run until the button pressed

    #define LED 8
    #define trigPin 12
    #define echoPin 13
    #define push_button 5
    int flag=0;
    void setup() {
    
    Serial.begin (9600);
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
    pinMode(LED, OUTPUT);
    pinMode(push_button,INPUT);
    }
    
    
    
    void loop() {
     int duration, distance;
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(1000);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    distance = (duration/2) / 29.1;
    if(digitalRead()==0)
    {
    flag=0;
    }
    
    
    if (distance >= 60 || distance <= 0){
    Serial.println("no object detected");
    
    digitalWrite(LED, LOW);}
    
    else {
    Serial.println("object detected");
    flag=1;
    while(flag=1)
    {
      digitalWrite(LED, HIGH);}
    }}