I cobbled the below together which without the "void test1" section works fine. At the moment when i push the button the buzzer will sound continuously until the button is released. Im trying to get the buzzer to sound only when the distance exceeds say 10 CM. I though of setting "void test" where i could define IF / ELSE statements for the measured distance. I think i need to somehow make the "distance" a variable ? am i on the right lines below:-
#define trigPin 13 //Sensor Echo pin connected to Arduino pin 13
#define echoPin 12 //Sensor Trip pin connected to Arduino pin 12
int buttoninput = 7;
int buttonstate = 0;
int led1 = 8;
const int buzzer = 6;
#include <LiquidCrystal.h> //Load Liquid Crystal Library
LiquidCrystal LCD(11,10,9,2,3,4,5);
//Simple program just for testing the HC-SR04 Ultrasonic Sensor with LCD dispaly
//URL:
void setup()
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buttoninput, INPUT_PULLUP);
digitalWrite(buttoninput, HIGH);
pinMode(buzzer, OUTPUT);
int distance = 0;
int calibrationVal = 0;
LCD.begin(16,2); //Tell Arduino to start your 16 column 2 row LCD
LCD.setCursor(0,0); //Set LCD cursor to upper left corner, column 0, row 0
LCD.print("Andys Distance:"); //Print Message on First Row
digitalWrite(led1, LOW);
pinMode(buzzer, OUTPUT);
//Serial.begin(9600);
distance = (distance);
}
void test1(); {
if (distance > == 10);
tone(buzzer, 1000);
} else {
noTone(buzzer);
}
void loop() {
if (digitalRead(buttoninput) == LOW) {
long duration, distance;
digitalWrite(led1, HIGH);
tone(buzzer, 1000);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
LCD.setCursor(0,1); //Set cursor to first column of second row
LCD.print(" "); //Print blanks to clear the row
LCD.setCursor(0,1); //Set Cursor again to first column of second row
LCD.print(distance); //Print measured distance
LCD.print(" cm"); //Print your units.
delay(250); //pause to let things settle
digitalWrite(led1, LOW);
noTone(buzzer);
} }
Unless I'm missing it, you aren't calling test1() from either setup() or loop().
Only setup() and loop() don't need the author to call them; that's done behind the scenes. All other functions, like your test1() need to be called from somewhere, eg:
i dont think i am getting that far, when i compile it flags an error at the "void test1()" point as below, i changed the code to call "test1" just after the distance calc:-
Arduino: 1.6.5 (Mac OS X), Board: "Arduino/Genuino Uno"
distance_lcd_buzzerv2:34: error: expected unqualified-id before '{' token
distance_lcd_buzzerv2:39: error: expected unqualified-id before 'else'
expected unqualified-id before '{' token
This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.
#define trigPin 13 //Sensor Echo pin connected to Arduino pin 13
#define echoPin 12 //Sensor Trip pin connected to Arduino pin 12
int buttoninput = 7;
int buttonstate = 0;
int led1 = 8;
const int buzzer = 6;
#include <LiquidCrystal.h> //Load Liquid Crystal Library
LiquidCrystal LCD(11,10,9,2,3,4,5);
//Simple program just for testing the HC-SR04 Ultrasonic Sensor with LCD dispaly
//URL:
void setup()
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buttoninput, INPUT_PULLUP);
digitalWrite(buttoninput, HIGH);
pinMode(buzzer, OUTPUT);
int distance = 0;
int calibrationVal = 0;
LCD.begin(16,2); //Tell Arduino to start your 16 column 2 row LCD
LCD.setCursor(0,0); //Set LCD cursor to upper left corner, column 0, row 0
LCD.print("Andys Distance:"); //Print Message on First Row
digitalWrite(led1, LOW);
pinMode(buzzer, OUTPUT);
//Serial.begin(9600);
}
void test1(); {
if (distance > = 10);
tone(buzzer, 1000);
delay(2500);
noTone(buzzer);
} else {
noTone(buzzer);
}
void loop() {
if (digitalRead(buttoninput) == LOW) {
long duration, distance;
digitalWrite(led1, HIGH);
//tone(buzzer, 1000);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
test1();
LCD.setCursor(0,1); //Set cursor to first column of second row
LCD.print(" "); //Print blanks to clear the row
LCD.setCursor(0,1); //Set Cursor again to first column of second row
LCD.print(distance); //Print measured distance
LCD.print(" cm"); //Print your units.
delay(250); //pause to let things settle
digitalWrite(led1, LOW);
//noTone(buzzer);
} }
I think there needs to be a remedial "forum" I have looked at quite a few examples but seem to be getting more confused.
Does anyone have a simple example of reading a calculated value as in this case "distance" then making an led or buzzer sound if the value is greater than X