Pages: [1] 2   Go Down
Author Topic: Programming LCD display for swtching between inputs  (Read 1344 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 98
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys,

I am new to programming and need some help for my project.

I am doing a auto feeder and am using a 20 x 4 LCD display and 2 pushbuttons to input 3 information needed in the LCD display as follows:

(1) Number of times to feed per day (range from 2 to 4)
(2) Number of days to feed (range from 1 to 10)
(3) 1st timing for feed

So how the program works is user gives input (1), and program calculates interval accordingly (eg if input is 2, then interval will be calculated as 24/2 = 12 hr interval). The next input (2) gives no. of days for the feeding to go on with the specified interval. The last input (3) would give the 1st timing for the 1st feed and subsequent feeding will be calculated accordingly (eg if feeding interval 12 hr and 1st timing is 12:00, then subsequent timing will be 24:00, followed by 12:00 etc etc..) And at the specified timings, a servo motor will rotate.

I am just starting to program the code and have come up with a code (with references from online codes) to come up with the increment for the first input as follows:

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

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

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

// Variables will change:
 int buttonPushCounter = 2;   // counter for the number of button presses
 int buttonState6 = 0;         // current state of the button
 int buttonState7 = 0;         // current state of the button
 int lastButtonState = 0;     // previous state of the button
 
 void setup() {

// initialize the button pin as a input:
  
pinMode(buttonPin, INPUT);
pinMode(buttonPin1, INPUT);
  
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()
 {
 
  buttonState6 = digitalRead(buttonPin);  // read the pushbutton up input pin:
  if (buttonState6 != lastButtonState)     // compare the buttonState to its previous state
  {
    if (buttonState6 == HIGH)            // if the state has changed, increment the counter  
     {
      buttonPushCounter++;              // Increment counter by 1
      lcd.setCursor(5,0);              // Set cursor at 5th column of 1st row      
      lcd.print(buttonPushCounter);    // print the counter
      
      if (buttonPushCounter > 4)      // if counter more than 4
      {
        buttonPushCounter = 2;        // reset counter to 2
        lcd.print(buttonPushCounter);  // print counter value of 2
      }
     }  
      delay(50);
   }
  
   // save the current state as the last state, for next time through the loop
   lastButtonState = buttonState6;
 }

However, I do not know how to do a code for the switching of input ( eg to go from input(1) to input (2) to put in the values.

Any kind souls could assist me pls. Thanks!


Moderator edit: CODE TAGS added.
« Last Edit: January 20, 2013, 10:31:39 am by AWOL » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 553
Posts: 46283
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
const int  buttonPin = 6;    // the pin that the increment pushbutton is attached to
const int  buttonPin1 = 7;    // the pin that the set pushbutton is attached to
Giving these variables meaningful names would be a good start. Why not name them incrementPin and setPin, so that when you reference them later, they make sense?

Quote
int buttonState6 = 0;         // current state of the button
 int buttonState7 = 0;         // current state of the button
Now, this doesn't make sense. If the pin names are nothing and 1, the state names should be nothing and 1, too. Don't change conventions. Of course, incrementState and setState make a lot more sense.

Quote
However, I do not know how to do a code for the switching of input ( eg to go from input(1) to input (2) to put in the values.
The increment switch should increment a value. The set switch should copy that value to a variable that makes sense for the state. Of course, this means you need to keep track of the state (1, 2, or 3), too, just like buttonPushCounter (which is another horrid name).
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 98
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So what I have written so far just increases the number for the 1st input from 2-4 am I right?

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

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 553
Posts: 46283
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 67
Posts: 2946
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

United Kingdom
Offline Offline
Tesla Member
***
Karma: 220
Posts: 6587
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
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...
}
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 98
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
// 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
 }
 }
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 553
Posts: 46283
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
    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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 98
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh sorry about the comment. It was wrong.

Code:
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
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 553
Posts: 46283
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 67
Posts: 2946
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Solving one problem. Look at "frequencyoffeed (incrementCounter);" I moved the lcd.setcursor and blink functions, and it works like you want.
Code:
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.
« Last Edit: January 25, 2013, 04:17:03 pm by HazardsMind » Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 98
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Solving one problem. Look at "frequencyoffeed (incrementCounter);" I moved the lcd.setcursor and blink functions, and it works like you want.
Code:
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:

Code:
// 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
 }
 }
 
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 90
Posts: 3527
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

incrementCounter looks like it is a global variable.  If it is then changing it anywhere in the sketch is allowed.  When you say
Quote
I created a new daysincrementCounter and daysincrementState
how did you do it ?  Did you really create new ones and if so, how ?

Quote
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.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Queens, New York
Offline Offline
Faraday Member
**
Karma: 67
Posts: 2946
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 98
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
// 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
 }
 }
 
Logged

Pages: [1] 2   Go Up
Jump to: