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