Hi everyone, I currently have a project which requires me to use LIDAR sensor to detect distance from an obstacle and using Arduino GPS module NEO-6M module to give output according to the speed its traveling to the obstacle. Basically it means that it will give out different output based on its speed and distance.
This coding that i've tried is that whenever the obstacle is within 200 to 300 cm, it will calculate the height of the obstacle, and when the height is more then 4cm and the object is travelling more than 1kmph, the buzzer will give out an output.
Problem about this coding is that no matter what i tried, in the beginning if the criteria is not fulfilled it will not buzz, but once it achieves it will buzz, but when the condition is not fulfilled it will not stop buzzing, basically means once it buzzes it wont stop, i feel like its a problem with looping,i've tried the buzzer with lidar and buzzer with speed separately it will buzz according to real time condition but once i tried combining it wont work, can anyone give an explanation and give me some ideas? i thank everyone ahead for your time, the coding are as shown:
#include <TinyGPS++.h> // Include TinyGPS++ library
#include <SoftwareSerial.h> // Include software serial library
#include <Wire.h>
#include <LIDARLite.h>
TinyGPSPlus gps;
#define S_RX 4 // Define software serial RX pin
#define S_TX 3 // Define software serial TX pin
const int buzzer = 7;//the pin of the active buzzer
int safetyDistance;
SoftwareSerial SoftSerial(S_RX, S_TX); // Configure SoftSerial library
LIDARLite lidarLite;
int cal_cnt = 0;
void setup(void) {
Serial.begin(9600);
SoftSerial.begin(9600);
pinMode(buzzer,OUTPUT);//initialize the buzzer pin as an output
lidarLite.begin(0, true); // Set configuration to default and I2C to 400 kHz
lidarLite.configure(0); // Change this number to try out alternate configurations
}
void loop() {
//Starting GPS module
while (SoftSerial.available() > 0) {
if (gps.encode(SoftSerial.read())) {
if (gps.speed.isValid()) {
Serial.print("Speed = ");
Serial.print(gps.speed.kmph());
Serial.println(" kmph");
}
else
Serial.println("Speed Invalid");
int dist;
// At the beginning of every 100 readings,
// take a measurement with receiver bias correction
if ( cal_cnt == 0 ) {
dist = lidarLite.distance(); // With bias correction
} else {
dist = lidarLite.distance(false); // Without bias correction
}
// Increment reading counter
cal_cnt++;
cal_cnt = cal_cnt % 100;
// Display distance
Serial.println("distance =");
Serial.print(dist);
Serial.println(" cm");
delay(10);
//Safety dstance running
safetyDistance=dist;
if (safetyDistance<300 && safetyDistance>200){
int x= 300- dist;
double height = x* cos (1.267109037);
//Display height
Serial.println ("height = ");
Serial.print (height);
Serial.println (" cm");
if (gps.speed.kmph()>1 && height>4){
digitalWrite(buzzer,HIGH);
}
else
digitalWrite(buzzer,LOW);
}
}
}
}