Smooth LCD brightness adjustment using two buttons

Hi everyone, I am new to Arduino, but I'm extremely excited to play with it and I'm amazed with almost endless possibilities that arduino board gives us!
I have my arduino for about a month now and I wanted to write a simple sketch for an LCD (16,2) which would display some data. My problem now is that I want to use a two brightness controlling buttons. I want to control LCD's backlight smootly by pressing either buttons. I wrote this simple sketch and when I press the Up button or Dn button, it doesn't have any affect on the brightness of backlight. What am I doing wrong? I also wanted to say that I wanted to stick with millis() timing trick because I don't want to suspend all other tasks.
Here is the code:

#include <LiquidCrystal.h>
//assign pin numbers:
const int thermoPin = A0;
const int buttonUp = 9;
const int buttonDn = 8;
const int bkLt = 6;
//set the variable for brightness timer
unsigned long beforeMillisBR_U= 0;
unsigned long beforeMillisBR_D= 0;
//set the variable for current time
unsigned long currentTime_U = millis();
unsigned long currentTime_D = millis();
//set the nterval constant
const long interval = 100;
//assign the liquid crystal display pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
 //initialize the LCD
{lcd.begin(16,2); 
 //set modes for button pins
pinMode(buttonUp,INPUT);
pinMode(buttonDn,INPUT);
 //set mode for LED brightness pin
pinMode(bkLt,OUTPUT);
 //initialize serial communication with computer
Serial.begin(9600);
int thermoState = analogRead(thermoPin); 
 int buttonUpState = digitalRead(buttonUp);
 int buttonDnState = digitalRead(buttonDn);
 
}

void loop() {
 
 

  

 //print the button states to the LCD
 Serial.println(thermoState);
 lcd.cursor();
 lcd.setCursor(0,0);
 lcd.print("UP "); ;
 lcd.setCursor(4,0);
 lcd.print(buttonUpState);
 lcd.setCursor(0,1);
 lcd.print("DN "); 
 lcd.setCursor(4,1);
 lcd.print(buttonDnState);
{ int brightnessValue = 50; 
 int fadeAmount = 5;
// brighten the backlight in the LCD
if(buttonUpState == 0){brightnessValue = brightnessValue + fadeAmount;}
if(currentTime_U - beforeMillisBR_U >= interval)
  {beforeMillisBR_U = currentTime_U;}



// darken the backlight in th LCD 
if(buttonDnState == 0){brightnessValue = brightnessValue - fadeAmount;}
if(currentTime_D - beforeMillisBR_D >= interval){
  beforeMillisBR_D = currentTime_D;
}
 analogWrite(bkLt, brightnessValue);

}
}

This one doesn't compile because of the curly brackets are in wrong places. I'm not able to fix it at this moment because I'm at school. Please help!

void setup() {
 //initialize the LCD
{

Too many {

alright, I took the { out , compiled the sketch and uploaded it to the arduino. Now I'm getting the brightness value at 50 and when I press the Up button, it brightens up by 5 points and that's it. it won't go any higher than 55 and when I let the button go it goes back to 50. How do I rewrite the brightnessValue to 55 and then to 60,65,70 and so on? I want the brightness to stay at what I set it to.

#include <LiquidCrystal.h>
           //assign pin numbers:
const int thermoPin = A0;
const int buttonUp = 9;
const int buttonDn = 8;
const int bkLt = 6;
          //set the variable for brightness timer
unsigned long beforeMillisBR_U= 0;
unsigned long beforeMillisBR_D= 0;
          //set the variable for current time
unsigned long currentTime_U = millis();
unsigned long currentTime_D = millis();
          //set the nterval constant
const long interval = 100;
//assign the liquid crystal display pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() 
          //initialize the LCD
{lcd.begin(16,2); 
          //set modes for button pins
pinMode(buttonUp,INPUT);
pinMode(buttonDn,INPUT);
          //set mode for LED brightness pin
pinMode(bkLt,OUTPUT);
          //initialize serial communication with computer
Serial.begin(9600);
 
 
}

void loop() 
{
               //print the button states to the LCD             
 int thermoState = analogRead(thermoPin); 
 int buttonUpState = digitalRead(buttonUp);
 int buttonDnState = digitalRead(buttonDn);
 Serial.println(thermoState);
 lcd.cursor();
 lcd.setCursor(0,0);
 lcd.print("UP "); ;
 lcd.setCursor(4,0);
 lcd.print(buttonUpState);
 lcd.setCursor(0,1);
 lcd.print("DN "); 
 lcd.setCursor(4,1);
 lcd.print(buttonDnState);
  { int brightnessValue = 50; 
    int fadeAmount = 5;
                // brighten the backlight in the LCD
  if(buttonUpState == 0){brightnessValue = brightnessValue + fadeAmount;}
  if(currentTime_U - beforeMillisBR_U >= interval)
      {beforeMillisBR_U = currentTime_U;}



                // darken the backlight in th LCD 
   if(buttonDnState == 0){brightnessValue = brightnessValue - fadeAmount;}
   if(currentTime_D - beforeMillisBR_D >= interval)
       {beforeMillisBR_D = currentTime_D;}
   
   analogWrite(bkLt, brightnessValue);
}
}








/code]

:astonished: my eyes hurt.
Use CTRL T on your sketch.
In your if functions place the { on a new line, then your statements one per line, then a new line then your }

Reattach the reformatted sketch again.

How are your switches wired?

I'm sorry, I was trying to rewrite it in 10 different ways and it got messed up.
my switches are wired so they send 0V to the pins 9 & 10 when pressed and I have two 10K resistors connected between switches and 5V so the pins read HIGH when not pressed.
The LCD shows properly the state of each button, 1 when idle and 0 when pressed.

#include <LiquidCrystal.h>
//assign pin numbers:
const int thermoPin = A0;
const int buttonUp = 9;
const int buttonDn = 8;
const int bkLt = 6;
//set the variable for brightness timer
unsigned long beforeMillisBR_U = 0;
unsigned long beforeMillisBR_D = 0;
//set the variable for current time
unsigned long currentTime_U = millis();
unsigned long currentTime_D = millis();
//set the nterval constant
const long interval = 100;
//assign the liquid crystal display pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup()
//initialize the LCD
{ lcd.begin(16, 2);
//set modes for button pins
  pinMode(buttonUp, INPUT);
  pinMode(buttonDn, INPUT);
  //set mode for LED brightness pin
  pinMode(bkLt, OUTPUT);
//initialize serial communication with computer
  Serial.begin(9600);


}

void loop()
{
//print the button states to the LCD
  int thermoState = analogRead(thermoPin);
  int buttonUpState = digitalRead(buttonUp);
  int buttonDnState = digitalRead(buttonDn);
  Serial.println(thermoState);
  lcd.cursor();
  lcd.setCursor(0, 0);
  lcd.print("UP "); ;
  lcd.setCursor(4, 0);
  lcd.print(buttonUpState);
  lcd.setCursor(0, 1);
  lcd.print("DN ");
  lcd.setCursor(4, 1);
  lcd.print(buttonDnState);
  { int brightnessValue = 50;
    int fadeAmount = 5;
// brighten the backlight in the LCD
    if (buttonUpState == 0) {
      brightnessValue = brightnessValue + fadeAmount;
    }
    if (currentTime_U - beforeMillisBR_U >= interval)
    {
      beforeMillisBR_U = currentTime_U;
    }


// darken the backlight in th LCD
    if (buttonDnState == 0) {
      brightnessValue = brightnessValue - fadeAmount;
    }
    if (currentTime_D - beforeMillisBR_D >= interval)
    {
      beforeMillisBR_D = currentTime_D;
    }

    analogWrite(bkLt, brightnessValue);
  }
}


/code]

int brightnessValue = 50;
int fadeAmount = 5;
Make these global.

What do you want to to here?
if (currentTime_U - beforeMillisBR_U >= interval)
{
beforeMillisBR_U = currentTime_U;
}
. . .
if (currentTime_D - beforeMillisBR_D >= interval)
{
beforeMillisBR_D = currentTime_D;
}

.

By global do you mean that I should post them at the beginning of my sketch? And I'm trying to delay brightness using millis(), just like it's done in the "blink without delay" example, but instead of blinking, I wanted to increase/decrease the brightness of an LED in a 100ms interval by 5 points. I know that it won't be as smooth as it's possible, but I'm not that far advanced in the code. I just started couple weeks ago.

By global do you mean that I should post them at the beginning of my sketch?

Well, yes, before setup()
See:
http://www.arduino.cc/en/Reference/Scope

BWD
Maybe this will help:

Hi,

Also

{ int brightnessValue = 50; // You do not need this {

remove the } at the end of your code in the main loop for this pair

if you use INPUT_PULLUP for your button pinmodes

you do not need the two 10k ohm resistors.

You guys are the best! The problem was with not setting the brightnessValue to global. Now that I've set it to global it dims and brightens the LCD as it should. Any suggestion on how to stop the brightnessValue from overflowing and returning to 0 ?
@ LarryD, very good tutorial and definitely something that I should've looked at before starting to mess around with anything more complex than just blinking an LED.

Let's say your maximum brightness allowed is 500 and the minimum is 50.
Add code to:

  • check to see if it is over 500 make it 500.
  • check to see if it is under 50 make it 50.

if(x > 500) x = 500;
if(x< 50) x = 50;
analogWrite(bkLt, x);

You could also use constrain();