Rotary+20x4 LCD

Hi everyone!
I am programming lcd+rotary with center switch. I am tring to write values at a specific place on lcd screen. For ''setting1'', it prints at the right cases: (7,1). For ''setting2'', it prints on (7,0) instead of (7,4). It seems like the case (7,4) don't want values on it

Sea the code line 157 to 171

Also I would like a suggestion to show the values on the lcd in real time while turning the rotary to select value.

//********************WORKS ON SERIAL MONITOR****************
/*******Interrupt-based Rotary Encoder Menu Sketch*******
 * by Simon Merrett, based on insight from Oleg Mazurov, Nick Gammon, rt and Steve Spence, and code from Nick Gammon
 * 3,638 bytes with debugging on UNO, 1,604 bytes without debugging
 */

 //Ajout mon lcd
 #include <LiquidCrystal_I2C.h> 
LiquidCrystal_I2C lcd(0x27, 20, 4);

// Rotary encoder declarations
static int pinA = 2; // Our first hardware interrupt pin is digital pin 2
static int pinB = 3; // Our second hardware interrupt pin is digital pin 3
volatile byte aFlag = 0; // let's us know when we're expecting a rising edge on pinA to signal that the encoder has arrived at a detent
volatile byte bFlag = 0; // let's us know when we're expecting a rising edge on pinB to signal that the encoder has arrived at a detent (opposite direction to when aFlag is set)
volatile byte encoderPos = 0; //this variable stores our current value of encoder position. Change to int or uin16_t instead of byte if you want to record a larger range than 0-255
volatile byte oldEncPos = 0; //stores the last encoder position value so we can compare to the current reading and see if it has changed (so we know when to print to the serial monitor)
volatile byte reading = 0; //somewhere to store the direct values we read from our interrupt pins before checking to see if we have moved a whole detent
// Button reading, including debounce without delay function declarations
const byte buttonPin = 4; // this is the Arduino pin we are connecting the push button to
byte oldButtonState = HIGH;  // assume switch open because of pull-up resistor
const unsigned long debounceTime = 10;  // milliseconds
unsigned long buttonPressTime;  // when the switch last changed state
boolean buttonPressed = 0; // a flag variable
// Menu and submenu/setting declarations
byte Mode = 0;   // This is which menu mode we are in at any given time (top level or one of the submenus)
const byte modeMax = 4; // This is the number of submenus/settings you want
byte setting1 = 0;  // a variable which holds the value we set 
byte setting2 = 0;  // a variable which holds the value we set 
boolean setting3 = 0;  // a variable which holds the value we set 
boolean setting4 = 0;  // a variable which holds the value we set 
/* Note: you may wish to change settingN etc to int, float or boolean to suit your application. 
 Remember to change "void setAdmin(byte name,*BYTE* setting)" to match and probably add some 
 "modeMax"-type overflow code in the "if(Mode == N && buttonPressed)" section*/

void setup() {
  //Rotary encoder section of setup
  pinMode(pinA, INPUT_PULLUP); // set pinA as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
  pinMode(pinB, INPUT_PULLUP); // set pinB as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
  attachInterrupt(0,PinA,RISING); // set an interrupt on PinA, looking for a rising edge signal and executing the "PinA" Interrupt Service Routine (below)
  attachInterrupt(1,PinB,RISING); // set an interrupt on PinB, looking for a rising edge signal and executing the "PinB" Interrupt Service Routine (below)
  // button section of setup
  pinMode (buttonPin, INPUT_PULLUP); // setup the button pin
  // DEBUGGING section of setup
  Serial.begin(9600);     // DEBUGGING: opens serial port, sets data rate to 9600 bps

  //démarre LCD
  lcd.init();                      // initialize the lcd   
  lcd.backlight();
  //message power ''on'' sur LCD
  lcd.setCursor(5,0); //Defining positon to write from first row, first column .
  lcd.print("CHARCUPRO");
  lcd.setCursor(3,2);
  lcd.print("Version 1.1.1");

  lcd.setCursor(0,2); //Second row, first column
  lcd.print(""); 
  delay(3000); //wait 8 sec
  lcd.clear(); //clear the whole LCD
   lcd.setCursor(1,1); 
  lcd.print("TEMP"); //text
  //----------------------
  lcd.setCursor(1,3); 
  lcd.print("HUMI"); //text
  //----------------------
  lcd.setCursor(13,1); 
  lcd.print("FAN"); //text
  //----------------------
  lcd.setCursor(13,3); 
  lcd.print("UV"); //text
  //----------------------
    lcd.setCursor(9,1); 
  lcd.print("C"); //text
  //----------------------
  lcd.setCursor(9,3); 
  lcd.print("%"); //text
  //----------------------
  lcd.setCursor(17,1); 
  lcd.print("0/1"); //text
  //----------------------
  lcd.setCursor(17,3); 
  lcd.print("0/1"); //text
  //----------------------
  
}

