HC SR04 to suspend the calculation until a button is pressed

Hi,

I’m new to coding. I’ve used two HC sr04 sensors to calculate the speed of an object. The problem I’m facing is, I’ve written an if statement; if the object is within 180cm, the mills function gets triggered. I want the further measurements being done on the sensor to stop and do the calculations and display the speed on the I2c display until I press a button. After I press the button I want the void loop to start over again. How do I do this? Is it possible in arduino?
Thanks in advance

Regards
Nava

You can write the codes for measurement inside an 'if' loop.

1 Like

Hi @nava6neeth,

welcome to the arduino-forum.

Yes sure this is possible.
It is a pretty easy exercise.
As you seem to be a newcomer. As long as you show some own effort in learning and then ask specific questions that are asking

  • for learning ressources
  • questions that are specific about a part of a program

You will always get helpful answers.
The first step to show this own effort is to read the "how to get the best out of this forum

I'm pretty sure that you agree and will follow the way how to solve your problem mimimum 200 minutes faster.
This requires to invest 20 minutes of your precious time to read how to speedup solving your problems.

Directly after registering you got presented informations how to speed up solving your problem.
You should really read it.

best regards Stefan

1 Like

Thanks @liaifat85 but I don’t understand, the code is within an if loop. The condition is if the object is within 200cm, they start the calculation.
Do you mean that this condition is enough?

I already did. Thank you

We can kinda guess how this needs to be coded based on your goal of finding the speed of the object.

However what you need to do first is to list (on paper or a text file) every step of the process. Start with both sensors off. Now assume you are the processor, what steps do you take?

Something like:

turn on the 1st sensor
read the 1st sensor
calc the distance
is the distance within 180
if not read the 1st sensor
is the distance within 180

if yes turn off sensor 1
turn on sensor 2
.
.
.
.
..

Hi @JohnRob, Thanks for the response.

The code I've written is as follows

/*

*/
#define rotA = 6
#define rotB = 7
#define ballSpeed = 8
#define speedLimit

// Variables for rotary function
int speedLimit = 0; 
int posA;
int posB;

//Variables for the Ultra Sensor 1
const int trigPin1 = 5;
const int echoPin1 = 6;
float duration1, distance1;

//Variables for Ultra Sensor 2
const int trigPin2 = 9;
const int echoPin2 = 10;
float duration2, distance2;

//Variable for calculating Speed
unsigned long time1;
unsigned long time2;
unsigned long delT;
const float distance = 0.0007;
int speed;

//Buzzer and Led
int buzzer = 12;
int led = 13;
bool reset = 11;


void setup() 
{
  pinMode(rotA, INPUT);
  pinMode(rotB, INPUT);
  
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  
  pinMode(buzzer, OUTPUT);
  pinMode(ked, OUTPUT);

  
  Serial.begin(9600);
  posB = digitalRead(rotA);
    
}

