Offline
Jr. Member
Karma: 0
Posts: 98
|
 |
« on: January 20, 2013, 05:03:05 am » |
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: // 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
Brattain Member
Karma: 311
Posts: 35471
Seattle, WA USA
|
 |
« Reply #1 on: January 20, 2013, 12:05:35 pm » |
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? 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. 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
Jr. Member
Karma: 0
Posts: 98
|
 |
« Reply #2 on: January 21, 2013, 11:19:43 pm » |
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
Brattain Member
Karma: 311
Posts: 35471
Seattle, WA USA
|
 |
« Reply #3 on: January 22, 2013, 06:15:20 am » |
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
Edison Member
Karma: 28
Posts: 1551
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #4 on: January 22, 2013, 08:24:06 am » |
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
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
United Kingdom
Offline
Faraday Member
Karma: 131
Posts: 4656
|
 |
« Reply #5 on: January 22, 2013, 09:14:35 am » |
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... }
|
|
|
|
|
Logged
|
Formal verification of safety-critical software, software development, and electronic design and prototyping. http://www.eschertech.com
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 98
|
 |
« Reply #6 on: January 25, 2013, 12:21:53 pm » |
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 } }
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 311
Posts: 35471
Seattle, WA USA
|
 |
« Reply #7 on: January 25, 2013, 12:34:35 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 98
|
 |
« Reply #8 on: January 25, 2013, 12:51:14 pm » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 311
Posts: 35471
Seattle, WA USA
|
 |
« Reply #9 on: January 25, 2013, 01:32:58 pm » |
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
Edison Member
Karma: 28
Posts: 1551
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #10 on: January 25, 2013, 04:14:36 pm » |
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.
|
|
|
|
« Last Edit: January 25, 2013, 04:17:03 pm by HazardsMind »
|
Logged
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 98
|
 |
« Reply #11 on: January 26, 2013, 05:37:46 am » |
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 } }
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 47
Posts: 1404
May all of your blinks be without delay
|
 |
« Reply #12 on: January 26, 2013, 08:03:07 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 28
Posts: 1551
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #13 on: January 26, 2013, 09:26:08 am » |
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
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 98
|
 |
« Reply #14 on: January 27, 2013, 02:24:42 am » |
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 } }
|
|
|
|
|
Logged
|
|
|
|
|
|