decrementing the delay value

I would like to decrease the delay value within a loop each time the program runs through the loop. At some minimum value the program would move to the next block. suggestions on programming this? I am new to arduino programming so please be specific.
thanks,
-jim

It's not entirely clear what you want to achieve, but if you're talking about code blocks (i.e. you want to end the loop) then you could doing it using any of the looping constructs. Here is one example using a while loop:

long delayValue = initialDelayValue;
while(delayValue > minimumDelay)
{
   delay(delayValue);
   delayValue -= decrement;
   // whatever else you want to do in the loop goes here
}

thank you for the reply. What I am trying to do is blink a series of LEDs with a delay between the HIGH signals to all the leds. The delay value would decrease upon each cycle through the leds.

I'm a bit unclear on how to do this. Let us start with a value of 10 as a delay and decrement it by 1 to a minimum of 3 before the program exits the loop.

Hope that explains. Thanks!!
-jim

long delayValue = 10000; // milliseconds
while(delayValue >= 3000)
{
   delay(delayValue);
   ... turn LEDs on
   delay(delayValue);
   ... turn LEDs off
   delayValue -= 1000;
   // whatever else you want to do in the loop goes here
}

I guess you want to turn the LEDs on and off each time round the loop, so I used two delays (one to control the on time, the other for the off time). You might want to halve the delay values.

I guess the delay durations you mentioned were in seconds.

I didn't put in code to actually turn the LEDs on and off. I assume you already know how to do that.

OK, Im still getting error messages. Here is my code. Please excuse my ignorance of what the error messages mean:

int led1 = 1;
int led2 = 2;
int led3 = 3;
int led4 = 4;
int led5 = 5;
int led6 = 6;
int led7 = 7;
int led8 = 8;
int led9 = 9;
int led10 = 10;
int led11 = 11;
int led12 = 12;

// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
pinMode(led7, OUTPUT);
pinMode(led8, OUTPUT);
pinMode(led9, OUTPUT);
pinMode(led10, OUTPUT);
pinMode(led11, OUTPUT);
pinMode(led12, OUTPUT);

long delayValue = 1000;
while(delayValue >=30)

void loop();
{

delay(delayValue);
digitalWrite(led1, HIGH);
delay(delayValue);
digitalWrite(led1, LOW);
delayValue-=10;
}

Widpyro:
OK, Im still getting error messages. Here is my code. Please excuse my ignorance of what the error messages mean:

int led1 = 1;  //avoid the use of pins 0 and 1. 

//They are used by USB when uploading code.
int led2 = 2;
int led3 = 3;
int led4 = 4;
int led5 = 5;
int led6 = 6;
int led7 = 7;
int led8 = 8;
int led9 = 9;
int led10 = 10;
int led11 = 11;
int led12 = 12;

// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.
  pinMode(led1, OUTPUT); 
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(led7, OUTPUT);
  pinMode(led8, OUTPUT);
  pinMode(led9, OUTPUT);
  pinMode(led10, OUTPUT);
  pinMode(led11, OUTPUT);
  pinMode(led12, OUTPUT);
  long delayValue = 1000;
}
 
void loop(){
  while(delayValue >=30){
        delay(delayValue);
        digitalWrite(led1, HIGH);
        delay(delayValue);
        digitalWrite(led1, LOW);
        delayValue-=10;
  }
}

sorry to be a pain, but Im getting an error message about the line just after the "loop" command. It says the "delayValue is not declared in this scope".
thanks,
jim

You have defined delayValue within setup() so it is only accessible to code in that function.

The definition should probably be moved so it is outside both setup() and loop(). The usual place to put what are called global variables is before setup(). Global variables can be accessed by all parts of your code.

...R

Thank you very much. That solved the problem.

I have one other question: is it possible to have a loop function within a loop? Or start another loop after exiting a loop?

And also, where would you recommend I find a good tutorial on arduino programming?

Thanks,
Jim

Widpyro:
I have one other question: is it possible to have a loop function within a loop? Or start another loop after exiting a loop?

You can have loops within loops within loops within ..... That doesn't mean it's a good idea :slight_smile:

Only one function in your program can be called "loop()" - every function must have a separate name.

Within the Arduino system the function called "loop()" never exits - it repeats continuously, perhaps thousands of times per second.

Sorry I learned my programming long before the Arduino so I have no experience of Arduino tutorials. Google will find dozens of them.

...R

So are the two parenthesis after the word "loop" the name of that loop? Could I start another loop called "loop2" or similar?
Thanks,
Jim

A function in C/C++ is defined like this

void functionName(int parameterName) {
   // function code goes here
}

The "void" tells the compiler that the function will not be returning a value. You could also, for example, have

int functionName(int parameterName) {
  parameterName = paramenterName / 2;
  return (parameterName);
}

which would tell the compiler that the function will be returning an integer value.

functionName can be anything you like as long as each name is unique

Sometimes you need to pass values to the function (parameters) and the type of those data variables and the name they will be known by within the function are defined within the (). In this example an integer value is being passed to the function. If the () brackets are empty it means that no parameters are required.

Bringing all this together ...

void loop() {

}

is a function named loop which doesn't require any parameters and doesn't return any value.

The word "loop" was chosen by the Arduino designers to convey the idea that it runs over and over repeatedly. But they could have called it anything.

And, yes, loop2 would be perfectly acceptable to the compiler. But I would strongly advise you to choose function names that are meaningful and clearly describe what the function does. It will make the code much easier to maintain.

...R

Unfortunately you're being caught out by a pretty poor choice of names by the people who designed the Arduino runtime framework.

'void loop()' declares a function named loop - this name has been defined by the Arduino framework and you are required to provide one function named loop() with that signature. Despite the name, this is NOT a loop, it's a function. I can't follow the train of thought that lead the developers to think this was a good choice of name, given how easily it confuses people unfamiliar with C++ - which are exactly the people that Arduino is aimed at.

There are various control structures that you can use in C++. Some of these allow the code to loop:
FOR
WHILE
DO/WHILE

These are ways of implementing loops. You would use them within a function.

Thank you Peter, and Robin. I'm having a lot of fun learning this and your help is greatly appreciated.
regards,
jim