void loop() 
{
  posA = digitalRead(rotA);
  
  if(rotA != posB)
  {
    noTone(buzzer);
    if(digitalRead(rotB) != posA)
    {
      speedLimit = speedLimit + 5;
      tone(buzzer,349);
      delay(6);
      noTone(buzzer);
    }
    else
    {
      speedLimit = speedLimit - 5;
      tone(buzzer,330);
      delay(6);
      noTone(buzzer);
    }
    if(speedLimit < 0)
    {
      speedLimit = 0;
      tone(buzzer,440);
      delay(6);
      noTone(buzzer);
    }
    else if(speedLimit > 120)
    {
      speedLimit = 120;
      tone(buzzer,440);
      delay(6);
      noTone(buzzer);
    }
    noTone(buzzer);
     Serial.print("Speed Limit: ");
     Serial.println(speedLimit);
     Serial.println("kmph");

  }
  
  posB = posA;
  
  digitalWrite(trigPin1, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin1, LOW);

  duration1 = pulseIn(echoPin1, HIGH);
  distance1 = (duration1*.0343)/2;
  Serial.print("Distance: ");
  Serial.println(distance1);
  delay(100);
  
  digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin2, LOW);

  duration2 = pulseIn(echoPin2, HIGH);
  distance2 = (duration2*.0343)/2;
  Serial.print("Distance: ");
  Serial.println(distance2);
  delay(100);
  
  noTone(buzzer);
  if(distance1 < 180)
  {
    tone(buzzer,261);
    delay(300);
    noTone(buzzer);
    digitalWrite(led, HIGH);  // turn the LED on (HIGH is the voltage level)
    delay(300);                      // wait for a second
    digitalWrite(led, LOW);   // turn the LED off by making the voltage LOW
    delay(300); 
    digitalWrite(led, HIGH);  // turn the LED on (HIGH is the voltage level)
    delay(300);                      // wait for a second
    digitalWrite(led, LOW);   // turn the LED off by making the voltage LOW
    delay(300); 
    
    
    time1 = millis();
    delay(1000);     //we might have to remove this if the second sensor is not getting triggered
  }
  if(distance2 < 180)
  {
      tone(buzzer,392);
    delay(300);
    noTone(buzzer);
    digitalWrite(led, HIGH);  // turn the LED on (HIGH is the voltage level)
    delay(300);                      // wait for a second
    digitalWrite(led, LOW);   // turn the LED off by making the voltage LOW
    delay(300); 
    digitalWrite(led, HIGH);  // turn the LED on (HIGH is the voltage level)
    delay(300);                      // wait for a second
    digitalWrite(led, LOW);   // turn the LED off by making the voltage LOW
    delay(300); 
    
    time2 = millis();
    delay(1000);
  }
  
  if(time2>time1)
  {
      delT = time2 - time1;
      delT = delT*3600000; //converting it to seconds
      speed = distance/delT;
      Serial.print("Speed: ");
      Serial.pintln(speed);
  }
  else
  {
      delT = time1 - time2;
      delT = delT*3600000; //converting it to seconds
      speed = distance/delT;
      Serial.print("Speed: ");
      Serial.pintln(speed);
  }

  
  if(reset==LOW)
  if(speed>speedLimit)
  {
    tone(buzzer,261);
    delay(300);
    tone(buzzer,392);
    delay(300);
    digitalWrite(led, HIGH);  // turn the LED on (HIGH is the voltage level)
    delay(300);                      // wait for a second
    digitalWrite(led, LOW);   // turn the LED off by making the voltage LOW
    delay(300);   
    
  }
  else
  {
    noTone(buzzer);
    digitalWrite(led, LOW)
  }
  
  speed = ballSpeed
  
  Serial.print("Speed Limit: ");
  Serial.println(speedLimit);
  Serial.print("Speed: ");
  Serial.println(ballSpeed);


}

The Problem I have is, The HC-SR04 keeps on calculating the distance even after both the sensor have gathered the required information, and they keep populating the serial monitor.

What I want is,

 noTone(buzzer);
  if(distance1 < 180)
  {
    tone(buzzer,261);
    delay(300);
    noTone(buzzer);
    digitalWrite(led, HIGH);  // turn the LED on (HIGH is the voltage level)
    delay(300);                      // wait for a second
    digitalWrite(led, LOW);   // turn the LED off by making the voltage LOW
    delay(300); 
    digitalWrite(led, HIGH);  // turn the LED on (HIGH is the voltage level)
    delay(300);                      // wait for a second
    digitalWrite(led, LOW);   // turn the LED off by making the voltage LOW
    delay(300); 
    
    
    time1 = millis();
    delay(1000);     //we might have to remove this if the second sensor is not getting triggered
  }
  if(distance2 < 180)
  {
      tone(buzzer,392);
    delay(300);
    noTone(buzzer);
    digitalWrite(led, HIGH);  // turn the LED on (HIGH is the voltage level)
    delay(300);                      // wait for a second
    digitalWrite(led, LOW);   // turn the LED off by making the voltage LOW
    delay(300); 
    digitalWrite(led, HIGH);  // turn the LED on (HIGH is the voltage level)
    delay(300);                      // wait for a second
    digitalWrite(led, LOW);   // turn the LED off by making the voltage LOW
    delay(300); 
    
    time2 = millis();
    delay(1000);
  }

After this part of the code has been executed successfully,
I want the result to be sent to the I2C Display
and I want the result to be on the I2C display, until I press the start button (Physical push button, not the one on the Arduino)
I do not know what logic or function I should be using

Well please read again How to get the best out of this forum and post accordingly

➜ Edit your post above by clicking the :pencil2: below you post and add code tags around your code.

It should look like this:

// your code is here
void setup() {
  •••
}

void loop() {
  •••
}

(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

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