I want to return a function whether it is true or false. If it's true run this code, else do not run this code.
void loop()
{
if(irrecv.decode(&results))
{
//If its true run this code
resultValues();
irrecv.resume();
}
}
void resultValues()
{
switch(results.value)
{
case 1886400719:
if(isOn)
{
Serial.println("On");
isOff = true;
isOn = false;
returnTrue();
}
else if(isOff){
Serial.println("Off");
isOn = true;
isOff = false;
returnFalse();
}
break;
How to I pass the value of isOff and isOn on the void loop? Thanks
dev_1
March 31, 2017, 1:29pm
2
Indenting your code is crucial. Either get into the habit, use a programming editor to help you, or press control-T in the IDE editor.
Without the complete program, I can't provide a complete answer. Here's a guess, which might be a waste of my time if I'm guessing wrong:
void loop()
{
if (irrecv.decode( &results ))
{
resultValues( results ); // return value not used?
irrecv.resume();
}
}
bool resultValues( ResultsTypeNameHere & results )
{
switch(results.value) {
case 1886400719:
if(isOn) {
Serial.println( F("On") ); // F macro saves RAM
isOn = false;
isOff = true;
return true; // but not tested by calller...
} else if (isOff) {
Serial.println("Off");
isOn = true;
isOff = false;
return false;
}
break;
If you want to return a value from your function, you put the return type before the function name (instead of void).
But I have to ask, are isOn and isOff always opposite of each other? If they are, you should only use one of them. Using isOn reads the nicest:
void loop()
{
if (irrecv.decode( &results ))
{
resultValues( results );
irrecv.resume();
}
}
bool resultValues( ResultsTypeNameHere & results )
{
switch(results.value) {
case 1886400719:
Serial.println( isOn ? F("On") : F("Off") );
isOn = !isOn;
return !isOn; // really? return "is off"?
The ternary operator ? and : is a little if-then-else statement that lets you write one print statement instead of 5 lines for this simple test. I do not recommend it for if tests that are more complicated.
The exclamation mark is the logical NOT operator (i.e., invert or negate). It's used to toggle isOn.
Cheers,
/dev