New with arduino. Need help with making one LED turn on slowly while another turns off at the same time

Hi, I am new to using Arduino. For a school subject, I need to make 1 led go off slowly while another one comes on at the same time.

I have used the following code:

int greenLedPin = 10;
int yellowLedPin = 9;

void setup() {
pinMode(greenLedPin, OUTPUT);
pinMode(yellowLedPin, OUTPUT);
}

void loop() {
for (int brightness=0; brightness <256; brightness++){
analogWrite(greenLedPin, brightness);
analogWrite(yellowLedPin, 255-brightness);
delay(10);
}

But according to my teacher, this code causes them to fade alternately. Can anyone help me to ensure that they do fade at the same time. One fades out while the other fades in at the same time.

Welcome to the forum and thank you for posting your code in code tags which many newcomers don't

I have moved your topic to the Programming category of the forum as it is more relevant to your question

Did you try?

Hello dvnxtshnz

Welcome to the worldbest Arduino forum ever.

Consider:

int greenLedPin = 10;
int yellowLedPin = 9;

void setup()
{
  pinMode(greenLedPin, OUTPUT);
  pinMode(yellowLedPin, OUTPUT);
}

void loop()
{
  for (int brightness = 0; brightness < 256; brightness++)
  {
    analogWrite(greenLedPin, brightness);
    analogWrite(yellowLedPin, 255 - brightness);
    delay(10);
  }
  for (int brightness = 0; brightness < 256; brightness++)
  {
    analogWrite(greenLedPin, 255-brightness);
    analogWrite(yellowLedPin, brightness);
    delay(10);
  }
}

Have a nice day and enjoy coding in C++.

Have you tried it on an actual Arduino?

It looks to me like that is exactly what will happen - for each iteration of the for loop, the green LED drive should increase by 1, and the yellow LED drive should decrease by 1.

The relation between the drive numbers 0..255 and the "brightness" as perceived by the human eye may be non-linear...

yes I tried it on arduino and because it's so fast it looks like they fade alternately

but I wonder if that is what my code says, because my teacher claims that my code says that they have to fade in and out while the assignment of the course I'm taking is that one fades out while the other fades in at the same time

yes I tried it on arduino, but my teacher claims that my code tells them to fade in and out in turn, whereas the assignment of the course I'm following is that one fades out while the other fades in at the same time

simply increase the delay to better see what happens.

1 Like

thank you, but I tried this code before, but then 1 led will stay on while the other slowly fades in and out and then vice versa, but the instruction is for 1 led to slowly turn on while the other slowly fades out at the same time or is that not technically possible?

Try this way.

"LEDduplo - Wokwi ESP32, STM32, Arduino Simulator

there is another factor to consider.
LED brightness is not linear to the PWM value.

To have a smooth ramp from 0 to 255, you’ll need a lookup table or function to give the correct dimming profile along that curve.

1 Like

You could do it hardware. When one is on the other is off.

image

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