# How do I make something loop a specific number of times?

I have something like: if x > y, do this, else do this. It woks fine but it would be better if it would “do this” repeatedly for a minimum amount of time, or a specific number of times. For my application, when the analog input is on the line and jumping back and forth between the numbers (because analog inputs are never perfectly stable) which determines whether to run the if or the else, the light flickers between colors. If i could just get it to stick with one part of the code for a certain amount of time it would look so flickery. How would i go about doing this? In my code below i have made sure to comment everything as detailed as i can so that it makes sense to anyone, but my thought process might not line up with yours, so ask me questions if you need to know something and please be patient with me, I’m new.

``````/*

***CODE FOR MULTIPLEXING TWO LEDS WITH A TLC5940 LED CONTROLLER***
TLC will be set by Tlc.set('pin number',  'pwm duty cycle 0 to 4095')
brightness is split into two parts, 4 bytes of brightness multiplied
evenly across all lights, and 8 bytes of color definition.
infrared is hooked up to an op amp to multiply the photo-transistors output by 201.
typical readings are 350 to 450 for no infrared light, and 500 to 950 with a reflecting object
serial monitor displays analog readings from the infrared sensors

*/

#include <Tlc5940.h> // Tlc library

int l = 16;          // brightness factor 0 to 16
int r = 2;           // tlc5940 pin 2 is for red
int g = 1;           // tlc5940 pin 1 is for green
int b = 0;           // tlc5940 pin 0 is for blue
int d = 2;           // set delay
int val;             // value for infrared sensor
int t = 405;         // set the number the analog input needs to pass to switch led colors (0 to 1024)

void setup()         // run once to set up the chip, outputs, and inputs
{
Serial.begin(9600);  // start serial output at the standard baud rate so i can tune the sensor
pinMode(6, OUTPUT);  // set the anode pin of led one to output
pinMode(7, OUTPUT);  // set the anode pin of led two to output
Tlc.init();          // initialize TLC5940
}

void loop()           // loop this code forever, this is where stuff happens
{
if (val < t)             // if the value is less than 4, make the lights blue and red. if not, make them green
{                                     //THIS IS A PART I WOULD LIKE TO LOOP FOR 100MS OR 25 TIMES                                                                                                               // If the IR sensor reads below t, run this
loop(25){
Tlc.set(r, 0 * l);    // set red to 0
Tlc.set(g, 0 * l);    // set green to 0
Tlc.set(b, 255 * l);  // set blue to full
digitalWrite(6, 0);   // turn everything off to let the TLC5940 update
Tlc.update();         // update
digitalWrite(7, 1);   // turn on one led to blue
delay(d);             // wait 2ms with the blue light on
Tlc.set(r, 255 * l);  // set red to full
Tlc.set(g, 0 * l);    // set green to o
Tlc.set(b, 0 * l);    // set blue to zero
digitalWrite(7, 0);   // turn off the blue led to let the tlc update
Tlc.update();         // update
digitalWrite(6, 1);   // turn on the red led
delay(d);             // wait 2ms with the red light on, then start again
}
}
else                     // if there is an analog reading greater than 4, make the lights green
{                                     //THIS IS A PART I WOULD LIKE TO LOOP FOR 100MS OR 25 TIMES                                                                         // If the IR sensor reads above t, run this
Tlc.set(r, 0 * l);    // set red to 0
Tlc.set(g, 255 * l);  // set green to full
Tlc.set(b, 0 * l);    // set blue to 0
digitalWrite(6, 0);   // turn everything off to allow the TLC5940 to update
Tlc.update();         // update
digitalWrite(7, 1);   // turn on one led
delay(d);             // wait 2ms with one green light
Tlc.set(r, 0 * l);    // set red to 0
Tlc.set(g, 255 * l);  // set green to full
Tlc.set(b, 0 * l);    // set blue to 0
digitalWrite(7, 0);   // turn off the led to let the TLC5940 update
Tlc.update();         // update
digitalWrite(6, 1);   // turn on the other led to green
delay(d);             // wait 2ms with the green light on then start again
}
}
``````

I understand that i could slow down the read to only read maybe 10 times per second, but im going to need it to read as fast as it can, because that will be needed later in developing animations in response to the IR sensor. So it would be best to make the changes by looping.
Other things, mostly unrelated to my question but answers would be helpful, I would like to switch the digital writes with port commands to make the multiplexing faster. For now, i need a 2ms delay between changes because for some reason the chip kills itself if its below 1.5ms. It would definitely have to do with my improper use of the Tlc5940 library. I’m supposed to have all pins low before updating the Tlc5940 and after i need to wait for while(tlc_needXLAT); before turning the power back on, but as you could see i have none of that anywhere in my code, because i have no idea how to put that in there. I’m having trouble learning how to use the library, where do i go to see how to properly use the library? The examples were a little helpful but had nothing to do with multiplexing, which is what I’m doing. That’s just what some guy told me i need to do. Also for the serial output, i don’t want to see every number it ever gets, they scroll by too fast for me to see them all anyway. How do i slow down the serial output without slowing down the whole code? I know some people want to see a schematic, but I promise it’s irrelevant to my question, all the details you need should be commented, but if you do need to know something about my circuit ill try to explain it and make a schematic if i really need to.

If you have any criticisms on my code let me know nicely, I’m always looking to learn and improve, Unless its about how I put all my squiggly brackets their own line, it just looks more organized to me.

Additional details that might be useful:

Using a windows 8 computer

Running this on an Arduino Mega 2560

To do things at timed interval or for certain amount of times use millis();. See the Blink Without Delay example in the IDE.

or replace loop(25) with

for(int ii=0; ii<25, ii++)

You need to read an intro on C programming.

For my application, when the analog input is on the line and jumping back and forth between the numbers (because analog inputs are never perfectly stable) which determines whether to run the if or the else, the light flickers between colors

The usual way to deal with that problem is to ignore small changes in the value from the analog input. Something like

``````newVal = analogRead(A0);
if ( abs (newVal - prevVal) > deadBand) {
prevVal = newVal;
// do stuff
}
``````

...R

sonyhome:

or replace loop(25) with

for(int ii=0; ii<25, ii++)

You need to read an intro on C programming.

Woops, that loop(25) was a poor failed attempted at making it loop 25 times, i forgot to remove that before posting. Thanks for the substitute

Pauly: To do things at timed interval or for certain amount of times use millis();. See the Blink Without Delay example in the IDE.

While im not sure how to us that, ill try. But ill definitely use that to slow down the serial outputs without slowing down the analog read