//void printLCD(){
  //These are the values which are not changing the operation
  
//}
void loop() {
  rotaryMenu();
  // carry out other loop code here 
 
}

void rotaryMenu() {  //This handles the bulk of the menu functions without needing to install/include/compile a menu library

     
  //DEBUGGING: Rotary encoder update display if turned
   if(oldEncPos != encoderPos) { // DEBUGGING
    Serial.println(encoderPos);// DEBUGGING. Sometimes the serial monitor may show a value just outside modeMax due to this function. The menu shouldn't be affected.
    oldEncPos = encoderPos;// DEBUGGING
    
    }
  // DEBUGGING
  // Button reading with non-delay() debounce - thank you Nick Gammon!
  byte buttonState = digitalRead (buttonPin); //toujours lire si bouton pesé ou pas
  if (buttonState != oldButtonState){
    if (millis () - buttonPressTime >= debounceTime){ // debounce
      buttonPressTime = millis ();  // when we closed the switch 
      oldButtonState =  buttonState;  // remember for next time 
      if (buttonState == LOW){
        Serial.println ("Button was pressed"); // DEBUGGING: print that button has been closed
        buttonPressed = 1;
      }
      else {
        Serial.println ("Button opened"); // DEBUGGING: print that button has been opened
        buttonPressed = 0;  
      }  
    }  // end if debounce time up
  } // end of  button state change

  //Main menu section
  if (Mode == 0) {
    if (encoderPos > (modeMax+10)) encoderPos = modeMax; // check we haven't gone out of bounds below 0 and correct if we have
    else if (encoderPos > modeMax) encoderPos = 0; // check we haven't gone out of bounds above modeMax and correct if we have
    if (buttonPressed){ 
      Mode = encoderPos; // set the Mode to the current value of input if button has been pressed
      Serial.print("Mode selected: "); //DEBUGGING: print which mode has been selected
      Serial.println(Mode); //DEBUGGING: print which mode has been selected
      buttonPressed = 0; // reset the button status so one press results in one action

      if (Mode == 1){
        Serial.println("TEMP_Mode 1"); //DEBUGGING: print which mode has been selected
        encoderPos = setting1; // start adjusting TEMP from last set point
        
      }
      if (Mode == 2) {
        Serial.println("HUMID_Mode 2"); //DEBUGGING: print which mode has been selected
        encoderPos = setting2; // start adjusting HUMIDITY from last set point
        
      }
      if (Mode == 3) {
        Serial.println("FAN_Mode 3"); //DEBUGGING: print which mode has been selected
        encoderPos = setting3; // start turning FAN on or off
       
      }
      if (Mode == 4) {
        Serial.println("UV_Mode 4"); //DEBUGGING: print which mode has been selected
        encoderPos = setting4; // start turning UV on or off
       
      }
    }   
  }
   
  if (Mode == 1 && buttonPressed) {
    setting1 = encoderPos; // record whatever value your encoder has been turned to, to setting 1
    setAdmin(1,setting1);
    //code to do other things with setting1 here, perhaps update display 
    lcd.setCursor(7,1); 
    lcd.print(setting1);
    
  }
   
  if (Mode == 2 && buttonPressed) {
    setting2 = encoderPos; // record whatever value your encoder has been turned to, to setting 2
    setAdmin(2,setting2);
    //code to do other things with setting1 here, perhaps update display
    lcd.setCursor(7,4); 
    lcd.print("setting2");
   
  }
  if (Mode == 3 && buttonPressed){
    setting3 = encoderPos; // record whatever value your encoder has been turned to, to setting 3
    setAdmin(3,setting3);
    //code to do other things with setting3 here, perhaps update display 
  }
  if (Mode == 4 && buttonPressed){
    setting3 = encoderPos; // record whatever value your encoder has been turned to, to setting 4
    setAdmin(4,setting4);
    
    //code to do other things with setting4 here, perhaps update display 
  }
} 


