Counter to Control a For Loop

Hi, in one of my arduino projects I have a RGB LED that changes output based on color and pwm level. This is controlled by a "for" loop that reads like this...

for(x = 255; x >= 0; x = x - 15){
            for(y = 255; y >= 0; y = y - 15){
                  for(z = 255; z >= 0; z = z - 15){.......}

For this particular project I need that each step within the "for" loop lasts during a number of readings of a light sensor (not elapsed time in itself). For this I included a reading counter at the sensor code in order to control the passage through each color/pwm step. So far I cannot figure out how to set this "counter" as a condition to trigger the color/pwm passage change I need. I would appreciate very much help in this matter...thank you !!

[sterretje edit] code tags fixed.

Like this?

if(count - lastCount >= interval){
   lastCount = count;
   ...
}

It sounds like it is the same sort of one-shot edge-detection mechanism as in these:

1 Like

As I did see an attempt to use code tags, I've fixed them for you. Code tags are three back ticks (```) before and after the code, not four normal ticks.

  • Show us your attempt at doing this so we can see where your logic may be failing.

  • Make sure your x, y, and z are int.


for(x = 255; x >= 0; x = x - 15){
            for(y = 255; y >= 0; y = y - 15){
                  for(z = 255; z >= 0; z = z - 15){.......}

Use the counter, not loop, to increase/decrease your PWM value. In this example, I am only printing the increasing read count. You could change the PWM.

int readCount, readMax = 9;

void setup() {
  Serial.begin(115200);
}

void loop() {
  if (readCount > readMax) {
    readCount = 0;
    Serial.println();
  }

  changePWM();

  readCount++;
}

void changePWM() {
  Serial.print(readCount);
  Serial.print(" ");
  delay(200);
}

... and a fancy example to slew through the colors using millis() / micros() as a timer...

Side note: when not doing increment ++ or decrement --, there's less visual noise and less chance of a typo by using compound assignment

for (x = 255; x >= 0; x -= 15) {

Also, unless you're modifying an existing variable with a loop and need that final value that exited the loop later, use a variable declared by the loop as the counter. It increases code locality. For example, here is a variation where a problem can be seen, just by itself

This is true even if you have (for example) several x-y-z loops in the same function. The only benefit of declaring the variable type just once is saving a bit of typing, maybe.

You should really post your complete sketch. I do not understand what you are tyring to explain.

You should explain in normal words what functionality you want to have because

Which indicates that you have no idea how to realise
or
you have a misconception how the code works

So please explain in plain simple everyday words what functionality you want to have.

I give an example

let's say counting up LED-number and brightness has just started

LED-number is 1 brightness is lowest with value 1 too

There is a light-sensor.
Reading the light-sensor sometimes takes 3 seconds, sometimes 2 minutes
So the light-sensor shall allways do 5 readings
which takes sometimes 15 seconds, sometimes 10 minutes
only when 5 readings of the light-sensor are done increment brightness by 1
when brightness reached the maximum-value increase LED-number by one

I am very sure that this is a wrong description but it shows the principle and as it is so wrong I hope it makes you understan how much room for wrong interpretations your initial posting is giving

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.