Trouble with function calling

Hi all

could any one help me with this sketch , it doesn’t work normally , the sketch has a function takes five parameters and return a float one , when I try to call the function , on the serial monitor , just the statement of “FirstState” appeared which is before function call and neither returned value from function and nor statement of “SecondState” appear , so is there any syntax error in the function body ?

const float golx=12;
const float goly=14;
const float n=0.005;
const int dstar=1;
const float qstar=1.5;
const float zeta=0.5;


  
float qx,qy,d1,d2,d3,th,zzz;           
int obs1[2];      // array
int obs2[2];      
int obs3[2];
float gatt [2];
float grep [2]; 
float resultant [2]; 



  
  void setup() {
  // put your setup code here, to run once:
Serial.begin(115200);
}


    
    void loop() {
  // put your main code here, to run repeatedly:
Serial.println("FirstState");
zzz=APFtt(3,4,1,1.4,3);
Serial.println(zzz);
Serial.println("SecondState");

}




float APFtt (float xc,float yc,float sf,float sl,float sr) {
  qx=xc;
  qy=yc;
    float dq ;
    dq = sqrt (pow((qx-golx),2) + pow((qy-goly),2));
    
    if (dq < dstar )
{
       
          gatt [1]= zeta * (qx - golx);
          gatt [2]= zeta * (qy - goly);    
}          
    else     
      
{
          gatt[1] =dstar * zeta *  (qx - golx  )/ dq;
          gatt[2] =dstar * zeta *  (qy - goly  )/ dq;
          
}
   

  d1 = sf;      
  

            if (d1<qstar && d1>0){
                obs1[1] = xc + d1;
                obs1[2] = yc + d1;
              

grep[1] = grep[1] +  (n * (   (1/qstar) - (1/d1)   ) * ((qx - obs1[1])/ pow(d1,2)));
grep[2] = grep[2] +  (n * (   (1/qstar) - (1/d1)   ) * ((qy - obs1[2])/ pow(d1,2))); }
         
  
  d2 = sl;      

            if (d2<qstar && d2>0){
                obs2[1] = xc + d2;
                obs2[2] = yc + d2;
              


grep[1] = grep[1] +  (n * (   (1/qstar) - (1/d2)   ) * ((qx - obs2[1])/ pow(d2,2)));
grep[2] = grep[2] +  (n * (   (1/qstar) - (1/d2)   ) * ((qy - obs2[2])/ pow(d2,2))); }


d3 = sr;      

            if (d3<qstar && d3>0){
                obs3[1] = xc + d3;
                obs3[2] = yc + d3;
              

grep[1] = grep[1] +  (n * (   (1/qstar) - (1/d3)   ) * ((qx - obs3[1])/ pow(d3,2)));
grep[2] = grep[2] +  (n * (   (1/qstar) - (1/d3)   ) * ((qy - obs3[2])/ pow(d3,2))); }



         
         
   // resultant = -1* (gatt + grep); 
   
   resultant[1] = -1* (gatt[1] + grep[1]); 
   resultant[2] = -1* (gatt[2] + grep[2]);  
   
 
  
   th= atan2(resultant[2] , resultant[1]);//%* 180/3.14 ;
return th;
}

Hi Maria88

int obs1[2];      // array
...
obs1[1] = xc + d1;
obs1[2] = yc + d1;

When you declare arrays with two elements, these elements are numbered 0 and 1. In your code, however, you are trying to access elements numbered 1 and 2.

Reading element 2 will give incorrect data. Writing to element 2 will probably corrupt memory and cause strange behaviour.

Try correcting this and see if it helps with the problem.

Regards

Ray

    void loop() {
  // put your main code here, to run repeatedly:
Serial.println("FirstState");
zzz=APFtt(3,4,1,1.4,3);
Serial.println(zzz);
Serial.println("SecondState");

}




