LCD 16x2 getting buttons to work code + errors

Hi,

i purchased LCD 16x2 - the one with 5 buttons on it - im trying to write code for my project which is a light sensing application. Im just trying to come up with some sort of an interface where i can navigate with the buttons

the code ive written is generating these errors

In function 'void loop()':
163: error: a function-definition is not allowed before '{' token
199: error: expected '}' at end of input

//------------------------------------
//----------------
//------------------------------------

/*-------Import required libraries---------*/
#include <LiquidCrystal.h>

/*-------Declare objects----------------*/
LiquidCrystal lcd(8,9,4,5,6,7);

/* ---------Button Information--------------

Right: 0.00V ; 0 @ 10 bit

UP: 0.71V ; 145 @ 10 bit

DOWN: 1.61V ; 329 @ 10 bit

LEFT: 2.47V ; 505 @ 10 bit

SELECT: 3.62V ; 741 @ 10 bit

-------------------------------------------*/

/*-------Definitions--------------*/
#define BUTTON_ADC_PIN A0  // A0 is the button ADC input
#define LCD_BACKLIGHT_PIN  // D3 controls LCD backlight

// ADC readings expected for the 5 buttons on the ADC input

#define RIGHT_10BIT_ADC 0    //right
#define UP_10BIT_ADC 145     //up
#define DOWN_10BIT_ADC 329   //down
#define LEFT_10BIT_ADC 505   //left
#define SELECT_10BIT_ADC 741 //select
#define BUTTONHYSTERESIS 10  //hysteresis for valid button sensing window

//return values for ReadButtons()

#define BUTTON_NONE 0
#define BUTTON_RIGHT 1
#define BUTTON_UP 2
#define BUTTON_DOWN 3
#define BUTTON_LEFT 4
#define BUTTON_SELECT 5

//Variables

byte buttonJustPressed = false; // This will be true after a ReadButtons() call if triggered
byte buttonJustReleased = false; // This will be true after a ReadButtons() call if triggered
byte buttonWas = BUTTON_NONE; // used by ReadButtons() for detection of button events



/*---------------------------------------------------------------
setup()
Called by the Arduino framework once, before the main loop begins
----------------------------------------------------------------*/
void setup() {
  // button ADC input
  pinMode(BUTTON_ADC_PIN,INPUT); //ensures A0 is an input
  digitalWrite(BUTTON_ADC_PIN,LOW); // ensures pullup is off on A0
  lcd.begin(16,2);
  //Print Starting TEXT
  
  lcd.print("Hi");
  lcd.setCursor(0,1);
  lcd.print("There");
  
  delay(3000);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Press SELECT");
  lcd.setCursor(0,1);
  lcd.print("to start: ");
  delay(5000);
}

/*
Approximation of 150 lux per 1uA
V = IR , (1uA)(56k ohms) = 56mV

150lx/1uA/56mV/1uA

150lx/56mV = 2.67857lx/mV


*/


void loop() {
  
  byte button;
  
   // get the latest button pressed and buttonJustPressed and buttonJustReleased flags.
  
  /*button = ReadButtons();
  if(buttonJustPressed || buttonJustReleased)
  {
   lcd.clear();
   lcd.setCursor(0,0);
  }*/
  
 
  lcd.clear();
  lcd.setCursor(0,0);
  
  switch(button)
  {
    case BUTTON_NONE:
    {
      lcd.clear();
      lcd.setCursor(0,0);
      break;
    }
    case BUTTON_RIGHT:
    {
      break;
    }
    case BUTTON_UP:
    {
      break;
    }
    case BUTTON_DOWN:
    {
      break;
    }
    case BUTTON_LEFT:
    {
      break;
    }
    case BUTTON_SELECT:
    {
        lcd.clear();
        lcd.setCursor(0,0);
        float photoDiode_1 = analogRead(A1);
        float photoDiode_2 = analogRead(A1);
        float photoDiode_3 = analogRead(A1);
        float averagetoprint = ((photoDiode_1)+(photoDiode_2)+(photoDiode_3))/3;
  
        float voltage_1 = photoDiode_1 * ((5.0 / 1023.0)*1000);
        float voltage_2 = photoDiode_2 * ((5.0 / 1023.0)*1000);
        float voltage_3 = photoDiode_3 * ((5.0 / 1023.0)*1000);
        float averageVoltage = ((voltage_1)+(voltage_2)+(voltage_3))/3;
  
        float lux = 2.67857 * averageVoltage;
  
        lcd.print(averagetoprint);
        lcd.setCursor(0,1);
        lcd.print(lux);
        lcd.print( " Lux");
        
        delay(5000);
        break;
    }
    default:
    {
      break;
    }
  } // end switch case

byte ReadButtons()
{
  unsigned int buttonVoltage;
  byte button = BUTTON_NONE; //return no button pressed if the below checks dont write to button
  
  //read the button ADC pin voltage
  
  buttonVoltage = analogRead(BUTTON_ADC_PIN);
  
  //sense ifthe voltage falls within valid voltage windows
  
  if(buttonVoltage < (RIGHT_10BIT_ADC + BUTTONHYSTERESIS))
  {
    button = BUTTON_RIGHT;
  }
  else if (buttonVoltage >=(UP_10BIT_ADC - BUTTONHYSTERESIS))
          && buttonVoltage <=(UP_10BIT_ADC + BUTTONHYSTERESIS))
  {
    button = BUTTON_UP;
  }
  else if (buttonVoltage >=(DOWN_10BIT_ADC - BUTTONHYSTERESIS)
        &&buttonVoltage <=(DOWN_10BIT_ADC + BUTTONHYSTERESIS))
        {
          button = BUTTON_DOWN;
        }
 else if (buttonVoltage >= (LEFT_10BIT_ADC - BUTTONHYSTERESIS)
       && buttonVoltage<=(LEFT_10BIT_ADC + BUTTONHYSTERESIS))
       {
         button = BUTTON_LEFT;
       }
 else if (buttonVoltage >=(SELECT_10BIT_ADC - BUTTONYHYSTERESIS)
      &&buttonVoltage <=(SELECT_10BIT_ADC + BUTTONHYSTERESIS))
     {
      button = BUTTON_SELECT;
     }
//Handle button flags for just pressed and just released events
}
}

