ultrasonic sensor code problem

hello, I'm working on a program for an ultrasonic sensor where when there is minimum fluctuations of distance an alarm will sound. the problem im getting is that I'm not sure how to write a program for this operation. any help will be greatly appreciated!!!!!!!!!!!!!!

flanhi:
hello, I'm working on a program for an ultrasonic sensor where when there is minimum fluctuations of distance an alarm will sound. the problem im getting is that I'm not sure how to write a program for this operation. any help will be greatly appreciated!!!!!!!!!!!!!!

Welcome to the Arduino forum, but it's a little hard to help when you supply nothing to work with. What ultrasonic sensor? A data sheet link? What have you tried? Do any of the many programs using an ultrasonic sensor not work for you?

What is the alarm you mention? Is it mechanical, electrical , or software alarm? Does it work?

Where is the program you are working on? And what are you having problems with?

Paul

Paul_KD7HB:
Welcome to the Arduino forum, but it’s a little hard to help when you supply nothing to work with. What ultrasonic sensor? A data sheet link? What have you tried? Do any of the many programs using an ultrasonic sensor not work for you?

What is the alarm you mention? Is it mechanical, electrical , or software alarm? Does it work?

Where is the program you are working on? And what are you having problems with?

Paul

so the ultra sonic sensor im using is HC-SR04, the ‘alarm’ is just a buzzer that came with the kit and a LED … the program i’ve written will be shown below.

Code: [Select]

// defines pins numbers
const int buzzer = 8;
const int trigPin = 12;
const int echoPin = A1; //input only pin
const int LED_RED = 7;
const int LED_GREEN = 13;

// defines variables
long duration;
int distance;

//LED
#include <LiquidCrystal.h>
LiquidCrystal LCD(11,10,9,2,3,4,5); // display pin loactions

void setup() {
pinMode(buzzer, OUTPUT); // sets buzzer as output
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(LED_RED, OUTPUT); // sets red led as output
pinMode(LED_GREEN, OUTPUT); // sets green led as output
LCD.begin(16,2);
LCD.setCursor(0,0);
LCD.print(" ");

Serial.begin(9600); // Starts the serial communication
}

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance in mm
distance = ((duration/2.9)/2);

// Prints the distance on the Serial Monitor
LCD.setCursor(0,1);
LCD.print(" ");
delay(100);

//if statement to set parameters for specific operations to occur
if (distance >= 70 && distance <= 90 )// this line is just here to ensure the if statement works, but im trying to replace this line
{
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_RED, HIGH);
digitalWrite(buzzer, HIGH);
tone(buzzer, 1000);
delay(1000);
noTone(buzzer);

LCD.print(“EMERGENCY”);
delay(1000);

} else {
digitalWrite(buzzer,LOW);
digitalWrite(LED_RED,LOW);
digitalWrite(LED_GREEN,HIGH);
LCD.setCursor(0,1);
LCD.print(" BREATHING ");

}

}

the ‘if’ statement is the problem. i want to run that ‘if’ statement like…

if there is minimum change in distance;
buzzer and red led are high
green led is low…

if u still need more information let me know. i also tried using matlab to solve this but im getting problems with the arduino connection even after installing the arduino hardware package.

You mean doing something like having a variable that holds the distance measured last time to compare to this time? And then you save the distance this time as last time and when you ping again calculate the new change?

Much depends on what you mean by "there is minimum change in distance".

From the sketch it appears that you want to detect a stopping of breathing. I think to do that you will want to calculate a running average of the distance and reset a timer when the current distance exceeds some specific distance above or below the average. If the timer reaches some time limit (no reset in 30 seconds, for example) you sound the alarm.

Lets take care of the easy stuff first. You have this code for your buzzer:

digitalWrite(buzzer, HIGH);
tone(buzzer, 1000);
delay(1000);
noTone(buzzer);

That code is for both a passive and an active buzzer. One buzzer can't be both. Does your buzzer work if you take out the "tone" lines? If yes, you have an active buzzer and take out the "tone" lines.

If no, then take out the digitalWrite(buzzer, HIGH); and the matching LOW line.

Paul

There's a chance that code will run either type of buzzer?

GoForSmoke:
There's a chance that code will run either type of buzzer?

Possibly, but that is not an appropriate way to discover what works!

Paul

GoForSmoke:
You mean doing something like having a variable that holds the distance measured last time to compare to this time? And then you save the distance this time as last time and when you ping again calculate the new change?

Thank you everyone!!!
I ended up going with this method and it’s working. however, i encountered another issue. when the program compares the previous distance to the new distance, i set an ‘if’ statement;

if (if (difference <=3 && difference >= 0 || difference>=-3 && difference <=0)

all measurements are converted tomm before this if statement.

the problem im getting is when the difference happens to fall within either range, the buzzer sounds immediately.
what im trying to figure out now is how can i allow it to read the difference for about 5 iterations before the buzzer sounds.

again, thank you for the help. Any and all assistance is appreciated!!!

if (if ( Why the double "if"?
Where is the closing ')'?

if (if (difference <=3 && difference >= 0 || difference>=-3 && difference <=0)

if (expression) // where expression evaluates to zero/false or non-zero/true

like this
if ( ((difference <=3) && (difference >= 0)) || ((difference>=-3) && (difference <=0)) )

or this which is different and not what you want, just showing re-arrangement
if ( (difference <=3) && ((difference >= 0) || (difference>=-3)) && (difference <=0) )

The language is C, express your literacy.

Tip: The Arduino IDE Autoformat tool (ctrl-T) catches unbalanced parens and braces.