How to automatically turn off the LCD backlight like a cellphone

Guys can u give me some tips on how to do this..

If you power the backlight from a PWM output you can change the brightness with an analogWrite() call.

It's then up to your sketch to work out when to do what with it though.

One idea:

When you press a button, set a counter to a value to define how long to stay on for (say 10000) and turns the backlight to full brightness. Every button press resets the value to 10000 again.

Your main loop decrements that counter by 1. When the counter reaches 0 the light goes off.

You could have intermediate stages so when the counter reaches say 50% of the max value the light goes dim.

majenko: If you power the backlight from a PWM output you can change the brightness with an analogWrite() call.

It's then up to your sketch to work out when to do what with it though.

One idea:

When you press a button, set a counter to a value to define how long to stay on for (say 10000) and turns the backlight to full brightness. Every button press resets the value to 10000 again.

Your main loop decrements that counter by 1. When the counter reaches 0 the light goes off.

You could have intermediate stages so when the counter reaches say 50% of the max value the light goes dim.

wow tnx ill try that one

switch(pressCount)
  
  
  
   {
     case 0:
  
  if (currState == HIGH){
  for(int fadeValue = 250 ; fadeValue >= 0; fadeValue -=1)
  { 
    analogWrite(lcdLight, fadeValue);         
    delay(100);                
  
  }}
  
    
            lcd.setCursor(0,0);
             lcd.print("Automated");
             lcd.setCursor(0,1);
             lcd.print("Plant System");

      break;

okay the lcd turns off after a while but... i still wait for the process to end before i can switch to another case... how to fix this pls

You handle the light control outside of the switch.

And not in its own loop, either. Something like this:

void loop()
{
  static int illuminate = 0;

  readSwitches(); // Whatever you do to read your inputs

  switch(switchPressed)
  {
    case 0:
      illuminate = 10000;
      analogWrite(lcdLight,255);
      // Do whatever you need to for this switch
      break;
    case 1:
      illuminate = 10000;
      analogWrite(lcdLight,255);
      // Handle this switch
      break;
  }
  if(illuminate>0)
  {
    illuminate--;
    if(illuminate == 5000)
    {
      analogWrite(lcdLight,128);
    }
  } else {
    analogWrite(lcdLight,0);
  }
}

Of course, you'll need to do other things to read the switches, and handle their operations, but you get the gist.

waaaaa....

need more example pls...

still getting the same problem...

the delay() function still blocking the program...

and im not good in blinkwithout delay example...

=(

the delay() function still blocking the program...

and im not good in blinkwithout delay example...

Then you are in a lot of trouble....

Don

yah i know....can anyone help me on this one...

majenko: If you power the backlight from a PWM output you can change the brightness with an analogWrite() call.

If powering the backlight direct from an Arduino pin, check that the backlight current requirement is no more than 40mA, or else use a series resistor of 100 ohms or more. Otherwise, use a transistor to switch it.

Hi, I have tested through transistor BC557 and it is working fine . http://www.nxp.com/documents/data_sheet/BC556_557.pdf Transistor pin2(base)-------/\/\/---1k------arduino pin 13 (with 1K registor) Transistor pin3(Collector)----arduino +5V Transistor pin1(Emmiter)-----LCD Backlight + pin(15)

You can safely use the arduino pin.

Thanks

Yes, BC557 (high side switch) is OK if the backlight takes no more than 100mA. BC327 (PNP, high side switch) or BC327 (NPN, low side switch) is OK at higher currents too.

Yes you much check the LCD model how much current it required for backlight, I have used it < 100mA.