////Sonar Sensor Pin///////
const int trigPin = 24; //
const int echoPin = 22; //
//////////////////////////////
///////Other Variables///////
int distance_cm = 0; //
///////////////////////////////
void setup() {
Serial.begin(9600);
}
void loop() {
distance_cm = distance();
SonarSensor();
Serial.println(distance_cm);
}
long distance () {
long duration = 0;
long testing = 0;
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
testing = duration / 29 / 2;
if(testing > 3000) testing = 50;
return testing;
}
void SonarSensor() {
if(distance_cm < 15 && distance_cm != 50) {
Serial.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
delay(3000);
Serial.println("NONONONONONNONONONONONONONONONONONONONOONONONONONONONO");
}
}
This is my code for my robot and as you can see, I have defined anything above 3000cm to 50cm. (in distances function) I have done this to see if the SonarSensor function still kicks in when it's not meant to be. (When distance value showed 3600 or near that, the SonarSensor function just started itself. However, after defining the value differently, it's still doing that kind of thing...
Please help me with this problem...
Thank you