float APFtt (float xc,float yc,float sf,float sl,float sr) {
  qx=xc;
  qy=yc;

Name must be the same. You have APFtt and float APFtt

steinie44: ```    void loop() {  // put your main code here, to run repeatedly: Serial.println("FirstState"); zzz=APFtt(3,4,1,1.4,3); Serial.println(zzz); Serial.println("SecondState");

}

float APFtt (float xc,float yc,float sf,float sl,float sr) {  qx=xc;  qy=yc;




Name must be the same.
You have APFtt and float APFtt

No, he did that part right. WHen you declare the function you have to list the return type. That's the float part. But when you call the function you don't say that part. You just use the function name.

The problem is likely related to the array index out of bounds mentioned above.

thanks a lot Hackscribble , steinie44 and Delt_G

It is solved , the problem was in array indexes are numbered 1 and 2 instead of 0 and 1 .

It is solved , the problem was in array indexes are numbered 1 and 2 instead of 0 and 1 .

[EDIT]

If that’s true, why is that after changing the [EDIT] indices I’m still not getting a result for “SecondState” ?
@OP
Can you see may error ?

const float golx=12;
const float goly=14;
const float n=0.005;
const int dstar=1;
const float qstar=1.5;
const float zeta=0.5;


  
float qx,qy,d1,d2,d3,th,zzz;           
int obs1[1];      // array
int obs2[1];      
int obs3[1];
float gatt [1];
float grep [1]; 
float resultant [1]; 



  
  void setup() {
  // put your setup code here, to run once:
Serial.begin(115200);
}


    
    void loop() {
  // put your main code here, to run repeatedly:
Serial.println("FirstState");
zzz=APFtt(3,4,1,1.4,3);
Serial.println(zzz);
Serial.println("SecondState");
delay(2000);
}




float APFtt (float xc,float yc,float sf,float sl,float sr) {
  qx=xc;
  qy=yc;
    float dq ;
    dq = sqrt (pow((qx-golx),2) + pow((qy-goly),2));
    
    if (dq < dstar )
{
       
          gatt [0]= zeta * (qx - golx);
          gatt [1]= zeta * (qy - goly);    
}          
    else     
      
{
          gatt[0] =dstar * zeta *  (qx - golx  )/ dq;
          gatt[1] =dstar * zeta *  (qy - goly  )/ dq;
          
}
   

  d1 = sf;      
  

            if (d1<qstar && d1>0){
                obs1[0] = xc + d1;
                obs1[1] = yc + d1;
              

grep[0] = grep[0] +  (n * (   (1/qstar) - (1/d1)   ) * ((qx - obs1[0])/ pow(d1,2)));
grep[1] = grep[1] +  (n * (   (1/qstar) - (1/d1)   ) * ((qy - obs1[1])/ pow(d1,2))); }
         
  
  d2 = sl;      

            if (d2<qstar && d2>0){
                obs2[0] = xc + d2;
                obs2[1] = yc + d2;
              


grep[0] = grep[0] +  (n * (   (1/qstar) - (1/d2)   ) * ((qx - obs2[0])/ pow(d2,2)));
grep[1] = grep[1] +  (n * (   (1/qstar) - (1/d2)   ) * ((qy - obs2[1])/ pow(d2,2))); }


d3 = sr;      

            if (d3<qstar && d3>0){
                obs3[0] = xc + d3;
                obs3[1] = yc + d3;
              

grep[0] = grep[0] +  (n * (   (1/qstar) - (1/d3)   ) * ((qx - obs3[0])/ pow(d3,2)));
grep[1] = grep[1] +  (n * (   (1/qstar) - (1/d3)   ) * ((qy - obs3[1])/ pow(d3,2))); }



         
         
   // resultant = -1* (gatt + grep); 
   
   resultant[0] = -1* (gatt[0] + grep[0]); 
   resultant[1] = -1* (gatt[1] + grep[1]);  
   
 
  
   th= atan2(resultant[1] , resultant[0]);//%* 180/3.14 ;
return th;

}
int obs1[1];      // array
int obs2[1];      
int obs3[1];
float gatt [1];
float grep [1]; 
float resultant [1];

Except for looking stupid, one element arrays are identical to scalar variables. Why are you using one element arrays?

          gatt [0]= zeta * (qx - golx);
          gatt [1]= zeta * (qy - goly);

Why are you then writing beyond the end of the array?

The value in the brackets in the declaration statement is the number of elements, not the upper index.

@Paul_S,

FYI , I'm not the OP.

I'm just an innocent spectator trying to learn something who got bit by a shark who mistook me for a baby seal. As far the one element array is concerned, I wouldn't even do that, because it makes no sense.

Thanks for the heads up anyway.