I have a function that I want to know if a value is saved correctly so I declared it as boolean and after write the value and read that is correct, I set a return = true for this function:
boolean LinWriteRead (byte CompresorID, byte IDSpeedSettingHb, byte IDSpeedSettingLb, byte ValueH, byte ValueL ){
contar2 ++;
if (contar2 == 1) LINwriteEnable = true;
else if (contar2 == 2) LINwriteEnable = false;
else if (contar2 > 2) contar2 = 0;
if (LINwriteEnable)LINwriteRegister(0x01,IDSpeedSettingHb,IDSpeedSettingLb,ValueH,ValueL);
else if (contar2 ==2)LINreadRegister(0x01,IDSpeedSettingHb,IDSpeedSettingLb);
if ( contar2 ==2 && valorH == ValueH && valorL == ValueL) {
mySerial.println ("VALUE SAVED");
valorH = 0x00;
valorL = 0x00;
return = true;
}
else if (contar2 ==2) {
error ++;
}
else if (error ==5) {
mySerial.println ("ERROR LIN WIRE NO CONECTED");
error=0;
return = false;
}
// delay (100);
}
Now, how can I call the function and after run, take the return value? And how can I try again to write the value, if the function return false?
@johnwasser, yes sorry, I typed wrong after many changes in the Code. Its without equal simbol. The problem is that I don't know how to call the function and get the return, I tried similar code recommended by @J-M-L
With your example line, the function is called and its return value is used as the condition of the if statement
If your function returned true, then the code block of this if statement will not be executed, because the result of the condition is false ( ! mean NOT so if (!true) is the same as if (false) )
You realise that the exclamation mark (bang) '!' before the function call means 'not', right ?
so it's something like this
if (functionCall is not true)
Serial.print("function call was false, so this gets printed");
if function is false, call again the function until get true
if you want to do this and have an active wait, you need to use a loop statement of some sort. for example
// Blocking active wait
while (! functionCall(x,y,z)) { // as long as functionCall returns false
yield(); // do nothing
}
yield() might be needed on some Arduino (like ESP) where the watchdog could bite you if you wait too long in that loop and do not give some processor time to some other stuff. using yield(); on AVR won't hurt, it will just do nothing.