Pages: [1]   Go Down
Author Topic: Referring to a group of functions  (Read 496 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a repeating chunk of code throughout my sketch and I was wondering if there was any way I could define the group of functions as 'x' so I could write 'x' whenever I wanted that set of functions to be performed.  For example, could I refer to all this:

Code:
             digitalWrite(ground[0], LOW);
             delay(Ptimer);
             digitalWrite(ground[0], HIGH);
             delay(Ptimer);
             digitalWrite(ground[1], LOW);
             delay(Ptimer);
             digitalWrite(ground[1], HIGH); 
             delay(Ptimer);
             digitalWrite(ground[2], LOW);
             delay(Ptimer);
             digitalWrite(ground[2], HIGH); 
             delay(Ptimer);
             digitalWrite(ground[3], LOW);
             delay(Ptimer);
             digitalWrite(ground[3], HIGH);
             count++;

as 'GroundAlternate' or something, so I needn't write that code out many times.

Cheers
Logged

Sydney, Australia
Online Online
Edison Member
*
Karma: 33
Posts: 1285
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Define your own function with the code inside it.  Look up any reference for C to work this out.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

UK
Offline Offline
Faraday Member
**
Karma: 100
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
You can either create a function:

[code

void doit(int x)
{
  digitalWrite(ground[x], LOW);
  delay(Ptimer);
  digitalWrite(ground[x], HIGH);
  delay(Ptimer);
}

// ...

doit(0);
doit(1);
doit(2);
...

Or you can create a macro:

Code:
#define DOIT(X) digitalWrite(ground[X], LOW); \
             delay(Ptimer); \
             digitalWrite(ground[X], HIGH); \
             delay(Ptimer); \

DOIT(0)
DOIT(1)
DOIT(2)
... etc ...

The former is more efficient from a code space point of view, but less efficient from an execute time point of view.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4813
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a repeating chunk of code throughout my sketch and I was wondering if there was any way I could define the group of functions as 'x' so I could write 'x' whenever I wanted that set of functions to be performed.  For example, could I refer to all this:

Code:
             digitalWrite(ground[0], LOW);
             delay(Ptimer);
             digitalWrite(ground[0], HIGH);
             delay(Ptimer);
             digitalWrite(ground[1], LOW);
             delay(Ptimer);
             digitalWrite(ground[1], HIGH); 
             delay(Ptimer);
             digitalWrite(ground[2], LOW);
             delay(Ptimer);
             digitalWrite(ground[2], HIGH); 
             delay(Ptimer);
             digitalWrite(ground[3], LOW);
             delay(Ptimer);
             digitalWrite(ground[3], HIGH);
             count++;

as 'GroundAlternate' or something, so I needn't write that code out many times.

Cheers

It is sort of funny that I can't quickly and easily find the teaching of so basic a thing as writing your own functions on site here. I see them in some examples but not knowing how to is like coming to a formal party in only underwear.

Making your own functions is something you should learn all of but in one post I can't hand it all to you.  I can show you how to fix this problem but it should only whet your appetite to learn more.

You have a basic pattern:
Code:
            digitalWrite(ground[0], LOW);
             delay(Ptimer);
             digitalWrite(ground[0], HIGH);
             delay(Ptimer);

The only thing that changes is which ground[] alternates between LOW and HIGH.
Let's pretend that ground[] is a global array.
You only want action and no information back from your function.

Code:
// here is your simple-could-be-better function
// it must be in code -after- ground[] is defined
// it is not to be defined inside any other function, it stands alone

void GroundAlternate( int thisOne )
{
             digitalWrite(ground[thisOne], LOW);
             delay(Ptimer);
             digitalWrite(ground[thisOne], HIGH);
             delay(Ptimer);
}

void setup( void )
{
// ....

// and somewhere down in your code, probably but not necessarily inside loop()
// this makes the 4 grounds alternate in turn

  for ( int i = 0; i < 4; i++ )
  {
    GroundAlternate( i );
  }


Really, go learn more. That way when you get an idea you will not have to invent solutions that are already inherent in C/C++. In the long run it will save you time and hair.

And once again, Majenko finishes typing before I do.....
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

UK
Offline Offline
Faraday Member
**
Karma: 100
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

And once again, Majenko finishes typing before I do.....

I'm not known as Ninja Fingers for nothing smiley-razz
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

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

Thank you everyone for the informative replies!  I literally started C like a week ago so basic things I'm still getting the hang of, but more importantly, once I understand it, it makes perfect sense.  I ended up creating my own functions:

Code:
void ascend(){
 
             digitalWrite(ground[0], LOW);
             delay(Ptimer);
             digitalWrite(ground[0], HIGH);
             delay(Ptimer);
             digitalWrite(ground[1], LOW);
             delay(Ptimer);
             digitalWrite(ground[1], HIGH); 
             delay(Ptimer);
             digitalWrite(ground[2], LOW);
             delay(Ptimer);
             digitalWrite(ground[2], HIGH); 
             delay(Ptimer);
             digitalWrite(ground[3], LOW);
             delay(Ptimer);
             digitalWrite(ground[3], HIGH);       
}

and

Code:
void reset() {
           for (int i = 0; i < 16; i++){
             digitalWrite(led[i], LOW);
           }
           for (int x = 0; x < 4; x++){
             digitalWrite(ground[x], HIGH);
           }
}

Majenko, I haven't looked into macros yet but they look pretty interesting, will have a read later, thanks!

GoForSmoke, thanks! it seems more helpful to define the function with variables (thisOne) which I can send depending on what values I want to activate, like what you have done here:

Code:
void GroundAlternate( int thisOne )
{
             digitalWrite(ground[thisOne], LOW);
             delay(Ptimer);
             digitalWrite(ground[thisOne], HIGH);
             delay(Ptimer);
}

But what if I have a pattern that doesn't seem to follow an easy order such that I could use:

Code:
for ( int i = 0; i < 4; i++ )
  {
    GroundAlternate( i );
  }

For example I have a pattern that goes something like:

Code:
             digitalWrite(led[0], HIGH);
             digitalWrite(led[5], HIGH);
             digitalWrite(led[10], HIGH);
             digitalWrite(led[15], HIGH);
             ...
             digitalWrite(led[1], HIGH);
             digitalWrite(led[5], HIGH);
             digitalWrite(led[10], HIGH);
             digitalWrite(led[14], HIGH);
             ...
             digitalWrite(led[2], HIGH);
             digitalWrite(led[6], HIGH);
             digitalWrite(led[9], HIGH);
             digitalWrite(led[13], HIGH);
             ...


Is there a way I can input these values from elsewhere that don't seem to follow a mathematical pattern?

Logged

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

Nvm! Figured out the last part

Code:

void spiral(int a, int b, int c, int d) {
             digitalWrite(led[a], HIGH);
             digitalWrite(led[b], HIGH);
             digitalWrite(led[c], HIGH);
             digitalWrite(led[d], HIGH); 
             
}
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4813
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

For example I have a pattern that goes something like:

Code:
             digitalWrite(led[0], HIGH);
             digitalWrite(led[5], HIGH);
             digitalWrite(led[10], HIGH);
             digitalWrite(led[15], HIGH);
             ...
             digitalWrite(led[1], HIGH);
             digitalWrite(led[5], HIGH);
             digitalWrite(led[10], HIGH);
             digitalWrite(led[14], HIGH);
             ...
             digitalWrite(led[2], HIGH);
             digitalWrite(led[6], HIGH);
             digitalWrite(led[9], HIGH);
             digitalWrite(led[13], HIGH);
             ...


Is there a way I can input these values from elsewhere that don't seem to follow a mathematical pattern?


Those lights are going to come on so fast, like a microsecond apart, you'd never see any pattern to it unless there's some kind of delays (doesn't have to be delay() function) between.

Yes you can make an array of bytes (because they count 0-255 while you have less than 255 pins and only use 1 byte each) to hold a sequence. Then when you iterate through the sequence, index 0 to last, the values in the array will be your pattern.
 
You can store fairly large arrays in PROGMEM (flash memory where the program is stored) if the ones you want won't fit in SRAM (regular memory for variables and the vital program stack).
UNO has only 2k of SRAM for everything, it's a bad idea to try and use it all up.
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

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

Thanks again!

Yeah I figure I could either make an array with my values so that I can reference them elsewhere but I found it easier to create a function with 4 variables that I just manually input values for in the code.  The point of the previous code is to light up 4 columns of LEDs simultaneously, which 4 columns I am lighting up changes, so I have put the appropriate delay in there.  My code is working perfectly at the moment (watching my little cube flash away smiley-grin)

If you are curious, this is what I ended up implementing:

Code:

int led[] = {22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37};
int ground[] = {50,51,52,53};
char Ptimer = 0.1; //timer set low enough for persistence of vision to be observed
int timer = 1000;  //generic timer delay
int count = 0;
int count1 = 0;

void setup() {               
 
     for (int i = 0; i < 16; i++){
       pinMode(led[i], OUTPUT);
     }
     for (int x = 0; x < 4; x++){
       pinMode(ground[x], OUTPUT);
     }
       
      digitalWrite(ground[0], LOW);
      digitalWrite(ground[1], HIGH);
      digitalWrite(ground[2], HIGH);
      digitalWrite(ground[3], HIGH);
    }


void ascend(){
 
             digitalWrite(ground[0], LOW);
             delay(Ptimer);
             digitalWrite(ground[0], HIGH);
             delay(Ptimer);
             digitalWrite(ground[1], LOW);
             delay(Ptimer);
             digitalWrite(ground[1], HIGH); 
             delay(Ptimer);
             digitalWrite(ground[2], LOW);
             delay(Ptimer);
             digitalWrite(ground[2], HIGH); 
             delay(Ptimer);
             digitalWrite(ground[3], LOW);
             delay(Ptimer);
             digitalWrite(ground[3], HIGH);       
}

void reset() {
           for (int i = 0; i < 16; i++){
             digitalWrite(led[i], LOW);
           }
           for (int x = 0; x < 4; x++){
             digitalWrite(ground[x], HIGH);
           }
}

void spiral(int a, int b, int c, int d) {
              while (count < timer){
             digitalWrite(led[a], HIGH);
             digitalWrite(led[b], HIGH);
             digitalWrite(led[c], HIGH);
             digitalWrite(led[d], HIGH);       
             ascend();
             count++;
          }
          count = 0;
          reset();             
}

void loop() {
 
  //-------------------------------------------------------------
  //   Flashes cube layers upwards getting progressively slower
  //-------------------------------------------------------------
 
for(int x = 0; x < 16; x++){
      digitalWrite(led[x], HIGH);
}
  while(count < 50){
   
      digitalWrite(ground[3], HIGH);
      digitalWrite(ground[0], LOW);
      delay(count);
      digitalWrite(ground[0], HIGH);
      digitalWrite(ground[1], LOW);
      delay(count);
      digitalWrite(ground[1], HIGH);
      digitalWrite(ground[2], LOW);
      delay(count);
      digitalWrite(ground[2], HIGH);
      digitalWrite(ground[3], LOW);
      delay(count);
     
      count++;
  }
  count = 0;
  reset();
  delay(200);
 
  //---------------------------------------------------
  //     Spiral code - anti-clockwise vertical spiral
  //---------------------------------------------------
 
  while (count1 < 10){
         spiral(0,5,10,15);
         spiral(1,5,10,14);           
         spiral(2,6,9,13);         
         spiral(3,6,9,12);         
         spiral(7,6,9,8);     
         spiral(11,10,5,4);     
         spiral(15,10,5,0);
         spiral(14,10,5,1);
         spiral(13,9,6,2);
         spiral(12,9,6,3);
         spiral(8,9,6,7);
         spiral(4,5,10,15);
         count1++;
  }
 
  count1 = 0;
  count = 0;
  reset();
  delay(200);
 
  //--------------------------------------------------------
  //     Sideways sweep
  //-------------------------------------------------------
 
  while (count1 < 10){
   
     spiral(0,1,2,3);
     spiral(4,5,6,7);
     spiral(8,9,10,11);
     spiral(12,13,14,15);
     spiral(3,7,11,15);
     spiral(2,6,10,14);
     spiral(1,5,9,13);
     spiral(0,4,8,12);
     spiral(12,13,14,15);
     spiral(8,9,10,11);
     spiral(4,5,6,7);
     spiral(0,1,2,3);
     spiral(0,4,8,12);
     spiral(1,5,9,13);
     spiral(2,6,10,14);
     spiral(3,7,11,15);
     count1++;
  }
  count1 = 0;
  delay(200);
}
Logged

Pages: [1]   Go Up
Jump to: