I wrote this code and I don't know what to do to change that. This code I want to use in my Raspberry pi with sonars - hc sr 04 to measure a distance. Please do you know how to fix my code? :) before this I wrote a code for-example. This is my real code. So please check it again :) thanks!
int zmeratSonar1() {
int smer = 0;
printf("meram sonar1");
digitalWrite(TRIGsonar1, HIGH);
delayMicroseconds(20);
digitalWrite(TRIGsonar1, LOW);
while (digitalRead(ECHOsonar1)==LOW);
long zaciatok = micros();
while (digitalRead(ECHOsonar1)==HIGH);
long cas = micros() - zaciatok;
int vzdialenost = cas/58;
if(vzdialenost < 100) {
smer = zmeratSonar28(); // <----here is my problem
}
else if(vzdialenost > 100) {
zmeratSonar1();
}
return smer;
}
int zmeratSonar28(){
int smer = 0;
printf("meram sonare 2 a 8");
//------------SONAR 2---------------------
digitalWrite(TRIGsonar2, HIGH);
delayMicroseconds(20);
digitalWrite(TRIGsonar2, LOW);
while (digitalRead(ECHOsonar2)==LOW);
long startTime2 = micros();
while (digitalRead(ECHOsonar2)==HIGH);
long travelTime2 = micros() - startTime2;
int distance2 = travelTime2/58;
//------------SONAR 8----------------------
digitalWrite(TRIGsonar8, HIGH);
delayMicroseconds(20);
digitalWrite(TRIGsonar8, LOW);
while (digitalRead(ECHOsonar8)==LOW);
long startTime8 = micros();
while (digitalRead(ECHOsonar8)==HIGH);
long travelTime8 = micros() - startTime8;
int distance8 = travelTime8/58;
//porovnanie vzdialenosti
if(distance2 > 100 || distance8 > 100) {
if(distance2 > distance8) {
smer = 2;
}
else {
smer = 8;
}
}
else{
smer = 0;
}
return smer;
}