Programming LCD display for swtching between inputs

I am still unsure as to how to write the code for the other pushbutton to switch BETWEEN inputs..

That is where the states come in. Each switch press change from one state to another. In the first state, the other switch presses increment one variable. In the second state, the other switch presses increment another variable. In the third state, the other switch presses increment a third variable.

I don't see any problems other than some labeling issues, but it seems that you have the first part. Just do the same for the second, and add another button counter and last button state. Normally you would use arrays for multiple buttons, not saying you still cant, but using arrays will shorten the code and get rid of repetition. Also maybe the use of CASE statements for multiple items, instead of IF/ELSE statements.

Look into both.

Instead of 3 push buttons, in these situations I use a rotary encoder with a built-in push button. Turning the encoder increases or decreases the selected variable, and pressing it selects the next variable.

Either way, the coding is most easily done as a state machine, something like this:

enum InputState { IS_timesPerDay, IS_numDays, IS_firstTime };

InputState currentInputState = IS_timesPerDay;

loop()
{
  switch(currentInputState)
  {
  case IS_timesPerDay:
    // if button 1 or 2 is pressed then adjust timesPerDay variable
    // if button 3 is pressed then change currentInputState to IS_numDays;
    break;

  case IS_numDays:
    // similar to above but adjust numDays variable or advance state to IS_firstTime
    break;

  case IS_firstTime:
    // etc.
    break;
  }
  // do everything else not involving input here...
}

Thanks guys.

I have worked a little bit more on my program. But I am still unable to switch between inputs when pressing the set push button. The cursor would not move to the next input. Have a strong feeling that my code for that portion is wrong but I don't know how to correct it. Pls assist.

And I am not too familiar with case statements as well. If using such statements is easier, pls assist me as to how to use it as well.

Thanks. This is my current code I have:

// include the library code:
#include <LiquidCrystal.h>
#include <Servo.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

Servo myservo;  // create servo object to control a servo 


// constants won't change
const int  incrementPin = 6;    // the pin that the increment pushbutton is attached to
const int  setPin = 7;    // the pin that the set pushbutton is attached to

// Variables will change:
 int incrementCounter = 2;   // counter for the number of button presses
 int incrementState = 0;         // current state of the button
 int setState = 0;         // current state of the button
 int lastButtonState = 0;     // previous state of the button
 int lastButtonState2 = 0;
 int pos = 0;    // variable to store the servo position 
 int feedinterval = 0;        // interval between feeds 
 
 void setup() {

// initialize the button pin as a input:
   
pinMode(incrementPin, INPUT);
pinMode(setPin, INPUT);
myservo.attach(9);              // attaches the servo on pin 9 to the servo object
   
lcd.begin(20,4);                 // LCD has 20 columns, 4 rows
lcd.setCursor(0,0);              // Choose column 0, row 0( 1st column, 1st row)
lcd.print("Freq:     Days:");    // Print the stated
lcd.setCursor(0, 1);             // Choose column 0, row 1
lcd.print("1st feed time:");     // Print the stated
lcd.setCursor (0, 2);            // Choose column 0, row 2
lcd.print("current time:");      // Print the stated
lcd.setCursor(0,3);              // Choose column 0, row 3(1st column, last row)
lcd.print("");                   // Print the stated
   
 }
 
 void loop() 
 {
  lcd.setCursor(5, 0);                        // set cursor to 5th column, 1st row
  lcd.blink();                                // blink cursor
  incrementState = digitalRead(incrementPin);  // read the pushbutton up input pin:
  if (incrementState != lastButtonState)     // compare the buttonState to its previous state 
  {
    if (incrementState == HIGH)            // if the state has changed, increment the counter  
     { 
      incrementCounter++;              // Increment counter by 1
      lcd.setCursor(5,0);              // Set cursor at 5th column of 1st row      
      lcd.print(incrementCounter);    // print the counter
      
      if (incrementCounter > 4)      // if counter more than 4
      {
        incrementCounter = 2;        // reset counter to 2
        lcd.setCursor(5,0);
        lcd.print(incrementCounter);  // print counter value of 2
      }
     }   
      delay(50);
      
      frequencyoffeed (incrementCounter);
   }
   
   // save the current state as the last state, for next time through the loop
   lastButtonState = incrementState;
   
   setState = digitalRead(setPin);  // read the pushbutton set input pin:
    if (setState != lastButtonState2)     // compare the buttonState to its previous state 
  {
    if (setState == HIGH)            // if the state has changed, increment the counter
    { 
      lcd.setCursor(5,0);
      lcd.noBlink(); 
      lcd.setCursor(15,0);
      lcd.blink();
    }
  }
 }
 
 void servo() 
{ 
  for (int i= 0; i < 1; i ++)
  {
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  }
} 