// Carry out common activities each time a setting is changed
void setAdmin(byte name, byte setting){
  Serial.print("Setting "); //DEBUGGING
  Serial.print(name); //DEBUGGING
  Serial.print(" = "); //DEBUGGING
  Serial.println(setting);//DEBUGGING
  encoderPos = 0; // reorientate the menu index - optional as we have overflow check code elsewhere
  buttonPressed = 0; // reset the button status so one press results in one action
  Mode = 0; // go back to top level of menu, now that we've set values
  Serial.println("Main Menu"); //DEBUGGING
}

//Rotary encoder interrupt service routine for one encoder pin
void PinA(){
  cli(); //stop interrupts happening before we read pin values
  reading = PIND & 0xC; // read all eight pin values then strip away all but pinA and pinB's values
  if(reading == B00001100 && aFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
    encoderPos --; //decrement the encoder's position count
    bFlag = 0; //reset flags for the next turn
    aFlag = 0; //reset flags for the next turn
  }
  else if (reading == B00000100) bFlag = 1; //signal that we're expecting pinB to signal the transition to detent from free rotation
  sei(); //restart interrupts
}

//Rotary encoder interrupt service routine for the other encoder pin
void PinB(){
  cli(); //stop interrupts happening before we read pin values
  reading = PIND & 0xC; //read all eight pin values then strip away all but pinA and pinB's values
  if (reading == B00001100 && bFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
    encoderPos ++; //increment the encoder's position count
    bFlag = 0; //reset flags for the next turn
    aFlag = 0; //reset flags for the next turn
  }
  else if (reading == B00001000) aFlag = 1; //signal that we're expecting pinA to signal the transition to detent from free rotation
  sei(); //restart interrupts
}
// end of sketch!

I see no code

Did you forget to post it ?

Much more likely, there is an error in the code you forgot to post.

Now it should work to see the code

Sry, responding to thread not @jremington

Here's a thought. Are the rows numbered 1 .. 4 or 0 .. 3?

a7

You got it me idiot... Ikew it is 0-3 and not 1-4 !!! Thank you!!

By the way, any idea of how to print values on lcd in real time while turning rotary^

Typically with changing information ppl make the mistake of clearing the screen and rewriting everything.

You can do better… just set the cursor and print enough spaces to complete erase the previous number or information, then set the cursor back and print the new data.

This will be way easier, much faster and with a bit of care like not doing that too rapidly, avoid aesthetically unpleasant display artifacts.

HTH

a7

but by just printing over the digits on the same case by turning the rotary fast, do I really need to program an "erase" between every fast digit change? Can I not just trust on the overwrite until I click to select value?

You only need to have new data length equal old data length to print new data cleanly.

What happens when old number is 1000 and new number is 999?

Check out snprintf.

Yes. I was thinking of graphic displays.

Even better.

a7

I am playin betwwen 0 and 35, but still...the same. got ya. will programm the write and erase function.
Thanks

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.