Dimming a LCD display

I have been trying to work on dimming the LCD display using the Up(U) and Down(D) buttons on a LCD keypad shield.

I have got it to work so its dims from off to fully on. but when i’m trying to print it to the LCD screen as a percentage it does some wack stuff with the numbers(when your at 100 and you press down it goes to 960, then 920 ect). Im trying to figure out what im doing wrong, but i think i have been staring at it too long and cant see the problem.

Here is a snippet of the code.

Thanks

  {
    btn_push = ReadKeypad();
    WaitBtnRelease();
    if (btn_push == 'D')
    {
      backLight -= 10;
      if (backLight <= 0)
        backLight = 0;
    }
    else if (btn_push == 'U')
    {
      backLight += 10;
      if (backLight >= 255)
        backLight = 255;
    }
    analogWrite(backLightPin, backLight);
    Percentage = (backLight * 100) / 255;
    lcd.setCursor(0, 0);
    lcd.print("Up and down keys");
    lcd.setCursor(0, 1);
    lcd.print("Backlight:");
    lcd.print(Percentage);
    Serial.print(Percentage);
  }
}

godspeed:
I have been trying to work on dimming the LCD display using the Up(U) and Down(D) buttons on a LCD keypad shield.

What do you actually want? Simulate dimming by printing percentage values to LCD and Serial?

Or do you actualy want to dim the LCD backlight?
On which product? Is the hardware ready for backlight dimming? On most LCD Keypad Shields it is NOT.

What datatype is "backLight"?

when your at 100 and you press down it goes to 960, then 920 ect

If you print a two digit number where there was a three digit number, the last digit is not going to be overwritten. Why would you expect it to be?

(deleted)

jurs:
What do you actually want? Simulate dimming by printing percentage values to LCD and Serial?

Or do you actualy want to dim the LCD backlight?
On which product? Is the hardware ready for backlight dimming? On most LCD Keypad Shields it is NOT.

I want to display the percentage that the backlight has dimmed by on the LCD, and the backlight to physically dim. I was displaying the Serial before because I was having trouble with the buttons and I wanted to see what they were doing and was also just mucking around to see what the percentage was while the display show the 0-255, I just haven’t taken it out yet.

The Keypad shield is a DFRobot Link

It uses digital pin 10 for controlling the backlight so I use PWM.

aarg:
What datatype is “backLight”?

int

PaulS:
If you print a two digit number where there was a three digit number, the last digit is not going to be overwritten. Why would you expect it to be?

I would have thought that it would have shown 1 instead of a 9 since that was the last number? I guess that’s why it never crossed my mind?

spycatcher2k:
if (percentage<100)
{print" “}
if (percentage<10)
{print” "}

Psudo code, or add spaces before percentage
or print " " (2 Spaces) After percentage to remove left over digits.

Thanks very much ill give that a try tonight :smiley:

I would have thought that it would have shown 1 instead of a 9 since that was the last number? I guess that's why it never crossed my mind?

I don't understand that. Suppose that you had printed the values on two different lines. The value started at 100 and was decremented to 96
100
96

Now, it should be pretty obvious that, since you print them in the same place that
960
is what you are going to see.

PaulS:
I don't understand that. Suppose that you had printed the values on two different lines. The value started at 100 and was decremented to 96
100
96

Now, it should be pretty obvious that, since you print them in the same place that
960
is what you are going to see.

Oh right sorry me just being stupid.

I was thinking more along the lines of starting at 100 decremented down to 96

100
196

I can see where my small brain went wrong.

Thanks for clearing that up

So my next question would be how would i make it so that each time you press the up or down button it will increment 10% each time?I could do it by working out what value i need to put here:

backLight += 10

but i was wondering if there was some code that would do it?

So my next question would be how would i make it so that each time you press the up or down button it will increment 10% each time?

10% of what?

10% of the current value?

backLight *= 1.1;

10% of some range?

backLight += (max - min) * 0.1;

I was thinking so that if we start with 100% on when you press the down button it will go to 90% then 80%

I was wondering if there was a something like that map() function that would do it.

But I'm guessing i will have to just do some math?

I was thinking so that if we start with 100% on when you press the down button it will go to 90% then 80%

Don't think that way, because you are still not defining what the percentage is of.

If you start at n, and want to drop by 10% of the range 0 to 255, that is one amount. If you want to drop 10% of n, that is another amount.

But, in neither case do you need to know that amount to increase or decrease by. I showed how to do it either way, with one line of code.

Oh I see. I want to start at n, and want to drop by 10% of the range 0 to 255.

so I put in your code:

backLight += (255 - 0) * 0.1;

And I set my initial backLight to 0 so its off.

When i press the up button my first increment is 9% instead of 10%

When i press the up button my first increment is 9% instead of 10%

We'll get somewhere when you stop thinking like a politician. A 9% increase or 10% increase is meaningless if you have no idea what the number is a %-age of.

If you say "I wrote..."

   backLight = 0;
   Serial.print("backLight = ");
   Serial.println(backLight);
   .
   .
   .
   backLight += 255 * 0.1;
   Serial.print("backLight = ");
   Serial.println(backLight);

and I get 0 and 25 as the values, when I expected potatoes and soy beans as the values, THEN we can help you understand why you got what you got instead of what you expected.

Posting some uncompilable snippet of code and waving your arms saying "I got the wrong answer" is not going to get you the right answer.

godspeed:
I want to display the percentage that the backlight has dimmed by on the LCD, and the backlight to physically dim. I was displaying the Serial before because I was having trouble with the buttons and I wanted to see what they were doing and was also just mucking around to see what the percentage was while the display show the 0-255, I just haven't taken it out yet.

The Keypad shield is a DFRobot Link
t uses digital pin 10 for controlling the backlight so I use PWM.

Wrong decision.

Pin-10 is meant for controlling backlight ON/OFF

ON: set pin-10 to pinModeINPUT
OFF: set pin-10 to OUTPUT LOW
PWM doesn't do that (except analogWrite(10,0) leads to backlight being OFF .

As soon as you set OUTPUT/HIGH on pin-10 you create a short circuit of pin-10 against ground

Current overload on microcontroller pins might lead to hardware damage (defective pin)

shield circuit schematics: http://www.dfrobot.com/wiki/images/a/a7/LCDKeypad_Shield_SCH.png

PWM OUTPUT is switching pin from OUTPUT/LOW to OUTPUT/HIGH and the other way round with PWM frequency. That's wrong if you want to see backlight dimming using this hardware circuit. You better switch from INPUT to OUTPUT (LOW ONLY!) and the other way round
in short perids of time to see a dimming effect

jurs:
As soon as you set OUTPUT/HIGH on pin-10 you create a short circuit of pin-10 against ground

I can not see that in the schematic.
D10Short.png

Whandall:
I can not see that in the schematic.
D10Short.png

No?

The Base-Emitter line in the transistor is acting like a diode: If you connect 5V HIGH outtput voltage to D10 which is connected to the transistor Base without using a current-limiting resistor, there will flow a current from Base to Emiiter/GND which is only limited by what pin-10 is able to deliver (absolutemax. rating for Atmega328 is 40mA, but you can get a bit more when shortening OUTPUT/HIGH with a diode to GND.

Ok, I agree.

So adding a current limiting resistor (1k) should work?

jurs:
Wrong decision.

Pin-10 is meant for controlling backlight ON/OFF

ON: set pin-10 to pinModeINPUT
OFF: set pin-10 to OUTPUT LOW
PWM doesn't do that (except analogWrite(10,0) leads to backlight being OFF .

As soon as you set OUTPUT/HIGH on pin-10 you create a short circuit of pin-10 against ground

Current overload on microcontroller pins might lead to hardware damage (defective pin)

shield circuit schematics: http://www.dfrobot.com/wiki/images/a/a7/LCDKeypad_Shield_SCH.png

PWM OUTPUT is switching pin from OUTPUT/LOW to OUTPUT/HIGH and the other way round with PWM frequency. That's wrong if you want to see backlight dimming using this hardware circuit. You better switch from INPUT to OUTPUT (LOW ONLY!) and the other way round
in short perids of time to see a dimming effect

Well that could be a problem. So what are my options? put a resistor in? just not bother dimming the backlight?

What do you mean "You better switch from INPUT to OUTPUT (LOW ONLY!) and the other way round
in short periods of time to see a dimming effect"

godspeed:
Well that could be a problem. So what are my options? put a resistor in? just not bother dimming the backlight?

What do you mean "You better switch from INPUT to OUTPUT (LOW ONLY!) and the other way round
in short periods of time to see a dimming effect"

I know, English is not my mother language, but is it really that hard to understand what I wrote?

Watch out for the dim() function in this dimming example sketch()

// Backlight dimming example sketch by 'jurs for LCD KeypaD Shields with DFRobot shield circuit schematics

This example sketch works in time slices of 1000 milliseconds like that:
- initially set "backlight OFF" (zero brightness)
then within one second dim brightness up to "backlight fully ON" (full brightness)
- then repeat the brightnesscycle

void setup()
{
}

void dim(byte dimvalue)
{  
 pinMode(10,INPUT); // backlight ON
 delayMicroseconds(dimvalue*4); // wait a short time (higher dimvalue results in higher brigthness)
 pinMode(10,OUTPUT); // backlight OFF
 delayMicroseconds((255-dimvalue)*4); wait another short time
}

void loop()
{
  byte dimvalue=(millis()%1000)>>2;
  dim(dimvalue);
}

Watch out what the dim() function is doing!

Actually it is just demnonstration code using bad and blocking delay in the code.
In a real-life cooperative multitasking program you probably want to use non-blocking code without any delay and have to do some optimizations if your code is going to do several things at the same time", besides of backlight dimming cycles.