Program Runs Twice then Freezes

Hi there,

i have a function as part of a really small program. The program runs fine twice then the third time it freezes. I have a function called mathFunc which take a value in and sets a string based on the input. If i comment out the function from the main program then it runs fine, but without fail every time i comment out this function it runs twice then stops. Any thoughts??

String MathFunc(int FSRval)
{
if (FSRval <= 500)
{
eCueValueRet = "S1";
delay(50);
}
else if (FSRval > 500 && FSRval <= 600)
{
eCueValueRet = "S2";
delay(50);
}
else if (FSRval > 600 && FSRval <= 650)
{
eCueValueRet = "S3";
delay(50);
}
else if (FSRval > 650 && FSRval <= 700)
{
eCueValueRet = "S4";
delay(50);
}
else if (FSRval > 700 && FSRval <= 750)
{
eCueValueRet = "S5";
delay(50);
}
else if (FSRval > 750 && FSRval <= 800)
{
eCueValueRet = "S6";
delay(50);
}
else if (FSRval > 800 && FSRval <= 850)
{
eCueValueRet = "S7";
delay(50);
}
else if (FSRval > 850 && FSRval <= 900)
{
eCueValueRet = "S8";
delay(50);
}
else if (FSRval > 900 && FSRval <= 950)
{
eCueValueRet = "S9";
delay(50);
}
else if (FSRval > 950 && FSRval <= 1000)
{
eCueValueRet = "S10";
delay(50);
}
else if (FSRval > 1000 && FSRval <= 1050)
{
eCueValueRet = "S11";
delay(50);
}
else
{
eCueValueRet = "S0";
delay(50);
}

}

i can not catch your problem without full code.

i have a function as part of a really small program.

Please read this before posting a programming question
then post the whole program.

You say the function returns a String (ack, pfft!) but you don't return anything. If you are using the global String (I assume) eCueValueRet as a return, the function should be declared to return a void.

Sorry you are right, I do just set a value against a global string, not returning. If i change what i set to a char rather than the string it runs fine and set the char every time. Sorry ill check if it runs ok as void.

Thanks for pointing out my obvious error. void works and the program runs fine. Thanks for the answers and sorry for wasting your time.

Take advantage of integer arithmetic truncation, use an array.

// start from 500
String[] values = {
  "S1", "S1", "S2", "S3", "S4", "S5",
  "S6", "S7", "S8", "S9", "S10", "S11"
};

String MathFunc(int FSRval)
{ 
  if (FSRval <= 500)
  {
    eCueValueRet = "S1";
  }
  else if (FSRval > 1050) {
    eCueValueRet = "S0";
  }
  else {
    eCueValueRet = values[ (FSRval-501)/50];
  }

  delay(50);   
}