LED Dimming Program Question

Hello,

I am beginner on learning arduino code. I got some problem on programming LED dimming. I want to build a LED dimming system. LED will follow Photoelectric sensor range.

For example, if Photoelectric sensor range between 20 ~ 200, LED will fade in until analog pwn value = 100. If Photoelectric sensor range below 20, LED will fade out until analog pwn value = 50.

The problem is that LED just fade in. It can not fade out. do i go something wrong on my arduino code?

int pwmPin = 3; // LED pin
#define pot1 A0 // Photoelectric sensor

int brightness1 = 50 ;
int fadeAmount = 5 ;



void setup()  // setup loop
{
  pinMode(pwmPin, OUTPUT);
  pinMode(pot1, INPUT);

  Serial.begin(9600);
}



void loop()
{

  int  value1 = analogRead(pot1);

  Serial.println(value1);// show sensor value


  if ( value1 <= 200 && value1 >= 20)
  {
    //LED fade in 
    for (brightness1; brightness1 <= 50 ; brightness1 = brightness1 + fadeAmount )
    {
      analogWrite(pwmPin, brightness1);
      delay(500);

    }


  }
  else if (value1 < 20)
  {
     //LED fade in 
    for (brightness1; brightness1 <= 50 ; brightness1 = brightness1 - fadeAmount )
    {
      analogWrite(pwmPin, brightness1);
      delay(500);

    }

  }

}

I wuold change the order of your if/else statement, like this:

(compiles, NOT tested)

int pwmPin = 3; // LED pin
#define pot1 A0 // Photoelectric sensor

int brightness1 = 50 ;
int fadeAmount = 5 ;



void setup()  // setup loop
{
  pinMode(pwmPin, OUTPUT);
  pinMode(pot1, INPUT);

  Serial.begin(9600);
}



void loop()
{

  int  value1 = analogRead(pot1);

  Serial.println(value1);// show sensor value

  if (value1 < 20)
  {
    //LED fade in
    for (brightness1; brightness1 > 50 ; brightness1 = brightness1 - fadeAmount )
    {
      analogWrite(pwmPin, brightness1);
      delay(500);

    }

  }
  else if ( value1 <= 200)
  {
    //LED fade in
    for (brightness1; brightness1 < 100 ; brightness1 = brightness1 + fadeAmount )
    {
      analogWrite(pwmPin, brightness1);
      delay(500);

    }


  }


}

hope that helps…

Does it work if you adjust the setting of the potentiometer then press the reset button on the Arduino?
You should probably reset the value of brightness1 if the value of the potentiometer changes between iterations of the loop().

Your mistake is in the fade down ‚Äėfor‚Äô loop:

    for (brightness1; brightness1 <= 50 ; brightness1 = brightness1 - fadeAmount )
    {
      analogWrite(pwmPin, brightness1);
      delay(500);
    }

You are only fading down while the brightness is already BELOW 50. Don’t you want to fade down while the brightness is ABOVE 50?

Thank you everyone’s suggestion! I finally can make it, but the code is so massive. It can fade in and fade out.

int pwmPin = 3;
#define pot1 A0

int brightness1 = 50 ;
int fadeAmount = 5 ;



void setup()  // setup loop
{
  pinMode(pwmPin, OUTPUT);
  pinMode(pot1, INPUT);

  Serial.begin(9600);
}


void loop()
{

  int  value1 = analogRead(pot1);


  Serial.println(value1);


  if ( value1 <= 200 && value1 >= 20)
  {
    if ( brightness1 == 50)
    {
      brightness1 = brightness1 + fadeAmount;
      analogWrite(pwmPin, brightness1);
      delay(500);
      brightness1 = brightness1 + fadeAmount;
      analogWrite(pwmPin, brightness1);
      delay(500);
      brightness1 = brightness1 + fadeAmount;
      analogWrite(pwmPin, brightness1);
      delay(500);
      brightness1 = brightness1 + fadeAmount;
      analogWrite(pwmPin, brightness1);
      delay(500);
      brightness1 = brightness1 + fadeAmount;
      analogWrite(pwmPin, brightness1);
      delay(500);
      brightness1 = brightness1 + fadeAmount;
      analogWrite(pwmPin, brightness1);
      delay(500);
      brightness1 = brightness1 + fadeAmount;
      analogWrite(pwmPin, brightness1);
      delay(500);
      brightness1 = brightness1 + fadeAmount;
      analogWrite(pwmPin, brightness1);
      delay(500);
      brightness1 = brightness1 + fadeAmount;
      analogWrite(pwmPin, brightness1);
      delay(500);
      brightness1 = brightness1 + fadeAmount;
      analogWrite(pwmPin, brightness1);
      delay(1000);
    }

  }



  else if (value1 < 20)
  {

    if ( brightness1 == 100)
    {
      brightness1 = brightness1 - fadeAmount;
      analogWrite(pwmPin, brightness1);
      delay(500);
      brightness1 = brightness1 - fadeAmount;
      analogWrite(pwmPin, brightness1);
      delay(500);
      brightness1 = brightness1 - fadeAmount;
      analogWrite(pwmPin, brightness1);
      delay(500);
      brightness1 = brightness1 - fadeAmount;
      analogWrite(pwmPin, brightness1);
      delay(500);
      brightness1 = brightness1 - fadeAmount;
      analogWrite(pwmPin, brightness1);
      delay(500);
      brightness1 = brightness1 - fadeAmount;
      analogWrite(pwmPin, brightness1);
      delay(500);
      brightness1 = brightness1 - fadeAmount;
      analogWrite(pwmPin, brightness1);
      delay(500);
      brightness1 = brightness1 - fadeAmount;
      analogWrite(pwmPin, brightness1);
      delay(500);
      brightness1 = brightness1 - fadeAmount;
      analogWrite(pwmPin, brightness1);
      delay(500);
      brightness1 = brightness1 - fadeAmount;
      analogWrite(pwmPin, brightness1);
      delay(1000);

    }

  }

but the code is so massive

So, put the repetitive code inside a loop.

aarg:
So, put the repetitive code inside a loop.

Thank you for your rely! Is it like this?

int pwmPin = 3;
#define pot1 A0

int brightness1 = 50 ;
int fadeAmount = 5 ;



void setup()  // setup loop
{
  pinMode(pwmPin, OUTPUT);
  pinMode(pot1, INPUT);

  Serial.begin(9600);
}



void loop()
{

  int  value1 = analogRead(pot1);


  Serial.println(value1);


  if ( value1 <= 200 && value1 >= 20)
  {
    if ( brightness1 == 50)
    {
      void fadein();
    }

  }



  else if (value1 < 20)
  {

    if ( brightness1 == 150)
    {

      void fadeout();

    }

  }

}



void fadein()
{
  brightness1 = brightness1 + fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 + fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 + fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 + fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 + fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 + fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 + fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 + fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 + fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 + fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 + fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 + fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 + fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 + fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 + fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 + fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 + fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 + fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 + fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 + fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
}

void fadeout()
{
  brightness1 = brightness1 - fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 - fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 - fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 - fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 - fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 - fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 - fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 - fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 - fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 - fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 - fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 - fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 - fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 - fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 - fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 - fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 - fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 - fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 - fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);
  brightness1 = brightness1 - fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(1000);
}
1 Like

