1

I have two LEDs an I want the first one getting linear brighter and the second one do the exact same thing just one ore two seconds later. When one LED has reached 100, it should getting darker until there's no light and then start again from the beginning. I#m trying now for quite a long time but couldn't get it going.

int LED = 11;
int LED2 = 10;
void setup() {

Serial.begin(9600);
}
int x;
int x2;
void loop() {

for(x = 0; x<=100;x++) 
{
while(x > 30)
{
for(x2 = 0; x2<=100;x2++)
{
analogWrite(LED2,x2);
delay(100); 
}
}

analogWrite(LED,x);
delay(100);
Serial.println(x);

}
}

I just wanted to do the beginning but it isn't working. Can somebody pleas help me?

2
  • I'd be inclined to use sin and cos to create a sinusoidal brightness profile. Loop from 0 to 2xPI at whatever increment you want (maybe use a DEG2RAD conversion so you can work in degrees) and cycle around a circle. Use sin to calculate the brightness of one LED ((sin(angle) + 1) * 50.0) and cos for the other. The two LEDs will then be 90° apart around the circle.
    – Majenko
    Jul 13 '17 at 22:07
  • @gamma, what about a graph that shows the brightness of both leds in time. I'm still confused what you want to achieve. Do you want other patterns as well ?
    – Jot
    Jul 14 '17 at 11:30
1

Note: I think you will need to adjust the timings.

Hope this help:

int LED = 11;
int LED2 = 10;

unsigned long start;

int lum = 0;  // Light level LED
int step = 1;
int lum2 = 0; // Light level LED2
int step2 = 1;

void setup() {
  Serial.begin(9600);
  start = millis();
}

void loop() {
  unsigned long now = millis();

  if (now % 100 == 0) {
    // Every 100 ms, light goes up/down 1 notch.
    light(LED, &lum, &step);

    if (now - start > 2000) {
      //  Time to start with LED2
      light(LED2, &lum2, &step2);
    }
  }
}

/*
 * Light up/down one notch on a pin.
 */
void light(int pin, int* p_lum, int* p_step) {
  analogWrite(pin, *p_lum);  
  *p_lum += *p_step;

  if (*p_lum == 100) {
    *p_step = -1;
  }
  if (*p_lum == 0) {
    *p_step = 1;
  }
}
4
  • Thanks for your help. This is not exactly what I ment. The first one should start an go up and while thise is happening the second one should go up. So the second LED should do the same thing like the first one but two seconds later.
    – gamma
    Jul 13 '17 at 19:22
  • @gamma. See new version
    – user31481
    Jul 13 '17 at 19:41
  • Well I'm not quite sure if this is it. Anyway thanks for helping. I wanted it to go up and down like tw for loops:
    – gamma
    Jul 13 '17 at 20:41
  • Sorry for that. What I wanted to say was: I wanted it to go up and down like tw for loops: for(x = 0; x<=100; x++) { analogWrite(LED,x); delay(100); } for (x = 100; x>=0;x--) { analogWrite(LED;x) delay(100) } but this with two LEDs and the second one starts about 2 seconds later and ends about 2 seconds later. Your code almost does that but I think it looks like a little bit different.
    – gamma
    Jul 13 '17 at 20:47
0

Here is one way to do it.

  1. Set up two output compare channels. They are identical except that they have slight different periods.
  2. In their respective isr, flip the led

The next effect is that the duty cycle on the led goes from 0 to 100 and then 0 and then ....

The periods determine how long the 0 to 100 to 0 cycle is. The period differential determines how fast the led goes on and off.

You can do that to the other led, either in the same isr or through two other channels.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.