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
}
}