howto fade lcd backlight with pwm when input goes low.

Hi

i been trying to fade an lcd backlight when an inferred sensor picks up movement, the sensors pin goes low.

The code from my sketch

void Movement(){
  if(digitalRead(movementSensor)==HIGH)  
    {
    //lcdBrightness = 255;
    analogWrite(pwmLCD, 255);
    }
  
  else
    {
    //lcdBrightness--;
    //lcdBrightness >= 0;    
    //for (int i=255; i >= 0; i--)   
    analogWrite(pwmLCD, 0);
    } 
}

If i take out the // this works but it just makes the lcd fade and start from the beginning and fade constantly, i would like it to stop at 0 and fade back on when the sensor pin goes HIGH again.

any suggestions would be appreciated :)

-G

First off, Movement() is a lousy function name. Does it detect movement? Does it implement movement? Does it measure movement? A function name should contain both a noun and a verb, unless the noun is very clear. In keeping with the Arduino convention, it should also with a lower case letter.

In you case, detectAndReactToMovement() would be a better name.

You determine if there has been movement, and react the same whether the state of the sensor is the same as last time, or if it is different.

To solve your problem, you need to separate the detection of and the reaction to movement, and only trigger the reaction if the state (movement detected or movement not detected) has changed.

Create two variables (global or static), thisState and lastState. Set thisState to the value returned by digitalRead.

void detectAndReactToMovement()
{
   thisState = digitalRead(movementSensor);
   if(thisState != lastState) // Do something only when the state changes
   {
      if(thisState == HIGH)
      {
         // Turn the light on
      }
      else
      {
         // Fade the light off
      }
   }
   lastState = thisState;
}

Thanx heaps Paul

You'd probably have a heart attack if you seen the rest of my code, (in my defense it works) Just. I'll give what you suggested a go soon and post some results.

-G

Ok Paul
i can see now this just cleaned up the mess i made but your code hasn’t addressed my issue, i am wanting to fade the displays back light from 255 to 0 when the state changes or vice verser .

So when the state is LOW the pwm-output stays at 0 then if the state goes high the lcd will light up slowly to full pwm-output 255.

The sample code only shows how to fade between constantly

void loop()  {
  // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);        
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  }

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);        
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  }
}

I don’t understand how to fade out and wait till the state changes

Any ideas :~

-G

I don't understand how to fade out and wait till the state changes

The stuff that happens in loop() happens every pass through loop.

If you call detectAndReactToMovement() in loop(), and put the code to fade the LCD backlight in that function at the appropriate point, the function will only invoke the fading at the appropriate time.

Simple, now i understand. Thank you for explaining it to me Paul.

-G

I have only just had a chance to look into this and i am still having trouble with the fading part.
You say that only when the state changes the command to fade only gets called once so then how do i iterate though the fade code?

I tried making another function to do the fading but that only gets called once also, i need to somehow call the function to fade and have it loop over it until it gets to 0 then finishes , until the state changes again.

int i = 255; 
         i--;
         analogWrite(lcdBacklight, i);

Help :frowning:

int i = 255; i--; analogWrite(lcdBacklight, i);

Is that code missing a "for" somewhere?

Your probably right AWOL, im i newbie to the programming world. Not really my issue tho, It been able to loop over code until the code becomes false and been able to call the code you want to loop over from a the state change that PaulS showed me.

Did you figure it out yet? Im still new to programming the arduino myslef, but i would think that you could start by setting the variable at zero, then put an if statement at the beginning of the code stating that the backlight brightness can only change when the value is either 0 or 255, so it would read the first state since the variable is already ate zero, start fading/brightening to 255, and wouldnt start fading to the other direction untill it has fully faded to the other when it is either 0 or 255. Just a thought :P