Moving the repetitive code to a function does not make the code less massive.

Your code is massive because you are writing these same three lines twenty times.

  brightness1 = brightness1 + fadeAmount;
  analogWrite(pwmPin, brightness1);
  delay(500);

If you want to repeat something 20 times, use a ‚Äėfor‚Äô loop:

  for (int i=0; i < 20; i++)
  {
    brightness1 = brightness1 + fadeAmount;
    analogWrite(pwmPin, brightness1);
    delay(500);
  }

Now your 60 lines are reduced to six. Much less massive.

johnwasser:
Moving the repetitive code to a function does not make the code less massive.

Your code is massive because you are writing these same three lines twenty times.

  brightness1 = brightness1 + fadeAmount;

analogWrite(pwmPin, brightness1);
 delay(500);




If you want to repeat something 20 times, use a 'for' loop:


for (int i=0; i < 20; i++)
 {
   brightness1 = brightness1 + fadeAmount;
   analogWrite(pwmPin, brightness1);
   delay(500);
 }



Now your 60 lines are reduced to six. Much less massive.

Thank you for your suggestion! It works !

int pwmPin = 3;
#define pot1 A0

int brightness1 = 10 ;
int fadeAmount = 5 ;

void setup()  // setup loop
{
  pinMode(pwmPin, OUTPUT);
  pinMode(pot1, INPUT);

  Serial.begin(9600);
}

void loop()
{

  int  value1 = analogRead(pot1);

  Serial.println(value1);

  if ( value1 <= 200 && value1 >= 20)
  {
    if ( brightness1 == 10)
    {
      for (int i = 0 ; i < 10; i++)
      {
        brightness1 = brightness1 + fadeAmount;
        analogWrite(pwmPin, brightness1);
        delay(500);
      }
    }
  }

  else if (value1 < 20)
  {
    if ( brightness1 == 60)
    {
      for (int i = 0 ; i < 10; i++)
      {
        brightness1 = brightness1 - fadeAmount;
        analogWrite(pwmPin, brightness1);
        delay(500);
      }
    }
  }
}

hello, I'm in the same situation, but with different codes. I've been trying to break up combinations all the time there's something I missed. thank you for your advice and for your help.

const int led = 6;                // the PWM pin the LED is attached to
const int BUTTON_PIN = 12;          // the number of the pushbutton pin
const int SHORT_PRESS_TIME = 500;  // 500 milliseconds

int brightness = 0;               // how bright the LED is
int fadeAmount = 5;               // how many points to fade the LED by
int lastState = LOW;                // the previous state from the input pin
int currentState = LOW;             // the current reading from the input pin

unsigned long pressedTime  = 0;
unsigned long releasedTime = 0;

// the setup routine runs once when you press reset:
void setup() {

  // open the serial port:
  Serial.begin(9600);
  Serial.print("start");

  // nano control
  pinMode(led, OUTPUT);
  pinMode(BUTTON_PIN, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {

  // read the state of the switch/button:
  lastState = digitalRead(BUTTON_PIN);

  if (lastState == HIGH && currentState == LOW) {      // button is pressed
    pressedTime = millis();
    currentState = HIGH;
  }
  else if (lastState == HIGH && currentState == HIGH) {  //  pressed after released
    pressedTime = millis();
    currentState = LOW;
  }
  else if (lastState == LOW && currentState == HIGH) {  // button is released
    releasedTime = millis();
  }


  if  (pressedTime < SHORT_PRESS_TIME)  {
    digitalWrite(led, currentState);                     // turn on/off LED
  }
  else if  (pressedTime > SHORT_PRESS_TIME)  {
    analogWrite(led, brightness);                 // turn brightness LED

    // change the brightness for next time through the loop:
    brightness = brightness + fadeAmount;

    // reverse the direction of the fading at the ends of the fade:
    if (brightness == 0 || 255) {
      fadeAmount = -fadeAmount;
    }

  }
  else if (releasedTime = 60000) {
    digitalWrite(led, LOW);                     // turn on/off LED
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(30);
}