void frequencyoffeed (int incrementCounter)
{
  if (incrementCounter == 2)        // for 2 times per day feeding
 {
   feedinterval = 1200;             // interval bettwen feeding
   lcd.setCursor(0,3);              // Set cursor at 1st column of 4th row      
   lcd.print(feedinterval);        // print the feedinterval 
 }
 if (incrementCounter == 3)        // for 3 times per day feeding
 {
   feedinterval = 800;              // interval bettwen feeding
   lcd.setCursor(0,3);              // Set cursor at 1st column of 4th row      
   lcd.print(feedinterval);        // print the feedinterval
 }
 if (incrementCounter == 4)        // for 4 times per day feeding
 {
   feedinterval = 600;              // interval bettwen feeding
   lcd.setCursor(0,3);              // Set cursor at 1st column of 4th row      
   lcd.print(feedinterval);        // print the feedinterval
 }
 }

I think you'd find it easier if the current and previous state variables used some naming convention. Personally, I like currState and prevState. It makes it very clear that they are both state variables and that there is an order to them.

incrementState and lastButtonState do not do that for me. setState and lastButtonState2 are even worse.

    if (setState == HIGH)            // if the state has changed, increment the counter
    { 
      lcd.setCursor(5,0);
      lcd.noBlink(); 
      lcd.setCursor(15,0);
      lcd.blink();
    }

I don't see the relationship between that comment and that code. I also don't see the value in that code. It is going to move the cursor, stop the blinking, move the cursor, and resume the blinking faster than you can see it doing anything.

Oh sorry about the comment. It was wrong.

if (setState == HIGH)            // if the state has changed, move the cursor to next input
    { 
      lcd.setCursor(5,0);
      lcd.noBlink(); 
      lcd.setCursor(15,0);
      lcd.blink();
    }

So how should I change the above code to make sure that the cursor indeed switches to the next input..I am stuck here and can't move. Hope you can help me out

So how should I change the above code to make sure that the cursor indeed switches to the next input..I am stuck here and can't move. Hope you can help me out

The position of the cursor on the LCD screen has nothing to do with there the sketch should store data when the other switch is pressed.

You can, in the body of that if statement, increment a variable whose value is used in the increment code to decide what to increment. Or, you can use the value in this block to determine where to copy the value in incrementCounter.

Solving one problem. Look at "frequencyoffeed (incrementCounter);" I moved the lcd.setcursor and blink functions, and it works like you want.

void loop() 
 {
   incrementState = digitalRead(incrementPin);  // read the pushbutton up input pin:
  if (incrementState != lastButtonState)     // compare the buttonState to its previous state 
  {
    if (incrementState == HIGH)            // if the state has changed, increment the counter  
     { 
      incrementCounter++;              // Increment counter by 1
      lcd.setCursor(5,0);              // Set cursor at 5th column of 1st row      
      lcd.print(incrementCounter);    // print the counter
      
      if (incrementCounter > 4)      // if counter more than 4
      {
        incrementCounter = 2;        // reset counter to 2
        lcd.setCursor(5,0);
        lcd.print(incrementCounter);  // print counter value of 2
      }
     }   
      delay(50);
      
      frequencyoffeed (incrementCounter);
      lcd.setCursor(5, 0);                        // set cursor to 5th column, 1st row 
      lcd.blink();                                // blink cursor

   }
   
   // save the current state as the last state, for next time through the loop
   lastButtonState = incrementState;
   
   setState = digitalRead(setPin);  // read the pushbutton set input pin:
    if (setState != lastButtonState2)     // compare the buttonState to its previous state 
  {
    if (setState == HIGH)            // if the state has changed, increment the counter
    { 
     // lcd.setCursor(5,0); //not needed
     // lcd.noBlink(); //not needed
      lcd.setCursor(15,0);
      lcd.blink();
    }
  }
 }

I noticed that your feeding/current time is not showing properly. It does not remove the zero from the 1200, so you need to add lcd.print(" "); to clear that part of the LCD to make the numbers show properly.

HazardsMind:
Solving one problem. Look at "frequencyoffeed (incrementCounter);" I moved the lcd.setcursor and blink functions, and it works like you want.

void loop() 

{
   incrementState = digitalRead(incrementPin);  // read the pushbutton up input pin:
  if (incrementState != lastButtonState)     // compare the buttonState to its previous state
  {
    if (incrementState == HIGH)            // if the state has changed, increment the counter 
     {
      incrementCounter++;              // Increment counter by 1
      lcd.setCursor(5,0);              // Set cursor at 5th column of 1st row     
      lcd.print(incrementCounter);    // print the counter
     
      if (incrementCounter > 4)      // if counter more than 4
      {
        incrementCounter = 2;        // reset counter to 2
        lcd.setCursor(5,0);
        lcd.print(incrementCounter);  // print counter value of 2
      }
     }   
      delay(50);
     
      frequencyoffeed (incrementCounter);
      lcd.setCursor(5, 0);                        // set cursor to 5th column, 1st row
      lcd.blink();                                // blink cursor

}
   
   // save the current state as the last state, for next time through the loop
   lastButtonState = incrementState;
   
   setState = digitalRead(setPin);  // read the pushbutton set input pin:
    if (setState != lastButtonState2)     // compare the buttonState to its previous state
  {
    if (setState == HIGH)            // if the state has changed, increment the counter
    {
     // lcd.setCursor(5,0); //not needed
     // lcd.noBlink(); //not needed
      lcd.setCursor(15,0);
      lcd.blink();
    }
  }
}




I noticed that your feeding/current time is not showing properly. It does not remove the zero from the 1200, so you need to add lcd.print(" "); to clear that part of the LCD to make the numbers show properly.

Thanks. Now the cursor blinks at the 2nd input as needed. And I have added the lcd.print(" ") as well.

For my next step, I need to increment the 2nd input using the same pushbutton as used for increment of the first input. So what I did is I created a new daysincrementCounter and daysincrementState and used the same code as for the increment of the 1st input. But now when I press the pushbutton, both the two inputs increase at the same time. I only what the 1nd input to increase when the set push button is pressed to go to the 2nd input.

How should I change the code?

And is there a easier method to write the code? Cos after the 2nd input is done, the cursor has to be set to a 3rd,4th,5th and 6th input where increment has to be done in a time format HH:MM(just like setting the time in a digital watch)

My progress of the code is below:

// include the library code:
#include <LiquidCrystal.h>
#include <Servo.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

Servo myservo;  // create servo object to control a servo 


// constants won't change
const int  incrementPin = 6;    // the pin that the increment pushbutton is attached to
const int  setPin = 7;    // the pin that the set pushbutton is attached to

// Variables will change:
 int freqincrementCounter = 2;   // counter for the number of button presses for frquency of feed
 int freqincrementState = 0;         // current state of the button
 int daysincrementCounter = 1;    // counter for number of button pushes for days
 int daysincrementState = 0;         // current state of the button
 int setState = 0;         // current state of the button
 int lastButtonState = 0;     // previous state of the button
 int lastButtonState2 = 0;
 int pos = 0;    // variable to store the servo position 
 int feedinterval = 0;        // interval between feeds 
 
 void setup() {

// initialize the button pin as a input:
   
pinMode(incrementPin, INPUT);
pinMode(setPin, INPUT);
myservo.attach(9);              // attaches the servo on pin 9 to the servo object
   
lcd.begin(20,4);                 // LCD has 20 columns, 4 rows
lcd.setCursor(0,0);              // Choose column 0, row 0( 1st column, 1st row)
lcd.print("Freq:     Days:");    // Print the stated
lcd.setCursor(0, 1);             // Choose column 0, row 1
lcd.print("1st feed time:");     // Print the stated
lcd.setCursor (0, 2);            // Choose column 0, row 2
lcd.print("current time:");      // Print the stated
lcd.setCursor(0,3);              // Choose column 0, row 3(1st column, last row)
lcd.print("");                   // Print the stated
   
 }
 
 void loop() 
 {
  freqincrementState = digitalRead(incrementPin);  // read the pushbutton up input pin:
  if (freqincrementState != lastButtonState)     // compare the buttonState to its previous state 
  {
    if (freqincrementState == HIGH)            // if the state has changed, increment the counter  
     { 
      freqincrementCounter++;              // Increment counter by 1
      lcd.setCursor(5,0);              // Set cursor at 5th column of 1st row      
      lcd.print(freqincrementCounter);    // print the counter
      
      if (freqincrementCounter > 4)      // if counter more than 4
      {
        freqincrementCounter = 2;        // reset counter to 2
        lcd.setCursor(5,0);
        lcd.print(freqincrementCounter);  // print counter value of 2
      }
     
     }   
      delay(50);
      
      frequencyoffeed (freqincrementCounter);
      lcd.setCursor(5, 0);                        // set cursor to 5th column, 1st row
      lcd.blink();                                // blink cursor
   }
   
   // save the current state as the last state, for next time through the loop
   lastButtonState = freqincrementState;
   
   setState = digitalRead(setPin);  // read the pushbutton set input pin:
    if (setState != lastButtonState2)     // compare the buttonState to its previous state 
  {
    if (setState == HIGH)            // if the state has changed, increment the counter
    { 
      lcd.setCursor(15,0);
      lcd.blink();
    }
  }
  daysincrementState = digitalRead(incrementPin);  // read the pushbutton up input pin:
  if (daysincrementState != lastButtonState)     // compare the buttonState to its previous state 
  {
    if (daysincrementState == HIGH)            // if the state has changed, increment the counter  
     { 
      daysincrementCounter++;              // Increment counter by 1
      lcd.setCursor(15,0);              // Set cursor at 5th column of 1st row      
      lcd.print(daysincrementCounter);    // print the counter
      
      if (daysincrementCounter > 10)      // if counter more than 4
      {
        daysincrementCounter = 1;        // reset counter to 2
        lcd.setCursor(15,0);
        lcd.print(daysincrementCounter);  // print counter value of 2
      }
 }
  }
  }
 void servo() 
{ 
  for (int i= 0; i < 1; i ++)
  {
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  }
} 

void frequencyoffeed (int freqincrementCounter)
{
  if (freqincrementCounter == 2)        // for 2 times per day feeding
 {
   feedinterval = 1200;             // interval bettwen feeding
   lcd.setCursor(0,3);              // Set cursor at 1st column of 4th row      
   lcd.print(feedinterval);        // print the feedinterval 
 }
 if (freqincrementCounter == 3)        // for 3 times per day feeding
 {
   feedinterval = 800;              // interval bettwen feeding
   lcd.setCursor(0,3);     // Set cursor at 1st column of 4th row      
   lcd.print("    ");
   lcd.setCursor(0,3);
   lcd.print(feedinterval);        // print the feedinterval
 }
 if (freqincrementCounter == 4)        // for 4 times per day feeding
 {
   feedinterval = 600;              // interval bettwen feeding
   lcd.setCursor(0,3);              // Set cursor at 1st column of 4th row 
   lcd.print("    ");
   lcd.setCursor(0,3);
   lcd.print(feedinterval);        // print the feedinterval
 }
 }

incrementCounter looks like it is a global variable. If it is then changing it anywhere in the sketch is allowed. When you say

I created a new daysincrementCounter and daysincrementState

how did you do it ? Did you really create new ones and if so, how ?

Cos after the 2nd input is done, the cursor has to be set to a 3rd,4th,5th and 6th

I can see a pattern here. It looks like an array would be a good way to hold several values with a similar meaning.

What you can do is add another condition in (incrementState != lastbuttonState) and add if the other button was pressed or not. This will determine what counter gets updated. Freq or days

I have tried to add another condition, but am not able to get my desired result.
What I am getting now is when the increment pushbutton is pressed, the freq increases. When I press the set pushbutton, it goes to the days input but when I press the increment pushbutton again, it goes back to the freq input and increases it. All along, the days input remains empty (no increments)

I have changed the code as below: What I think it would do is that when the set button is pressed, the freq input would remain as its last state and only the days input would increase when the increment push button is pressed..But since the code does not do what I thought it would do, I realize that something is wrong in the code but am not able to how to correct it.

Hope you could assist. The code is as below:

// include the library code:
#include <LiquidCrystal.h>
#include <Servo.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

Servo myservo;  // create servo object to control a servo 


// constants won't change
const int  incrementPin = 6;    // the pin that the increment pushbutton is attached to
const int  setPin = 7;    // the pin that the set pushbutton is attached to

// Variables will change:
 int freqincrementCounter = 2;   // counter for the number of button presses for frquency of feed
 int freqincrementState = 0;         // current state of the button
 int daysincrementCounter = 1;    // counter for number of button pushes for days
 int daysincrementState = 0;         // current state of the button
 int setState = 0;         // current state of the button
 int lastButtonState = 0;     // previous state of the button
 int lastButtonState2 = 0;
 int pos = 0;    // variable to store the servo position 
 int feedinterval = 0;        // interval between feeds 
 
 void setup() {

// initialize the button pin as a input:
   
pinMode(incrementPin, INPUT);
pinMode(setPin, INPUT);
myservo.attach(9);              // attaches the servo on pin 9 to the servo object
   
lcd.begin(20,4);                 // LCD has 20 columns, 4 rows
lcd.setCursor(0,0);              // Choose column 0, row 0( 1st column, 1st row)
lcd.print("Freq:     Days:");    // Print the stated
lcd.setCursor(0, 1);             // Choose column 0, row 1
lcd.print("1st feed time:");     // Print the stated
lcd.setCursor (0, 2);            // Choose column 0, row 2
lcd.print("current time:");      // Print the stated
lcd.setCursor(0,3);              // Choose column 0, row 3(1st column, last row)
lcd.print("");                   // Print the stated
   
 }
 
 void loop() 
 {

    freqincrementState = digitalRead(incrementPin);  // read the pushbutton up input pin:
  if (freqincrementState != lastButtonState)     // compare the buttonState to its previous state 
  {
    if (freqincrementState == HIGH)            // if the state has changed, increment the counter  
     { 
      freqincrementCounter++;              // Increment counter by 1
      lcd.setCursor(5,0);              // Set cursor at 5th column of 1st row      
      lcd.print(freqincrementCounter);    // print the counter
      
      if (freqincrementCounter > 4)      // if counter more than 4
      {
        freqincrementCounter = 2;        // reset counter to 2
        lcd.setCursor(5,0);
        lcd.print(freqincrementCounter);  // print counter value of 2
      }
     }
        
      delay(50);
      
      frequencyoffeed (freqincrementCounter);
      lcd.setCursor(5, 0);                        // set cursor to 5th column, 1st row
      lcd.blink();                                // blink cursor
   }
   
   // save the current state as the last state, for next time through the loop
   lastButtonState = freqincrementState;
   
   setState = digitalRead(setPin);  // read the pushbutton set input pin:
    if (setState != lastButtonState2)     // compare the buttonState to its previous state 
  {
    if (setState == HIGH)            // if the state has changed, increment the counter
    { 
      lcd.setCursor(15,0);
      lcd.blink();
  
  daysincrementState = digitalRead(incrementPin);  // read the pushbutton up input pin:
  if(freqincrementState == lastButtonState);
  {
  if (daysincrementState != lastButtonState)     // compare the buttonState to its previous state 
  {
    if (daysincrementState == HIGH)            // if the state has changed, increment the counter  
     { 
      daysincrementCounter++;              // Increment counter by 1
      lcd.setCursor(15,0);              // Set cursor at 5th column of 1st row      
      lcd.print(daysincrementCounter);    // print the counter
      
      if (daysincrementCounter > 9)      // if counter more than 4
      {
        daysincrementCounter = 1;        // reset counter to 2
        lcd.setCursor(15,0);
        lcd.print(daysincrementCounter);  // print counter value of 2
      }
     }
  }
  }
  }
  }
 }
  
 void servo() 
{ 
  for (int i= 0; i < 1; i ++)
  {
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  }
} 

void frequencyoffeed (int freqincrementCounter)
{
  if (freqincrementCounter == 2)        // for 2 times per day feeding
 {
   feedinterval = 1200;             // interval bettwen feeding
   lcd.setCursor(0,3);              // Set cursor at 1st column of 4th row      
   lcd.print(feedinterval);        // print the feedinterval 
 }
 if (freqincrementCounter == 3)        // for 3 times per day feeding
 {
   feedinterval = 800;              // interval bettwen feeding
   lcd.setCursor(0,3);     // Set cursor at 1st column of 4th row      
   lcd.print("    ");
   lcd.setCursor(0,3);
   lcd.print(feedinterval);        // print the feedinterval
 }
 if (freqincrementCounter == 4)        // for 4 times per day feeding
 {
   feedinterval = 600;              // interval bettwen feeding
   lcd.setCursor(0,3);              // Set cursor at 1st column of 4th row 
   lcd.print("    ");
   lcd.setCursor(0,3);
   lcd.print(feedinterval);        // print the feedinterval
 }
 }

Anyone can help?

I have made some changes to my codes. But I am still having problem when I press the set pushbutton.
It works fine till it blinks at the LCD position (15,0) but after that when I press the increment pushbutton to increase the values at the dayscounter, it goes back to the freqcounter and increases it. So the program does not run the code for the daycounter..

Anyone can help? The code is:

#include <LiquidCrystal.h>                // Include libraries
#include <Servo.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);    // initialize the library with the numbers of the interface pins
Servo myservo;

const int  incrementPin = 6;              // the pin that the increment pushbutton is attached to
const int  setPin = 7;                    // the pin that the set pushbutton is attached to

int freqstate = 0;                        // state of the frequency pushbutton (low or high)
int setstate = 0;
int daystate = 0;

void setup() 
 {
  pinMode(incrementPin, INPUT);    // initialize the button pin as a input:
  pinMode(setPin, INPUT);
  myservo.attach(9);               // attaches the servo on pin 9 to the servo object
   
  lcd.begin(20,4);                 // LCD has 20 columns, 4 rows
  lcd.setCursor(0,0);              // Choose column 0, row 0( 1st column, 1st row)
  lcd.print("Freq:     Days:");    // Print the stated
  lcd.setCursor(0, 1);             // Choose column 0, row 1
  lcd.print("1st feed time:");     // Print the stated
  lcd.setCursor (0, 2);            // Choose column 0, row 2
  lcd.print("current time:");      // Print the stated
  lcd.setCursor(0,3);              // Choose column 0, row 3(1st column, last row)
  lcd.print("");                   // Print the stated
   
 }
 
 void loop()
 {
   for ( int freqcounter = 2; freqcounter < 5; freqcounter ++)  // freq value from 2 to 4
   {
     freqstate = digitalRead (incrementPin);
       if (freqstate == HIGH)
       {
         lcd.setCursor (5,0);         // set at 5th column, 0th row
         lcd.blink();
         lcd.print(freqcounter);                                 // print value of freq
       
         if (freqcounter > 4)                                    // if freq value more than 4, reset to 2
         {
           freqcounter = 2;
         }
         delay(300);
       }
     }
     
     setstate = digitalRead(setPin);            // read the pushbutton set input pin:
     if (setstate == HIGH)                    // if the state has changed, increment the counter
    { 
                                 
      lcd.setCursor(15,0);
      lcd.blink();
     
   for ( int daycounter = 1; daycounter < 11; daycounter ++)  // freq value from 2 to 4
   {
     daystate = digitalRead (incrementPin);
       if (daystate == HIGH)
       {
         lcd.setCursor (15,0);         // set at 5th column, 0th row
         lcd.blink();
         lcd.print(daycounter);                                 // print value of freq
       
         if (daycounter > 10)                                    // if freq value more than 4, reset to 2
         {
           daycounter = 1;
           
         }
         
         
         delay(300);
       }
     }
 }
 }

What is the for loop doing there ?

The idea using your 2 buttons is to use one of them to change what you are setting (frequency, days or first feed time) and the other one to change the value. So, each time you press the set button the program knows which setting to change.

loop()
read the whatToSet button
if button is pressed
  update a state counter (1=frequency, 2=days, 3= first feed time)
  deal with rollover
  move the LCD cursor
end of if

read the setValue button
if button is pressed
  if state counter is 1
    update frequency
  end of if
  else
  if state counter is 2
    update days
  end of if
  else
  if state counter is 3
    update first feed time
  end of if
end of if
end of loop()

Get this much working and you are well on the way

Looks like this was what I was looking for..

But I am unsure of programming the updating part and the rollover..
Can I get an example please?

You are already doing the rollover

if (freqcounter > 4)                                    // if freq value more than 4, reset to 2
 {
   freqcounter = 2;
 }

As to updating you do something similar.

if button is pressed
  if state counter is 1
    frequency counter = frequency counter +1 (CLUE:there is a neat way of doing this)
    deal with rollover (as above)  
    show frequency counter on the LCD
  end of if

Oh so that is called rollover :slight_smile:

How am I supposed to declare the state counter? As 1 = freq, 2 = day, 3 = time and son on...
So do I declare it as int statecounter = an array?
Or have differrent state counters for each input? Like statecounter1 = freq, statecounter2 = day and so one?

Declare the state counter as an int. You can then increment it (and rollover) to your heart's content. The check for what state the input is at is then easy

if (state == 1)
{
  //do stuff here
}

This being C you can also give each state its own name to make it more obvious what you are testing for but walk before you run and include plenty of comments.

By the way, your button input really needs to be debounced or it will be unreliable but you have plenty of other things to do so I suggest that you put it on the 'to do' list and accept odd responses to buttons for now.

I have tried doing the code. But there is problem.

When I press the setbutton, instead of the cursor going to the freq input, it goes to the days input and starts to increment non-stop immediately, without even pressing the increment button..

Made some changes but does not seem to rectify the problem..

Anyone able to spot the mistake?

#include <LiquidCrystal.h>                // Include libraries
#include <Servo.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);    // initialize the library with the numbers of the interface pins
Servo myservo;

const int  incrementPin = 6;              // the pin that the increment pushbutton is attached to
const int  setPin = 7;                    // the pin that the set pushbutton is attached to

int setState = 0;
int setCounter = 0;
int valueState = 0;
int freqCounter = 0;
int dayState = 0;
int dayCounter = 0;

void setup() 
 {
  pinMode(incrementPin, INPUT);    // initialize the button pin as a input:
  pinMode(setPin, INPUT);
  myservo.attach(9);               // attaches the servo on pin 9 to the servo object
   
  lcd.begin(20,4);                 // LCD has 20 columns, 4 rows
  lcd.setCursor(0,0);              // Choose column 0, row 0( 1st column, 1st row)
  lcd.print("Freq:     Days:");    // Print the stated
  lcd.setCursor(0, 1);             // Choose column 0, row 1
  lcd.print("1st feed time:");     // Print the stated
  lcd.setCursor (0, 2);            // Choose column 0, row 2
  lcd.print("current time:");      // Print the stated
  lcd.setCursor(0,3);              // Choose column 0, row 3(1st column, last row)
  lcd.print("");                   // Print the stated
   
 }
 
 void loop()
 {
   setState = digitalRead(setPin);            // read the pushbutton set input pin:
     if (setState == HIGH)                    // if the state has changed, increment the counter
     {
       setCounter++;
       
       if (setCounter > 6)
       {
         setCounter = 1;
       }
     }
     
     valueState = digitalRead(incrementPin);
     if (valueState == HIGH)
     { 
       if (setCounter == 1)
       {
         lcd.setCursor(5,0);
         lcd.blink();
         {
         freqCounter++;
         lcd.setCursor(5,0);
         lcd.print(freqCounter);
         }
         
         if (freqCounter > 4)
         {
           freqCounter = 2;
         }
       }
     }
     
     else if (setCounter == 2)
     {    
      lcd.setCursor(15,0);
      lcd.blink();
      {
      dayCounter++;
      lcd.setCursor(15,0);
      lcd.print(dayCounter);
         
         if (dayCounter > 10)
         {
           freqCounter = 1;
         }
       }
     }
     
 }

setCounter can have a value between 1 and 6, inclusive.

In the if(valueState == HIGH) block, you only deal with two of the setCounter variables.

You are not detecting edges, and incrementing setCounter only when there is a transition from released to switched for the pin that the switch is attached to. Why not?

You are not detecting edges, and incrementing variables in the if(valueState == HIGH) blocks. Why not?

You are not using the internal pullup resistors for the switch pins. Why not?