speeding up timer in running light

Hi All,

I'm fairly new to arduino, so don't get to complicated on me. :confused:

The aim is to make some LED's run from left to right in a loop.
no problem there.

but I want the speed to increase after each loop and decrease again, so that the motion speeds up and slows down in predefined steps.

How to attack this one?

thanks
Carlos

How to attack this one?

First post the code that you have, following the guidelines for posting code in the "how to use this forum-please read" sticky. Then we can help to make the code work like you want.

The code is

int timer = 100;           // The higher the number, the slower the timing.

void setup() {
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 3; thisPin < 13; thisPin++) {
    pinMode(thisPin, OUTPUT);
  }
}

void loop() {
  // loop from the lowest pin to the highest:
  for (int thisPin = 3; thisPin < 13; thisPin++) {
    // turn the pin on:
    digitalWrite(thisPin, HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(thisPin, LOW);
  }

  // loop from the highest pin to the lowest:
  for (int thisPin = 12; thisPin >= 3; thisPin--) {
    // turn the pin on:
    digitalWrite(thisPin, HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(thisPin, LOW);
  }
}

So it's the 'timer' value the determines how fast the LEDs move. Change 'timer' in the pattern of your choice.

Maybe you could put each cycle in a function and call the function with different values of timer.

Like this:

void setup()
{
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 3; thisPin < 13; thisPin++)
  {
    pinMode(thisPin, OUTPUT);
  }
}


void loop()
{
  // Speed up over 25 cycles, delays of 100 to 50
  for (int timer = 100; timer > 50; timer -= 2)
  {
    oneCycle(timer);
  }


  // Slow down over 25 cycles, delays of 50 to 100
  for (int timer = 50; timer < 100; timer += 2)
  {
    oneCycle(timer);
  }
}


void oneCycle(int delayTime)
{
  // loop from the lowest pin to the highest:
  for (int thisPin = 3; thisPin < 13; thisPin++)
  {
    // turn the pin on:
    digitalWrite(thisPin, HIGH);
    delay(delayTime);
    // turn the pin off:
    digitalWrite(thisPin, LOW);
  }


  // loop from the highest pin to the lowest:
  for (int thisPin = 12; thisPin >= 3; thisPin--)
  {
    // turn the pin on:
    digitalWrite(thisPin, HIGH);
    delay(delayTime);
    // turn the pin off:
    digitalWrite(thisPin, LOW);
  }
}

You could put the predefined times in an array. This allows any times that you want (not necessarily even multiples nor sequential order). In loop make an index variable the increments each time through loop. Use the index to select which element of the array gets put into the timer variable. When the index reaches the end of the array reset the index to 0 to repeat.

unsigned int times[] = {100, 200, 300, 400, 500, 400, 300, 200, 100};
int numberOfTimes;
int timer = 100;           // The higher the number, the slower the timing.

void setup()
{
   Serial.begin(115200);
   numberOfTimes = sizeof(times) / sizeof(times[0]);  
   // use a for loop to initialize each pin as an output:
   for (int thisPin = 3; thisPin < 13; thisPin++)
   {
      pinMode(thisPin, OUTPUT);
   }
}

void loop()
{
   static int index = 0;
   timer = times[index];
   index++;
   if (index >= numberOfTimes)
   {
      index = 0;
   }
   // loop from the lowest pin to the highest:
   for (int thisPin = 3; thisPin < 13; thisPin++)
   {
      // turn the pin on:
      digitalWrite(thisPin, HIGH);
      delay(timer);
      // turn the pin off:
      digitalWrite(thisPin, LOW);
   }

   // loop from the highest pin to the lowest:
   for (int thisPin = 12; thisPin >= 3; thisPin--)
   {
      // turn the pin on:
      digitalWrite(thisPin, HIGH);
      delay(timer);
      // turn the pin off:
      digitalWrite(thisPin, LOW);
   }
}

Thanks, I'll give it a try.

Works like a charm.

Thanks for all the help

:smiley: