simple C question

What I want to do is control a loop with 2 independent variables (x and y for instance). Something like this.

** for (int x=0,int y=0; x<=100 && y<=255; x++, y++){**
analogWrite(PWMpin13, x);
analogWrite(PWMpin12, y);
delay(10);
analogWrite(PWMpin13, 0);
analogWrite(PWMpin12, 0);
}

I want one write sequence to max out at 100 (x) and a seperate write sequence to max out at 255 (y).
I know there is a way to do this, but everything I try either errors or doesn't work correctly. It's probably some silly syntax mistake.

I appreciate any help you can offer.

XD

All the variables declared in the for statement need to be the same type. So, the for statement looks like:

   for (int x=0, y=0; x<=100 && y<=255; x++, y++){

I will give it a try. Thanks!

I suspect your loop will exit once x reaches 100
you'll need 2 loops to achieve separate end conditions

mmcp42:
I suspect your loop will exit once x reaches 100
you'll need 2 loops to achieve separate end conditions

Actually, if multiple end-conditions are needed in a single loop (that is, you need two or more variables to progress from a starting point to an ending point, all at the same time), it can be done by using a variant of the Bresenham Algorithm:

:slight_smile:

well basically what I am trying to do is light 2 different pins of the same RGB LED to 2 different intensities. When I try and make yellow (green + red) there is too much red. So what I want is green at 100% and red at 75%. It needs to be done in the same time frame which is why I want a multi variable loop. I will definately try these suggestions out. Thanks

Bradmdickerson:
well basically what I am trying to do is light 2 different pins of the same RGB LED to 2 different intensities. When I try and make yellow (green + red) there is too much red. So what I want is green at 100% and red at 75%. It needs to be done in the same time frame which is why I want a multi variable loop. I will definately try these suggestions out. Thanks

Using the Bresenham algorithm should do it; you are essentially wanting to "plot a line" from 0,0 to 100,75 (ok, in PWM/Arduino-land, 0,0 to 255,192) - this is what the Bresenham algorithm can accomplish. As noted before, its possible to extend it to "n" variables (with a bit of thought and creativity).

:slight_smile:

Well, I imagine that I'm doing the same thing, but the psudo code used in that wiki article is harder to understand then C, so I didn't really try to dig through it.

Here is how I would do it, with 75% pwm on the first pin.

for(byte x = 0; x < 192; x++)
{
analogWrite(RedLEDPin, x);
analogWrite(GreenLEDPin, (x*4/3));
delay(10);
}

You don't need to set the PWM to 0 each loop. 191 is ~75% of 255, not just a random number.

That code will ramp the brightness up from off, while keeping the red at 75%. This is what your original code implied. However, if ALL you want to do is have it shine yellow, you only need to do this:

analogWrite(RedPin, 191);
analogWrite(GreenPin, 255);
delay(1000);
analogWrite(RedPin, 0);    //These two lines turn the LEDs back off, and can be omitted, if that is not desired.
analogWrite(GreenPin, 0);

That works great. Thanks everybody! I'll have a video of my project up soon.