Pages: [1] 2   Go Down
Author Topic: Functions  (Read 700 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Faraday Member
**
Karma: 65
Posts: 2504
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

UK
Offline Offline
Sr. Member
****
Karma: 7
Posts: 436
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Or....

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

Offline Offline
Faraday Member
**
Karma: 65
Posts: 2504
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

O-I-C
Logged

"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Same thing but a bit more readable.

Code:
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);
}
« Last Edit: May 26, 2013, 08:06:42 pm by lloyddean » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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.
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Interesting

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

    return lasts;

    return switchs;

    return leds;

    return currents;

    return ons;
}
Logged

Offline Offline
God Member
*****
Karma: 25
Posts: 526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yeah...  Does that actually work?
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: May 27, 2013, 06:42:08 pm by lloyddean » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I meant of the posted fragment not your full code.
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: May 27, 2013, 10:36:33 pm by lloyddean » Logged

0
Offline Offline
Tesla Member
***
Karma: 143
Posts: 9586
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
//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
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
God Member
*****
Karma: 25
Posts: 526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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

Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nice!

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

Pages: [1] 2   Go Up
Jump to: