Go Down

Topic: Programming LCD display for swtching between inputs (Read 1 time) previous topic - next topic

kurtselva

Anyone can help?

I have made some changes to my codes. But I am still having problem when I press the set pushbutton.
It works fine till it blinks at the LCD position (15,0) but after that when I press the increment pushbutton to increase the values at the dayscounter, it goes back to the freqcounter and increases it. So the program does not run the code for the daycounter..

Anyone can help? The code is:
Code: [Select]
#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 freqstate = 0;                        // state of the frequency pushbutton (low or high)
int setstate = 0;
int daystate = 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()
{
   for ( int freqcounter = 2; freqcounter < 5; freqcounter ++)  // freq value from 2 to 4
   {
     freqstate = digitalRead (incrementPin);
       if (freqstate == HIGH)
       {
         lcd.setCursor (5,0);         // set at 5th column, 0th row
         lcd.blink();
         lcd.print(freqcounter);                                 // print value of freq
       
         if (freqcounter > 4)                                    // if freq value more than 4, reset to 2
         {
           freqcounter = 2;
         }
         delay(300);
       }
     }
     
     setstate = digitalRead(setPin);            // read the pushbutton set input pin:
     if (setstate == HIGH)                    // if the state has changed, increment the counter
    {
                                 
      lcd.setCursor(15,0);
      lcd.blink();
     
   for ( int daycounter = 1; daycounter < 11; daycounter ++)  // freq value from 2 to 4
   {
     daystate = digitalRead (incrementPin);
       if (daystate == HIGH)
       {
         lcd.setCursor (15,0);         // set at 5th column, 0th row
         lcd.blink();
         lcd.print(daycounter);                                 // print value of freq
       
         if (daycounter > 10)                                    // if freq value more than 4, reset to 2
         {
           daycounter = 1;
           
         }
         
         
         delay(300);
       }
     }
}
}

       
         
     

UKHeliBob

What is the for loop doing there ?

The idea using your 2 buttons is to use one of them to change what you are setting (frequency, days or first feed time) and the other one to change the value.  So, each time you press the set button the program knows which setting to change.

Code: [Select]
loop()
read the whatToSet button
if button is pressed
  update a state counter (1=frequency, 2=days, 3= first feed time)
  deal with rollover
  move the LCD cursor
end of if

read the setValue button
if button is pressed
  if state counter is 1
    update frequency
  end of if
  else
  if state counter is 2
    update days
  end of if
  else
  if state counter is 3
    update first feed time
  end of if
end of if
end of loop()

Get this much working and you are well on the way
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

kurtselva

Looks like this was what I was looking for..

But I am unsure of programming the updating part and the rollover..
Can I get an example please?

UKHeliBob

You are already doing the rollover
Code: [Select]

if (freqcounter > 4)                                    // if freq value more than 4, reset to 2
{
   freqcounter = 2;
}


As to updating you do something similar.
Code: [Select]
if button is pressed
  if state counter is 1
    frequency counter = frequency counter +1 (CLUE:there is a neat way of doing this)
    deal with rollover (as above) 
    show frequency counter on the LCD
  end of if


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

kurtselva

Oh so that is called rollover :)

How am I supposed to declare the state counter? As 1 = freq, 2 = day, 3 = time and son on...
So do I declare it as int statecounter = an array?
Or have differrent state counters for each input? Like statecounter1 = freq, statecounter2 = day and so one?

Go Up