End a function?

As you can imagine this function doesn’t “toggle” correctly. It turns the led off and back on. I need to know how to end the function (where i’ve put return;).

 int toggle(int pinnumber){
  if(digitalRead(pinnumber) == HIGH){
    Serial.print("toggle functions says ");
    Serial.print(pinnumber);
    Serial.print(" is on");
    Serial.println();
    // if light is on turn it off
    digitalWrite(pinnumber, LOW);
    return;
  } 
  if(digitalRead(pinnumber) == LOW){
    Serial.print("toggle functions says ");
    Serial.print(pinnumber);
    Serial.print(" is off");
    Serial.println();
    // if light is off turn it on
    digitalWrite(pinnumber, HIGH);
    return;
  }
}

The problem is after you're setting the pin LOW, you're still checking if it's LOW before setting it HIGH again. You want to use an "else if" statement so the second "pin is low" block doesn't execute after you have set the pin LOW in the first block.

 int toggle(int pinnumber){
  if(digitalRead(pinnumber) == HIGH){
    Serial.print("toggle functions says ");
    Serial.print(pinnumber);
    Serial.print(" is on");
    Serial.println();
    // if light is on turn it off
    digitalWrite(pinnumber, LOW);
    return;
  }
  [glow]else[/glow] if(digitalRead(pinnumber) == LOW){
    Serial.print("toggle functions says ");
    Serial.print(pinnumber);
    Serial.print(" is off");
    Serial.println();
    // if light is off turn it on
    digitalWrite(pinnumber, HIGH);
    return;
  }
}

Also, your function is defined as returning an int, but your return statements don't return anything. Either you set the return type to void, or you return something.

The problem is after you're setting the pin LOW, you're still checking if it's LOW before setting it HIGH again

I don't see how an extra else there would make any difference. There is after all a "return" at the end of both if-statements.

Aren't you supposed to decide once and for all whether a pin is input or output with pinMode() and stick to that?

I don't see how an extra else there would make any difference. There is after all a "return" at the end of both if-statements.

Hmm, that's true. Other than not returning an int (which the compiler will happily ignore) I'm not sure why it doesn't work then.

Aren't you supposed to decide once and for all whether a pin is input or output with pinMode() and stick to that?

When a pin is set to an output, digitalRead gives you the current output state, rather than reading the input.

When a pin is set to an output, digitalRead gives you the current output state, rather than reading the input.

So, tehjrow, what is the pinMode of pinnumber?

It was running both functions. I just didn't know how to use return.

return(0);

seems to work.

Ah, ok. To use the return statement you just enter return, followed by the value you wish to return. It’s not a function, more of a key word, so in your example you don’t need the brackets:

return 0;

The reason return(0); works is because it is treating “(0)” as a mathematical expression. You can use this to perform arithmetic before returning a value, eg.

int add(int a, int b)
{
    return a+b;
}