ot sure if anyone here can help, essentially im trying to use a switch case but i tried making a button function which i can use. Something isnt working like i expect it to

any help would be great

Is that the DFRobotics one?

There's code on their site right here.

Its not but the code looks similar so i tried using that version of code

Here is my new code

#include <LiquidCrystal.h>

/******************************************



******************************************/

LiquidCrystal lcd(8,9,4,5,6,7);

//define some values used by panel and buttons

int lcd_key = 0;
int adc_key_in = 0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5

//read the buttons
int read_LCD_buttons()
{
  adc_key_in = analogRead(A0); // read value
  
  if(adc_key_in > 1000) return btnNONE;
  
  if(adc_key_in < 50) return btnRIGHT;
  
  if(adc_key_in < 250) return btnUP;
  
  if(adc_key_in < 450) return btnDOWN;
  
  if(adc_key_in < 650) return btnLEFT;
  
  if(adc_key_in < 850) return btnSELECT;
  
  return btnNONE; // If all else fails
  
  void setup()
  {
    lcd.begin(16,2); // start
    lcd.setCursor(0,0);
    lcd.print("Hi");
    lcd.setCursor(0,1);
    lcd.print("There");
    delay(3000);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Press SELECT");  
    lcd.setCursor(0,1);
    lcd.print("to start");
  }
  
  void loop()
  {
    lcd_key = read_LCD_buttons(); //read the buttons
    
    switch (lcd_key)    //Depending on button pressed, perform action
    {
      case btnRIGHT
      {
        lcd.clear()
        lcd.print("RIGHT");
        delay(3000);
        lcd.clear()
        lcd.setCursor(0,0);
        lcd.print("Press SELECT");  
        lcd.setCursor(0,1);
        lcd.print("to start");
        break;
      }
      case btnLEFT
      {
        lcd.clear();
        lcd.print("Left");
        delay(3000);
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Press SELECT");  
        lcd.setCursor(0,1);
        lcd.print("to start");
        break;
      }
      case btnUP
      {
        lcd.clear();
        lcd.print("Up");
        delay(3000);
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Press SELECT");  
        lcd.setCursor(0,1);
        lcd.print("to start");
        break;
      }
      case btnDOWN
      {
        lcd.clear();
        lcd.print("Down");
        delay(3000);
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Press SELECT");  
        lcd.setCursor(0,1);
        lcd.print("to start");
        break;
      }
      case btnSELECT
      {
        lcd.clear();
        float photoDiode_1 = analogRead(A1);
        float photoDiode_2 = analogRead(A1);
        float photoDiode_3 = analogRead(A1);
        float averagetoprint = ((photoDiode_1)+(photoDiode_2)+(photoDiode_3))/3;
  
        float voltage_1 = photoDiode_1 * ((5.0 / 1023.0)*1000);
        float voltage_2 = photoDiode_2 * ((5.0 / 1023.0)*1000);
        float voltage_3 = photoDiode_3 * ((5.0 / 1023.0)*1000);
        float averageVoltage = ((voltage_1)+(voltage_2)+(voltage_3))/3;
  
        float lux = 2.67857 * averageVoltage;
  
        lcd.print(averagetoprint);
        lcd.setCursor(0,1);
        lcd.print(lux);
        lcd.print( " Lux");
        delay(5000);
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Press SELECT");  
        lcd.setCursor(0,1);
        lcd.print("to start");
        break;
      }
      case btnNONE;
      {
        break;
      }
    }//end switch case
  } // end void loop

and the errors I'm getting.

In function 'int read_LCD_buttons()':
42: error: a function-definition is not allowed here before '{' token
57: error: a function-definition is not allowed here before '{' token
139: error: expected `}' at end of input

So did you try debug that and fix the compile errors?

You're missing a } after the btnNONE line, before setup, and you have no ":" on your case lines, for a start