Simple LED toggle function won't toggle!

Hi, Hope someone can help me on this..... I'm trying to toggle an LED. I have written a function, but all my program does now is light the LED when the button(pin 8) is pressed and held. I can't see what I have done wrong. Can you help??

Thanks if you can!

Chris.

int paddockPin = 8; int paddockLedPin1 = 13; boolean currentButtonState = LOW; boolean paddockGateState = LOW; boolean ledPin1State = false;

void setup() { pinMode(paddockPin, INPUT); pinMode(paddockLedPin1, OUTPUT); }

boolean alarm(boolean alarmState, int alarmPin, boolean alarmLedState, int alarmLed) { boolean currentButtonState = LOW; currentButtonState = debounce(alarmState, alarmPin); if(alarmState == LOW && currentButtonState == HIGH) { alarmLedState = !alarmLedState; alarmState = HIGH; } alarmState = currentButtonState; digitalWrite(alarmLed, alarmLedState); return alarmState, alarmLedState; }

boolean debounce(boolean last, int pin) { boolean current = digitalRead(pin); if(last != current) { delay(5); current = digitalRead(pin); } return current; }

void loop() { alarm(paddockGateState, paddockPin, paddockGateState, paddockLedPin1); }

boolean alarm(boolean alarmState, int alarmPin, boolean alarmLedState, int alarmLed)
{
  boolean currentButtonState = LOW;
  currentButtonState = debounce(alarmState, alarmPin);
  if(alarmState == LOW && currentButtonState == HIGH)
  {
    alarmLedState = !alarmLedState;
    alarmState = HIGH;
  }
  alarmState = currentButtonState;
  digitalWrite(alarmLed, alarmLedState);
  return alarmState, alarmLedState;
}

I assume that is the function you are asking about.

The answer is simple. You cannot return multiple items.

If you need to return more than one item then you should either consider returning a “struct” of items, or using pointers for passing the items so they can be modified in place:

boolean alarm(boolean alarmState, int alarmPin, boolean *alarmLedState, int alarmLed)
{
  boolean currentButtonState = LOW;
  currentButtonState = debounce(alarmState, alarmPin);
  if(alarmState == LOW && currentButtonState == HIGH)
  {
    *alarmLedState = !(*alarmLedState);
    alarmState = HIGH;
  }
  alarmState = currentButtonState;
  digitalWrite(alarmLed, *alarmLedState);
  return alarmState;
}

Note the inclusion of the * infront of all alarmLedState variables.

This has the effect of working on the address at which the variable is located in memory, instead of the contents of the variable itself. You pass the address of the variable to the function, and the function modifies the content, rather than passing a copy of the content and hoping to return a new value.

To call it you would use something like:

newState = alarm(currentAlarmState, fireAlarmPin, &fireAlarmLedState, fireAlarmLed);

Note the & in front of the fireAlarmLedState variable. This causes it to pass the address of the variable, not the content (it converts a boolean into a boolean *, or an int into an int *, etc).

Thanks for the tips. As you can probably guess, I'm new to programming, so your tips have been a great help- thanks! :)

Wrighty,

Are we trying to do the same thing here?

http://arduino.cc/forum/index.php/topic,119824.0.html

Ian

I think we are trying to do similar things. My main issue was that I had the system working just fine rewriting the code for each of my sensor inputs. I just wanted to try and understand how to convert my routine into a single function which gets called for each of the sensors- basically eradicating duplicate code. Think I'm almost there. :grin: