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):
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.
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.
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);
}
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.