-5

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;
}
Bryn McKerracher
  • 683
  • 6
  • 21

3 Answers3

2

You are using the function sum before you have declared it. You could either move the function sum above the use, or you could forward declare it:

int sum();  // Forward declared
int number = 0;

int a() {
 for(int i = 0; i < 20; i++) {
  if((i % 2) == 1) {
   number = sum();
  }  
 }
 return number;
}

int sum() {
 number = number + 100;
 return number;
}

Further explanation of the this problem, can be found elsewhere on Stack Overflow, such as the answers to this question: Why do functions need to be declared before they are used?

Note: I never thoroughly tested your code, which I guess you never did either, so as LogicStuff pointed out it didn't even compile, I've made a few changes to make the code compile, as few changes as possible so that the original code should still be visible from it. Thanks for pointing it out LogicStuff.

Community
  • 1
  • 1
Tommy Andersen
  • 7,165
  • 1
  • 31
  • 50
2

I suppose your problem is that you have a compilation error. You use sum in place where it is not yet visible. Either move sum above a(), or forward declare it with int sum(); above a().

Another problem is that:

if(i % 2 = 1) {

should be:

if((i % 2) == 1) {
           ^~~~~ !!

Whoo, I found third problem :)

You try to use variable number inside int sum() which is declared inside of a(), you simply cannot do it. You should pass number to sum by reference (no need for return value, you return it in number parameter):

void sum(int& number) {

and call it:

sum(number);  // this is in place of `number = sum();`

in a()

marcinj
  • 48,511
  • 9
  • 79
  • 100
  • I propose changing the `sum` signature (and show OP the implementation), so it either just returns the new value, or modifies the argument. While doing both compiles with the implementation OP currently has, it's weird and it could all be as well just one `+=`. – LogicStuff Dec 25 '15 at 22:50
  • @LogicStuff fixed it, each time I look at this code I find something new – marcinj Dec 25 '15 at 22:59
  • Hi, I wrote it in a hurry...if you want my real code, please write me your email :) :) – Timco Vanco Dec 25 '15 at 23:30
  • @MarcinJędrzejewski please write me your email, I will send you my full real code :) – Timco Vanco Dec 25 '15 at 23:32
  • @TimcoVanco why dont you want to post your Full Real Code to SO? – marcinj Dec 25 '15 at 23:33
  • @MarcinJędrzejewski Because it does not allow me to do that, I can not post new question for 7 days... :( – Timco Vanco Dec 25 '15 at 23:36
  • @MarcinJędrzejewski Check my code above, I have edited it :) now it is my real code – Timco Vanco Dec 26 '15 at 00:03
0

There are a number of errors in your code which will cause compiler errors:

If you're using the sum() function in int a(), then you need to forward-declare it.

int sum();

int add() {
    //
}

int sum() {
    //
}

All sum() really does however, is add 100 to a variable. This can be incorporated into the add() function very easily via the += operator, meaning your code is equivalent to this:

int a() {
    int number = 0;        //Added a necessary ';'
    for(int i = 0; i < 20; i++) { 
        if(i % 2 == 1) {   //Corrected this from if(i % 2 = 1)
            number += 100; //No need for the sum() function
        }  
    }
    return number;
}

The crucial aspect:

There are a lot of additional optimizations you could perform on your setup; Essentially, int a() could be simplified to

int a() {
    return 1000;
} 

So, you could just as easily not have a() be a function at all:

#define a() 1000

Or, (probably better):

const int a = 1000;

EDIT : For your updated code you need to write int zmeratSonar28(); before int zmeratSonar1() starts:

int zmeratSonar28();

int zmeratSonar1() {
    //Sonar code stuff
}

int zmeratSonar28() {
    //Other sonar code stuff
}
Bryn McKerracher
  • 683
  • 6
  • 21