Functions

Please excuse my lack of C knowledge. I have done programming before, but it has been primarily matlab. I am stuck in the matlab mindset and can't figure my way around it. I think the best way to communicate this is to give a brief example:

So I have two LEDs. I would like to have either led perform the following function (taken from the function example):

void blinkLED()
{
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}

...

from my matlab experience I would like to make that code work for some input variable:

void blinkLED(a)
{
digitalWrite(a, HIGH);
delay(1000);
digitalWrite(a, LOW);
delay(1000);
}

where "a" would be defined when called upon in the loop, i.e. blinkLED(ledOne);

I can't get this to compile... how do I go about this?

byte a;

void setup ()
{
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);
}

void loop ()
{
  a = 7;
  blinkit();
  a = 8;
  blinkit();
}

void blinkit ()
{
  digitalWrite(a,HIGH);
  delay(1000);
  digitalWrite(a,LOW);
  delay(1000);
}

Or....

void setup ()
{
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);
}

void loop ()
{
  blinkit(7);
  blinkit(8);
}

void blinkit (byte a)
{
  digitalWrite(a,HIGH);
  delay(1000);
  digitalWrite(a,LOW);
  delay(1000);
}

O-I-C

Same thing but a bit more readable.

const uint8_t       pinLED_1    = 7;
const uint8_t       pinLED_2    = 8;
const unsigned long ONE_SECOND  = 1000UL;

void blinkit(uint8_t pin)
{
    digitalWrite(pin, HIGH);
    delay(ONE_SECOND);

    digitalWrite(pin, LOW);
    delay(ONE_SECOND);
}

void loop()
{
    blinkit(pinLED_1);
    blinkit(pinLED_2);
}

void setup()
{
    pinMode(pinLED_1, OUTPUT);
    pinMode(pinLED_2, OUTPUT);
}

cowasaki hit the nail on the head... Thanks for the help guys.

int switchOne = 31;
int switchTwo = 32;
int switchThree = 33;
int switchFour = 34;
int ledOne = 41;
int ledTwo  = 42;
int ledThree = 43;
int ledFour = 44;
boolean lastOne = LOW;
boolean lastTwo = LOW;
boolean lastThree = LOW;
boolean lastFour = LOW;
boolean currentOne = LOW;
boolean currentTwo = LOW; 
boolean currentThree = LOW; 
boolean currentFour = LOW;
boolean onOne = true;
boolean onTwo = false;
boolean onThree = false;
boolean onFour = false;

int toggle(int switchs, int leds, boolean lasts, boolean currents, boolean ons)
{
     currents = digitalRead(switchs);
  if (lasts != currents)
  {
    delay(10);
    currents = digitalRead(switchs);
  }
  if (lasts == LOW && currents == HIGH)
  {
    ons = !ons;
  }
  
  currents = lasts;
  
 digitalWrite(leds, ons);
 return lasts; 
 return switchs;
 return leds;
 return currents;
 return ons;
}


void setup() {
  pinMode(switchOne, INPUT);
  pinMode(ledOne, OUTPUT);
  pinMode(switchTwo, INPUT);
  pinMode(ledTwo, OUTPUT);
  pinMode(switchThree, INPUT);
  pinMode(ledThree, OUTPUT);
  pinMode(switchFour, INPUT);
  pinMode(ledFour, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
toggle(switchOne, ledOne, lastOne, currentOne, onOne);
toggle(switchTwo, ledTwo, lastTwo, currentTwo, onTwo);
toggle(switchThree, ledThree, lastThree, currentThree, onThree);
toggle(switchFour, ledFour, lastFour, currentFour, onFour);
}

So I have gotten pretty close with my debounce program called "toggle". The weird thing is that if the toggle program is in the void loop it seems to switch the LED whereas when the toggle program is external to the void loop it just lights up a led and then it turns off again when the switch is released. I tried inserting "return" variable hoping that would update my variables.

Interesting

int toggle(int switchs, int leds, boolean lasts, boolean currents, boolean ons)
{
    ...

    return lasts;

    return switchs;

    return leds;

    return currents;

    return ons;
}

Yeah... Does that actually work?

Ask the original poster, but I'd say not very likely!

EDIT: I say that because I can only guess as to his intent since he's not yet stated it.

Ill check it out here shortly, but the intent is to run 4 LEDs via the same function. If the LED is off when the button is pushed, turn on. If the LED is on when the button is pushed, turn off.

I meant of the posted fragment not your full code.

// <http://forum.arduino.cc/index.php?topic=168581.new#new>

#define A_SIZEOF(ARRAY)     (sizeof(ARRAY) / sizeof(ARRAY[0]))

const uint8_t   pinSWITCH_1 = 31;   // momentary push button
const uint8_t   pinSWITCH_2 = 32;   // momentary push button
const uint8_t   pinSWITCH_3 = 33;   // momentary push button
const uint8_t   pinSWITCH_4 = 34;   // momentary push button

const uint8_t   pinLED_1    = 41;
const uint8_t   pinLED_2    = 42;
const uint8_t   pinLED_3    = 43;
const uint8_t   pinLED_4    = 44;

const uint8_t   LED_OFF     = LOW;
const uint8_t   LED_ON      = HIGH;

const uint8_t   SWITCH_OFF  = LOW;
const uint8_t   SWITCH_ON   = HIGH;

struct circuit_t
{
    const unsigned long _millisDEBOUNCE;
    const uint8_t       _pinSWITCH;
    const uint8_t       _pinLED;

    
    uint8_t             _stateSwitchPrevious;
    uint8_t             _stateLED;
    unsigned long       _millisPrevious;

    circuit_t(uint8_t pinSwitch, uint8_t pinLED, unsigned long millisDEBOUNCE = 50)
        : _millisDEBOUNCE(millisDEBOUNCE)
        , _pinSWITCH(pinSwitch)
        , _pinLED(pinLED)

        , _stateSwitchPrevious(LED_OFF)
        , _stateLED(LED_OFF)
        , _millisPrevious(millis())
    {}

    void begin() const
    {
        pinMode(_pinSWITCH, INPUT);
        pinMode(pinLED_1,  OUTPUT);
    }

    void process()
    {
        uint8_t stateSwitch = digitalRead(_pinSWITCH);
        if ( stateSwitch != _stateSwitchPrevious )
        {
            _millisPrevious = millis();
        } 

        if ( (millis() - _millisPrevious) > 50UL )
        {
            if ( stateSwitch != _stateSwitchPrevious )
            {
                _stateSwitchPrevious = stateSwitch;

                if ( _stateSwitchPrevious == SWITCH_ON )
                {
                    _stateLED = !_stateLED;
                }
            }
        }

        digitalWrite(_pinLED, _stateLED);

        _stateSwitchPrevious = stateSwitch;
    }
};

circuit_t circuits[] =
{
      { pinSWITCH_1, pinLED_1 }
    , { pinSWITCH_2, pinLED_2 }
    , { pinSWITCH_3, pinLED_3 }
    , { pinSWITCH_4, pinLED_4 }
};

void loop()
{
    for ( size_t i = A_SIZEOF(circuits); i--; )
    {
        circuits[i].process();
    }
}

void setup()
{
    for ( size_t i = A_SIZEOF(circuits); i--; )
    {
        circuits[i].begin();
    }
}

EDIT: OCD concerning formatting

bronco9588:
Ill check it out here shortly, but the intent is to run 4 LEDs via the same function. If the LED is off when the button is pushed, turn on. If the LED is on when the button is pushed, turn off.

Simple toggle setup you can try by touching a wire attached to ground to pin 5.

//zoomkat LED button toggle test 11-08-2012

int button = 5; //button pin, connect to ground as button
int press = 0;
boolean toggle = true;

void setup()
{
  pinMode(13, OUTPUT); //LED on pin 13
  pinMode(button, INPUT); //arduino monitor pin state
  digitalWrite(5, HIGH); //enable pullups to make pin 5 high
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      digitalWrite(13, HIGH);   // set the LED on
      toggle = !toggle;
    }
    else
    {
      digitalWrite(13, LOW);    // set the LED off
      toggle = !toggle;
    }
  }
  delay(500);  //delay for debounce
}

I think you have almost got it. The problem is that you can't return 4 values like that from a single function. Actually, only two of the values change. If you pass them by address instead of by value, it should work:

int toggle(int switchs, int leds, boolean &lasts, boolean currents, boolean &ons)
{
     currents = digitalRead(switchs);
  if (lasts != currents)
  {
    delay(10);
    currents = digitalRead(switchs);
  }
  if (lasts == LOW && currents == HIGH)
  {
    ons = !ons;
  }
  
//  currents = lasts;
  lasts = currents;  // I think this is what you really want to do
  
 digitalWrite(leds, ons);
}

Nice!

That did the trick. Can you explain what the "&" sign does and when I should consider using it?

When you don't use the & with a variable passed to a function you are passing the value of that variable. If the function changes the value it is only changing its local copy, not the original and the changed value is lost when the function ends.

If you use the & you pass the address of the variable. If the function changes the value it is changing the original one so the changed value is available when the function ends.

Can you explain what the "&" sign does and when I should consider using it?

It means "pass by reference". C normally is a "pass by value" language. When you call a function, with a variable in the parentheses, the value in the variable is passed to the function. With pass by reference, the address of the variable is passed, not the value in the variable. With the address, the function can access the value in the variable, and it can change the value in the variable.

Any time you need to have a function change the value in a variable, as opposed to creating a new value, use pass by reference.