Decrement to 0 and back up again

Hello. I am very new to programming logic. I know some Javascript, but more DOM manipulation and dealing with arrays & objects rather than heavy logic. I am trying something that should be simple, but I'm having an issue working out the logic. I am hoping someone here may help.

I am trying to blink a series of 3 lights. The function that blinks the light contains a for loop. int on = 1000ms and int off=200ms. At the end of the function the on variable is decremented by 25, so that when the next light starts blinking the on value is now 975ms, and so on until it hits 0.

What I want to do is when I get to 0 increment back up to the original 1000, and then back down again. I'm just lost on the logic I would need to make that happen. I hope this isn't a stupid question. I'm just looking for ways to improve my logical thinking. Thank you in advance.

Here's an example of the function:
1
2
3
4
5
6
7
8
9
10
void loopLights(int iterations, int pin, int reduce) {
  int x = iterations;

  for(int i = 0; i < x; i++) {
    digitalWrite(pin, HIGH);
    delay(on);
    digitalWrite(pin, LOW);
    delay(off);
  }
}
Last edited on
I am trying to blink a series of 3 lights. The function that blinks the light contains a for loop. int on = 1000ms and int off=200ms. At the end of the function the on variable is decremented by 25, so that when the next light starts blinking the on value is now 975ms, and so on until it hits 0.


That could look something like this
1
2
3
4
5
6
7
8
9
10
11
12
13
int const step_size = 25;
int increment = -step_size;
int on_duration_ms = 1000;
 
do
{
  digitalWrite(pin, HIGH); 
  delay(on_duration_ms); 
  if (on_duration_ms <= step_size) increment = -increment;
  on_duration_ms += increment;
  digitalWrite(pin, LOW);
  delay(200); 
} while (on_duration_ms <= 1000); 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const int ledPin =  13;

int ledState = LOW;
int interval = 25;

int on{5};
int off{1000};

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  
    ledState = HIGH;
    digitalWrite(ledPin, ledState);
    delay(on);

    ledState = LOW;
    digitalWrite(ledPin, ledState);
    delay(off);

    off -= interval;
    
    if(off == 0)
      off = 1000;

  }
To produce max to zero and back to max again, start with a value twice the size of max. For i=2*max i>0; i-=step, produces a value range from 2*max to step. When you subtract max from this, you get a range from max down to -(max-step). The absolute value of this range results in a range from max down to 0 and back up to max-step. It is important that we do not get back to max, but indeed stop at max-step (max minus step). The next repetition of this range will produce a starting value of max, and had we ended the previous range in max, we would have two max values right after each other. For the same reason we need to have one last max value after the last loop, this of course is no issue if the loop is meant to run indefinitely, in which case the outer for loop should be replaced with while(1).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

int main(void)
{
int bounceMaxValue = 100;
int valueStep = 25;
int bounceCount = 3;
int internalMaxValue = bounceMaxValue * 2;
int loopRepeatCount = (internalMaxValue / valueStep) * bounceCount + 1;

std::cout << "We bounce from " << bounceMaxValue << " to 0 and back again " << bounceCount << " times." << std::endl;
std::cout << "Producing a total of " << loopRepeatCount << " numbers." << std::endl;

for( int bi=0; bi<bounceCount; bi++ )
	{
	for( int vi=internalMaxValue; vi>0; vi-=valueStep )
		{
		std::cout << abs(vi-bounceMaxValue) << std::endl;
		}
	}
std::cout << bounceMaxValue << std::endl;

return 0;
}
The previous produces a sawtooth pattern, this produces a triangle pattern.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const int ledPin =  13;

int ledState = LOW;
int interval = 25;

int on{5};
int off{1000};

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  
    ledState = HIGH;
    digitalWrite(ledPin, ledState);
    delay(on);

    ledState = LOW;
    digitalWrite(ledPin, ledState);
    delay(off);

    off -= interval;
    
    if( (off == 0) || (off == 1000) )
    {
      interval = -interval;
    }
  }
Topic archived. No new replies allowed.