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?
Time to add some debugging statements to your code.
Serial.println(setCounter);
before you read incrementPin. This will show what it is and hence which setting the increment pin should affect. Expect it not to be what you think it should be due to key bounce as I mentioned before. Fix that in a crude way by reading the button twice with delay(50) between the reads to allow the bouncing to stop. There is a much better way to do it but that can wait.
Serial.println() some messages at various parts of your code so that you can keep track of where it is when it is running and what the values of variables are. Just before the 'if' tests is a good place.
2 things about that.
(1) deal with the rollover before outputting dayCounter (same problem with resetting setCounter after outputting it)
(2) you are resetting freqCounter to 1 when dayCounter goes over 10. I am sure that you can see why that is wrong
Fix those problems then if it still not working set setCounter to a fixed value as if the button had been read and see what happens