Go Down

Topic: How do I repeat a function n times before moving to next function? (Read 2838 times) previous topic - next topic

tazer77

Total noob here.  I'm programming many LEDs to perform multiple functions in a light show sort of way.  I've got about 6 different functions working fine, but I want to repeat each function a number of times before moving to the next function.  I found this code
Code: [Select]
   int i;
   int n;
   // Code to get some value of "n"

   for (i = 0; i < n; i++) {// Loop to do "something" n times
       // Do "something"
   }

but I started with a sketch that set everything up with "index", so I'm not sure how to incorporate the above type of coding, as index already takes up my "for" loop to move between LEDs.  Here is a simplified example of what I'm working on
Code: [Select]
int ledPins[] = {3,5,6};

void setup()
{
  int index;
  for(index = 0; index <= 2; index++)
  {
    pinMode(ledPins[index],OUTPUT);
  }
}

void loop()
{
  FUNCTION1(); 
  FUNCTION2();
  FUNCTION3();
  FUNCTION4();       
         
 

void FUNCTION1()
{
  int index;
  int delayTime = 200;
  for(index = 0; index <= 2; index++)
  {
    digitalWrite(ledPins[index], HIGH);   
    delay(delayTime);                             
    digitalWrite(ledPins[index], LOW);   
  }
  for (index = 2; index >= 0; index--) {
    digitalWrite(ledPins[index], HIGH);
    delay(delayTime);                             
    digitalWrite(ledPins[index], LOW);
  } 
}
 
void FUNCTION2()
{
  int index;
  int delayTime = 100;
  for(index = 0; index <= 2; index++)
  {
    digitalWrite(ledPins[index], HIGH);
    delay(delayTime);               
  }                                 
  for(index = 2; index >= 0; index--)
  {
    digitalWrite(ledPins[index], LOW);
    delay(delayTime);
  }               
}
 


Is there another way to do what I want without using index, so I can use my "for" loop to determine the number of times the function repeats?

Secondary question...... How can I run 2 functions simultaneously with Function 1 controlling one group of outputs and Function 2 controlling a different group of outputs?

Sorry if this is a stupid question, but I'm stuck.  Thanks in advance.

marco_c

Code: [Select]
for (uint8_t i=0; i<n1; i++)
  FUNCTION1();
for (uint8_t i=0; i<n2; i++)
  FUNCTION2();

etc ...



Quote
Secondary question...... How can I run 2 functions simultaneously with Function 1 controlling one group of outputs and Function 2 controlling a different group of outputs?
Yes but not with all the delay()s in the code. You need to get your head around BlinkWithoutDelay and finite state machines.

jack wp

"Is there another way to do what I want without using index, so I can use my "for" loop to determine the number of times the function repeats?"

Yes. Check the documentation about the "for" loop function. It explains it pretty well.

If you have further questions, just ask.

Go Up