Pages: [1]   Go Down
Author Topic: question about creating functions  (Read 665 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi. I wrote the code below to fade in and out the three colors on a rgb LED on three different "schedules" to get this nice sorta continuously changing color thing.  It's working fine, but I was wondering if anyone has any ideas on perhaps making the fading routine into a function. I can see how to do it, except for the problem with updating the boolean flag. Can I have multiple return values? That would be great, but not sure if it's possible.
Code:
#define R_LED 11    // defining 3 pins for the 3 colors of the LED
#define G_LED 9    
#define B_LED 10

int r = 0;         // variables for brightness for each led
int g = 0;
int b = 0;

boolean r_dir = 1;     // variables to flag for direction up or down
boolean g_dir = 1;     // for fades
boolean b_dir = 1;

void setup() {
      //  nothing to setup
}

void loop()  {
  
  // loop, one per color for fades
  
  if(r_dir)  {
    r = r + 3;              // increment up
    if (r >= 252) {
      r_dir = 0;             //change direction
    }
  }
  else  {
    r = r - 3;              // increment down
    if (r <= 0)   {
      r_dir = 1;             // change direction
    }
  }
  
  // now for green
  
  if(g_dir)  {
    g = g + 5;              // increment up
    if (g >= 250) {
      g_dir = 0;             //change direction
    }
  }
  else {
    g = g - 5;              // increment down
    if (g <= 0)   {
      g_dir = 1;             // change direction
    }
  }
  
  // and for blue
 
   if(b_dir)  {
    b = b + 7;              // increment up
    if (b >= 248) {
      b_dir = 0;             //change direction
    }
  }
  else {
    b = b - 7;              // increment down
    if (b <= 0)   {
      b_dir = 1;             // change direction
    }
  }
  
 //  now to write the values
 
 
analogWrite(R_LED, r);
analogWrite(G_LED, g);
analogWrite(B_LED, b);
delay(50);

}

Any help would be greatly appreciated!


thanks

pam
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's already in a function - "loop()"     smiley-wink

You can't return multiple values from a function, but you could pass a single pointer to multiple parameters, using a structure, and a pointer to it into your function;

Code:
typedef struct {
  int currentVal;
  boolean direction;
  byte increment;
  byte LEDpin;
} colour_t;

colour_t red = {0, 1, 3, R_LED};
colour_t green = {yadda-yadda};

..

..
void update (colour_t* LED)
{
  if (LED->direction)
etc, and so-on.
}
...
...
update (&red);

« Last Edit: June 17, 2009, 03:08:28 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks for the helpful response!  I had a feeling that was the answer smiley

pam
Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 12
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Just to give you more options:
Quote

const byte analogPin1 = 0;
const byte analogPin2 = 1;
const byte analogPin3 = 2;

int value1 = 0;
int value2 = 0;
int value3 = 0;

void readAnalogValuesInto(int &pin1, int &pin2, int &pin3);

void setup(){
  Serial.begin(9600);
  pinMode(analogPin1,INPUT);
  pinMode(analogPin2,INPUT);
  pinMode(analogPin3,INPUT);
}

void loop(){
  readAnalogValuesInto(value1,value2,value3);
  
  Serial.print(value1);
  Serial.print(" ");
  Serial.print(value2);
  Serial.print(" ");
  Serial.println(value3);
}

/*
This function will not return anything,
but will modify the values passed down as arguments
*/
void readAnalogValuesInto(int &pin1, int &pin2, int &pin3){
  pin1 = analogRead(analogPin1);
  pin2 = analogRead(analogPin2);
  pin3 = analogRead(analogPin3);
}



This is called: Passing values by reference.
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 77
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Keep in mind I only know C and not C++, but in C your could would be a bit different:

Code:
const byte analogPin1 = 0;
const byte analogPin2 = 1;
const byte analogPin3 = 2;

int value1;
int value2;
int value3;

void readAnalogValuesInto(int *pin1, int *pin2, int *pin3);

void setup(){
 Serial.begin(9600);
 pinMode(analogPin1,INPUT);
 pinMode(analogPin2,INPUT);
 pinMode(analogPin3,INPUT);
}

void loop(){
 readAnalogValuesInto(&value1, &value2, &value3);
 
 Serial.print(value1);
 Serial.print(" ");
 Serial.print(value2);
 Serial.print(" ");
 Serial.println(value3);
}

/*
This function will not return anything,
but will modify the values passed down as arguments
*/
void readAnalogValuesInto(int *pin1, int *pin2, int *pin3){
 *pin1 = analogRead(analogPin1);
 *pin2 = analogRead(analogPin2);
 *pin3 = analogRead(analogPin3);
}
« Last Edit: June 17, 2009, 11:38:49 pm by rFree » Logged

Pages: [1]   Go Up
Jump to: