Using multiple buttons with ultrasonic sensor (HC-SR04)

Hi, I've been working on doing a line follower robot, that requires me to select which way to go (I use button to select which way). While I was in the final phase of my project, I ran into a problem. I have to use 3 buttons (2 for selecting, 1 for like the enter button). But, when I press the "enter button", the ultrasonic only respond once. I need it to always checking the distance to avoid obstacles.
I use this code to test the it

#define F_trigPin 50
#define F_echoPin 48

int inPin1 = 41;
int inPin2 = 39;
int inPin3 = 35;

int reading;
int reading2;
int reading3;

int previous = HIGH;
int previous2 = HIGH;
int previous3 = HIGH;

int state = LOW;
int state2 = LOW;

long time = 0;
long debounce = 200;

void setup() {
  // put your setup code here, to run once:
  pinMode(F_trigPin, OUTPUT);
  pinMode(F_echoPin, INPUT);

  pinMode(inPin1, INPUT);
  pinMode(inPin2, INPUT);
  pinMode(inPin3, INPUT);

  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:

  reading = digitalRead(inPin1);
  reading2 = digitalRead(inPin2);
  reading3 = digitalRead(inPin3);
  
  long F_dur, F_cm;
  F_trigger_ready();
  F_dur = pulseIn(F_echoPin, HIGH);
  F_cm = (F_dur/2)/29.1;

  if (reading == HIGH && previous == LOW && millis() - time > debounce) 
  {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

    time = millis();    
  }

  if (reading2 == HIGH && previous2 == LOW && millis() - time > debounce) 
  {
    if (state2 == HIGH)
      state2 = LOW;
    else
      state2 = HIGH;

    time = millis();    
  }

  if (reading3 == HIGH && previous3 == LOW && millis() - time > debounce) 
  {
    Serial.print(F_cm);
    Serial.println("cm");

    if (F_cm < 20)
    {
        Serial.println("Detected");
    }
      
    time = millis();
  }  

  previous = reading;
  previous2 = reading2;
  previous3 = reading3;
}

void F_trigger_ready()
{
  digitalWrite(F_trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(F_trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(F_trigPin, LOW);
}

inPin1 = first way
inPin2 = second way
inPin3 = enter button

I'm using this button

amrin:
Hi, I've been working on doing a line follower robot, that requires me to select which way to go (I use button to select which way). While I was in the final phase of my project, I ran into a problem. I have to use 3 buttons (2 for selecting, 1 for like the enter button). But, when I press the "enter button", the ultrasonic only respond once. I need it to always checking the distance to avoid obstacles.
I use this code to test the it

#define F_trigPin 50

#define F_echoPin 48

int inPin1 = 41;
int inPin2 = 39;
int inPin3 = 35;

int reading;
int reading2;
int reading3;

int previous = HIGH;
int previous2 = HIGH;
int previous3 = HIGH;

int state = LOW;
int state2 = LOW;

long time = 0;
long debounce = 200;

void setup() {
  // put your setup code here, to run once:
  pinMode(F_trigPin, OUTPUT);
  pinMode(F_echoPin, INPUT);

pinMode(inPin1, INPUT);
  pinMode(inPin2, INPUT);
  pinMode(inPin3, INPUT);

Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:

reading = digitalRead(inPin1);
  reading2 = digitalRead(inPin2);
  reading3 = digitalRead(inPin3);
 
  long F_dur, F_cm;
  F_trigger_ready();
  F_dur = pulseIn(F_echoPin, HIGH);
  F_cm = (F_dur/2)/29.1;

if (reading == HIGH && previous == LOW && millis() - time > debounce)
  {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

time = millis();   
  }

if (reading2 == HIGH && previous2 == LOW && millis() - time > debounce)
  {
    if (state2 == HIGH)
      state2 = LOW;
    else
      state2 = HIGH;

time = millis();   
  }

if (reading3 == HIGH && previous3 == LOW && millis() - time > debounce)
  {
    Serial.print(F_cm);
    Serial.println("cm");

if (F_cm < 20)
    {
        Serial.println("Detected");
    }
     
    time = millis();
  }

previous = reading;
  previous2 = reading2;
  previous3 = reading3;
}

void F_trigger_ready()
{
  digitalWrite(F_trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(F_trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(F_trigPin, LOW);
}




I'm using this button

I can't follow your program from what you explained. Which pin has the "enter button" and which pins are the which way button and what is the third pin for? Would be easier if you used names that match your description.

Paul

If you want to display "detected" whenever something is close, DON'T make that happen only when 'reading3' goes HIGH.

  if (reading3 == HIGH && previous3 == LOW && millis() - time > debounce) 
  {
    Serial.print(F_cm);
    Serial.println("cm");

    if (F_cm < 20)
    {
        Serial.println("Detected");
    }
      
    time = millis();
  }

Move that printing up neat the top of loop() where you measure the distance. You will need to find something else to do when reading3 goes HIGH. Maybe set the direction.