please help me for my project. LCD Shield

please help me for my project. i want to make a PID controlled automatic curtain with LDR. And i want to enter P-I-D values from LCD shield. I have found how to make a menu on LCD shield but my problem is that i cant enter values from LCD shild.

This is code which i’ve found:

String menuItems[] = {"P modulu", "I modulu","D modulu"};

// Navigation button variables
int readKey;
int savedDistance = 0;
int adc_key_in  = 0;
// Menu control variables
int menuPage = 0;
int maxMenuPages = round(((sizeof(menuItems) / sizeof(String)) / 2) + .5);
int cursorPosition = 0;
int light = 50;
        
#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5


  int read_LCD_buttons()
{
  adc_key_in = analogRead(0);      // read the value from the sensor 
  if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
  if (adc_key_in < 50)   return btnRIGHT;  
  if (adc_key_in < 195)  return btnUP; 
  if (adc_key_in < 380)  return btnDOWN; 
  if (adc_key_in < 555)  return btnLEFT; 
  if (adc_key_in < 790)  return btnSELECT;   
  return btnNONE;  // when all others fail, return this...
}

                                                                                      
 int sel_number = 5;
 int lcd_key     = 0;
 int buttonDelay = 30;
 //////////////////////////////////////////////////////
// Creates 3 custom characters for the menu display
byte downArrow[8] = {
 0b00100, //   *
 0b00100, //   *
 0b00100, //   *
 0b00100, //   *
 0b00100, //   *
 0b10101, // * * *
 0b01110, //  ***
 0b00100  //   *
};

byte upArrow[8] = {
 0b00100, //   *
 0b01110, //  ***
 0b10101, // * * *
 0b00100, //   *
 0b00100, //   *
 0b00100, //   *
 0b00100, //   *
 0b00100  //   *
};

byte menuCursor[8] = {
 B01000, //  *
 B00100, //   *
 B00010, //    *
 B00001, //     *
 B00010, //    *
 B00100, //   *
 B01000, //  *
 B00000  //
};

#include <Wire.h>
#include <LiquidCrystal.h>

// Setting the LCD shields pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

void setup() {

 // Initializes serial communication
 Serial.begin(9600);

 // Initializes and clears the LCD screen
 lcd.begin(16, 2);
  lcd.setCursor(0,0);
 lcd.print("PID kontrol");
 delay(3000);
 lcd.clear();
  
 // Creates the byte for the 3 custom characters
 lcd.createChar(0, menuCursor);
 lcd.createChar(1, upArrow);
 lcd.createChar(2, downArrow);

 pinMode(13,OUTPUT);
 pinMode(12,OUTPUT);
 pinMode(11,OUTPUT);
 pinMode(3,OUTPUT);
 
 
}

void loop() 
{

 mainMenuDraw();
 drawCursor();
 operateMainMenu();

}

// This function will generate the 2 menu items that can fit on the screen. They will change as you scroll through your menu. Up and down arrows will indicate your current menu position.
void mainMenuDraw() {

 Serial.print(menuPage);
 lcd.clear();
 lcd.setCursor(1, 0);
 lcd.print(menuItems[menuPage]);
 lcd.setCursor(1, 1);
 lcd.print(menuItems[menuPage + 1]);
 if (menuPage == 0) {
   lcd.setCursor(15, 1);
   lcd.write(byte(2));
 } else if (menuPage > 0 and menuPage < maxMenuPages) {
   lcd.setCursor(15, 1);
   lcd.write(byte(2));
   lcd.setCursor(15, 0);
   lcd.write(byte(1));
 } else if (menuPage == maxMenuPages) {
   lcd.setCursor(15, 0);
   lcd.write(byte(1));
 }
 }

// When called, this function will erase the current cursor and redraw it based on the cursorPosition and menuPage variables.
void drawCursor() {
 
 for (int x = 0; x < 2; x++) {     // Erases current cursor
   lcd.setCursor(0, x);
   lcd.print(" ");
 }

 // The menu is set up to be progressive (menuPage 0 = Item 1 & Item 2, menuPage 1 = Item 2 & Item 3, menuPage 2 = Item 3 & Item 4), so
 // in order to determine where the cursor should be you need to see if you are at an odd or even menu page and an odd or even cursor position.
 if (menuPage % 2 == 0) {
   if (cursorPosition % 2 == 0) {  // If the menu page is even and the cursor position is even that means the cursor should be on line 1
     lcd.setCursor(0, 0);
     lcd.write(byte(0));
   }
   if (cursorPosition % 2 != 0) {  // If the menu page is even and the cursor position is odd that means the cursor should be on line 2
     lcd.setCursor(0, 1);
     lcd.write(byte(0));
   }
 }
 if (menuPage % 2 != 0) {
   if (cursorPosition % 2 == 0) {  // If the menu page is odd and the cursor position is even that means the cursor should be on line 2
     lcd.setCursor(0, 1);
     lcd.write(byte(0));
   }
   if (cursorPosition % 2 != 0) {  // If the menu page is odd and the cursor position is odd that means the cursor should be on line 1
     lcd.setCursor(0, 0);
     lcd.write(byte(0));
   }
 }

 }

 

void operateMainMenu() {
 
 int activeButton = 0;
 while (activeButton == 0) {
   int button;
   readKey = analogRead(0);
   if (readKey < 790) {
     delay(100);
     readKey = analogRead(0);
   }
   button = evaluateButton(readKey);
   switch (button) {
     case 0: // When button returns as 0 there is no action taken
       break;
     case 1:  // This case will execute if the "forward" button is pressed
       button = 0;
       
       
       switch (cursorPosition) 
       { // The case that is selected here is dependent on which menu page you are on and where the cursor is.
         case 0:
           menuItem1();
           break;
     /*    case 1:
           menuItem2();
           break;
         case 2:
           menuItem3();
           break;
         case 3:
           menuItem4();
           break;          */
         
       }

       
       activeButton = 1;
       mainMenuDraw();
       drawCursor();
       break;
     case 2:
       button = 0;
       if (menuPage == 0) {
         cursorPosition = cursorPosition - 1;
         cursorPosition = constrain(cursorPosition, 0, ((sizeof(menuItems) / sizeof(String)) - 1));
       }
       if (menuPage % 2 == 0 and cursorPosition % 2 == 0) {
         menuPage = menuPage - 1;
         menuPage = constrain(menuPage, 0, maxMenuPages);
       }

       if (menuPage % 2 != 0 and cursorPosition % 2 != 0) {
         menuPage = menuPage - 1;
         menuPage = constrain(menuPage, 0, maxMenuPages);
       }

       cursorPosition = cursorPosition - 1;
       cursorPosition = constrain(cursorPosition, 0, ((sizeof(menuItems) / sizeof(String)) - 1));

       mainMenuDraw();
       drawCursor();
       activeButton = 1;
       break;
     case 3:
       button = 0;
       if (menuPage % 2 == 0 and cursorPosition % 2 != 0) {
         menuPage = menuPage + 1;
         menuPage = constrain(menuPage, 0, maxMenuPages);
       }

       if (menuPage % 2 != 0 and cursorPosition % 2 == 0) {
         menuPage = menuPage + 1;
         menuPage = constrain(menuPage, 0, maxMenuPages);
       }

       cursorPosition = cursorPosition + 1;
       cursorPosition = constrain(cursorPosition, 0, ((sizeof(menuItems) / sizeof(String)) - 1));
       mainMenuDraw();
       drawCursor();
       activeButton = 1;
       break;
   }
 }
 }

// This function is called whenever a button press is evaluated. The LCD shield works by observing a voltage drop across the buttons all hooked up to A0.
int evaluateButton(int x) {
 int result = 0;
 if (x < 50) {
   result = 1; // right
 } 
 else if (x < 195) {
   result = 2; // up
 } 
 else if (x < 380) {
   result = 3; // down
 } 
 else if (x < 790) {
   result = 4; // left
 }
 return result;
}

// If there are common usage instructions on more than 1 of your menu items you can call this function from the sub
// menus to make things a little more simplified. If you don't have common instructions or verbage on multiple menus
// I would just delete this void. You must also delete the drawInstructions()function calls from your sub menu functions.
void drawInstructions() {

 lcd.setCursor(0, 1); // Set cursor to the bottom line
 lcd.print("Use ");
 lcd.write(byte(1)); // Up arrow
 lcd.print("/");
 lcd.write(byte(2)); // Down arrow
 lcd.print(" buttons");

}

void menuItem1()
{ 

 int activeButton = 0;
 lcd.clear();
 lcd.setCursor(3, 0);
 lcd.print("P=");
 lcd.print(sel_number);



while (activeButton == 0) {     //bu komut ile p sekmesine giriş yaptık
   int button;
   
   button = evaluateButton(readKey);
   switch(button){
   
     case 2:
        lcd.clear();
                 lcd.setCursor(3, 0);
                     lcd.print("P=");
                   lcd.print(sel_number=sel_number+5);
         }

    
}
}
int maxMenuPages = round(((sizeof(menuItems) / sizeof(String)) / 2) + .5);

This will fail when you wise up and stop using Strings.

The proper way to determine the number of elements in an array is (sizeof(menuItems) / sizeof(menuItems[0]).

    int button;
   
    button = evaluateButton(readKey);

Why do you need three lines of code to do this?

    int button = evaluateButton(readKey);
void setup() {

It pisses me off no end to see someone put the { on the line with the function, to save lines of code, and then put a blank line after it.

Put EVERY { on a line BY ITSELF.
Put EVERY } on a line BY ITSELF.
Do NOT put blank lines at the start of, or at the end of, a function.

Put ONE blank line between functions.

Use Tools + Auto Format to properly indent code.

Use code tags (the </> icon) to properly post code.

but my problem is that i cant enter values from LCD shild.

An LCD is an output device. I don’t know what “shild” you are using, or why you think you can enter values using it. I don’t know why you can’t.

This sounds more like whining than any real explanation of whatever problem you are having.

i want to increase and decrease this value and assign to P-I-D. but i dont know how i can

Ekran Alıntısı.PNG

Ekran Alıntısı1.PNG

Ekran Alıntısı2.PNG

Ekran Alıntısı3.PNG

And this my PID controlled code but i will mix this code with the LCD code later. Now i have to know how to solve the problem i told.

#include <PID_v1.h> 
#include<Servo.h> 
double Setpoint ; // will be the desired value 
double Input; // photo sensor 
double Output ; //LED 
double Kp=5, Ki=60, Kd=0;  
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT); 
Servo myServo;



void setup() {      
myServo.attach(3);
Serial.begin(9600);      //Hardcode the brigdness value   
Setpoint = 75;   //Turn the PID on   
myPID.SetMode(AUTOMATIC);   //Adjust PID values 
myPID.SetOutputLimits(0,180);  
myPID.SetTunings(Kp, Ki, Kd); 
} 

void loop() { 
  //Read the value from the light sensor. Analog input : 0 to 1024. We map is to a value from 0 to 255 as it's used for our PWM function.
    Input = map(analogRead(5), 0, 1024, 0, 180);  // photo senor is set on analog pin 5 

      //PID calculation 
       myPID.Compute(); 

       //Write the output as calculated by the PID function 
       //analogWrite(3,Output); //LED is set to digital 3 this is a pwm pin.  
       myServo.write(Output);


        
       //Send data by serial for plotting  
       Serial.print(Input); 
       Serial.print(" ");
      // delay(1000);
       Serial.println(Output);
       Serial.print(" "); 
       Serial.println(Setpoint);

       
}

Before you posted, did you read HOW TO USE THIS FORUM?

it is a sticky pospost on top of every forum.

You subjet line is poot and you did not follow the rules if posting code.

Now i have to know how to solve the problem i told.

You have not explained what the problem is. You push some switches. It is not clear which ones. Something happens. It is not at all clear what that is. Pretend that we can't see what you are doing, because we can't, and that we can't see what the results are, because we can't. Stop whining, and provide some facts.

Thanks PaulS you are right. I will explain gradually.

  1. There are 3 menu options,

first one is "P module"
Second is "I module"
Third is "D module"

  1. When I push the right button for the "P module", I can see "P=5" initially. But I want to increase it by the pushing up button and decrease it by the pushing down button. And so on for "I module" and "D module".

  2. Left button is for come back to main menu.

I hope I can explain it. Thanks.