Arduino LCD Scrolling Menu Item

Hello everyone,

For a project in uni i need to make an menu with rotary encoder to control some leds and measure distance. the menu items i need to use are longer than the 16 pixel lcs. My question is, how can i program that if the text is is longer than 16 chars, that the text automaticly will scroll to the left and back so you can easily read the menu item.

Is there an function in the liquidCrystal livrary that can do this?
or something with a for loop.

Thanks in advance.
Greetz Amaury Verschooren
Belgium

No, it does not. It has a scrollDisplayLeft but that sounds more like a shift/rotate function.

So it's up to you. Just print the text again after a while (use millis()) only go a char to the right.

If you have non-blocking code this will not be to hard. If you do have blocking code this can be a pain. So show us what you got :slight_smile:

here is my file
:slight_smile:

project.ino (7.4 KB)

Now posted like you are asked to in "How to use the forum"

//Ultrasone
  #include <NewPing.h>
  int trigPin = 3;
  int echoPin = 5;
  NewPing sonar(trigPin, echoPin, 500);

//Servo
  #include <Servo.h>
  Servo ServoMoter;
  int servo = 6;
  int valServo;

//Lcd
  #include <LiquidCrystal.h>
  const int rs = 12, en = 11, d4 = 10, d5 = 9, d6 = 8, d7 = 7;
  LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

//This shit
  #include <EEPROM.h>


//Rotary Enconder
  int rotaryA = 2;
  int rotaryB = 4;
  long rotaryCount = 1;
  long lastRotaryCount = 0;
  
//Leds
  int ledarray[] = {A2, A3, A4, A5, A6, A7};

//Button
  #define Button A1
  
  
//Variables
  int ButtonState;
  int lastButtonState;
  int state = 0;
  boolean Flag = false;
  int Hoek;
  int test;
  int test2;
 
  
//Menu opties
  char *MainMenu[] = {" Afstand Meten", 
                       ">Afstand Meten", 
                       " Hoek Meten",
                       ">Hoek Meten",};
  char *AfstandMeten[] = {">Minimum meetwaarde instellen",
                          ">Maximum meetwaarde instellen",
                          ">Bekijken verdeling over leds",
                          ">Meten activeren",
                          ">Terug naar hoofdmenu",
                          " Minimum meetwaarde instellen",
                          " Maximum meetwaarde instellen",
                          " Bekijken verdeling over leds",
                          " Meten activeren",
                          " Terug naar hoofdmenu"
                          };
  char * HoekMeten[] = {">Instellen Van De Hoek",
                        ">Hoek Activeren",
                        ">Terug Naar Hoofdmenu",
                        " Instellen Van De Hoek",
                        " Hoek Activeren",
                        " Terug Naar Hoofdmenu"};

void setup() {
  //lcd
  lcd.begin(16, 2);

  //button
  pinMode(Button, INPUT);
  //rotary
  pinMode(rotaryA, INPUT);
  pinMode(rotaryB, INPUT);
  
  //Leds
  for (int i = 0; i<6;i++){
  pinMode(ledarray[i], OUTPUT);
  }
  //Servo
  ServoMoter.attach(servo);
  
  attachInterrupt (0, rotaryTurn, RISING);
  Serial.begin(9600);
  lcd.setCursor(0,0);  
}

void loop() 
  {
  if(rotaryCount >2 && state == 0){
  rotaryCount = 1;
  }
  if(rotaryCount <1 && state == 0){
  rotaryCount = 2;
  }

  if (rotaryCount >3 && state == 1){
  rotaryCount = 1;
  }
  if (rotaryCount <1 && state == 1){
  rotaryCount = 3;
  }

  if (rotaryCount >180 && state == 2){
  rotaryCount = 1;
  }
  if (rotaryCount <1 && state == 2){
  rotaryCount = 180;
  }
  if (rotaryCount <1 && state == 4){
  rotaryCount = 5;
  }
  if (rotaryCount >5 && state == 4){
  rotaryCount = 1;
  }


  switch (state)
  {
  case 0:
  mainMenu(rotaryCount);
  break;

  case 1:
  analogWrite(ledarray[0], 0);
  SubHoekMeten(rotaryCount);
  break;

  case 2:
  Sub1Instellen_Van_Hoek(rotaryCount);
  break;

  case 3:
  Sub1Activeren_Van_Hoek(rotaryCount, Hoek, ServoMoter);
  break;

  case 4:
  SubAfstandMeten(rotaryCount);
  analogWrite(ledarray[0], 1023);
  break;
  }


    if (lastRotaryCount != rotaryCount) {
    
    lastRotaryCount = rotaryCount;
    }

}
void rotaryTurn(){
    if (digitalRead (rotaryB)) {
     rotaryCount--;
     Flag = false;
     }
  else {
     rotaryCount++;
     Flag = false;
     
  }
} 

void mainMenu(int aantalMenuOpties){

  ButtonState = analogRead(Button);
 
 
  if( aantalMenuOpties == 1){
      if (Flag == false)
  {
    Flag = true;
    lcd.clear();
  }
    lcd.setCursor(0,0);
    lcd.print(MainMenu[1]);
    lcd.setCursor(0,1);
    lcd.print(MainMenu[2]);
  }
  if( aantalMenuOpties == 2){
      if (Flag == false)
  {
    Flag = true;
    lcd.clear();
  }
    lcd.setCursor(0,0);
  lcd.print(MainMenu[0]);
  lcd.setCursor(0,1);
  lcd.print(MainMenu[3]);
  
  }
  if (aantalMenuOpties == 2 && ButtonState <10 && state == 0){
    rotaryCount = 1;
    state = 1;
  }
  if (aantalMenuOpties == 1 && ButtonState <10 && state == 0){
    rotaryCount = 1;
    state = 4;
  }
  
    
    

}
void SubAfstandMeten(int aantalMenuOpties){
  ButtonState = analogRead(Button);
  switch (aantalMenuOpties)
  {
  case 1:
    if (Flag == false){
       Flag = true;
       lcd.clear();}
    lcd.setCursor(0,0);
    lcd.print(AfstandMeten[0]);
    lcd.setCursor(0,1);
    lcd.print(AfstandMeten[6]);
  break;
  case 2:
    if (Flag == false){
       Flag = true;
       lcd.clear();
     }
    lcd.setCursor(0,0);
    lcd.print(AfstandMeten[1]);
    lcd.setCursor(0,1);
    lcd.print(AfstandMeten[7]);
  break;
  case 3:
    if (Flag == false){
       Flag = true;
       lcd.clear();
      }
    lcd.setCursor(0,0);
    lcd.print(AfstandMeten[2]);
    lcd.setCursor(0,1);
    lcd.print(AfstandMeten[8]);
  break;
  case 4:
    if (Flag == false){
       Flag = true;
       lcd.clear();
     }
    lcd.setCursor(0,0);
    lcd.print(AfstandMeten[3]);
    lcd.setCursor(0,1);
    lcd.print(AfstandMeten[9]); 
  break;
  case 5:
    if (Flag == false){
       Flag = true;
       lcd.clear();
     }
    lcd.setCursor(0,0);
    lcd.print(AfstandMeten[8]);
    lcd.setCursor(0,1);
    lcd.print(AfstandMeten[4]); 
  break;
  }
 
 /* if ( aantalMenuOpties == 1 && ButtonState < 10){
    Flag = false;
    state = 5;
  }
  if ( aantalMenuOpties == 2 && ButtonState < 10){

    Flag = false;
    state = 6;
  }
  if ( aantalMenuOpties == 3 && ButtonState < 10){

    Flag = false;
    state = 7;
  }
  if ( aantalMenuOpties == 4 && ButtonState < 10){

    Flag = false;
    state = 4;
  }*/
  if ( aantalMenuOpties == 5 && ButtonState < 10){

    Flag = false;
    state = 0;
  }
}
void SubHoekMeten(int aantalMenuOpties){
  
     ButtonState = analogRead(Button);
     if (ButtonState >50){
       test = 1;
     }
  switch (aantalMenuOpties)
  {
  case 1:
    if (Flag == false)
    {
    Flag = true;
    lcd.clear();
    }
    lcd.setCursor(0,0);
    lcd.print(HoekMeten[0]);
    lcd.setCursor(0,1);
    lcd.print(HoekMeten[4]);
    break;
  case 2:
    if (Flag == false)
    {
    Flag = true;
    lcd.clear();
    }
    lcd.setCursor(0,0);
    lcd.print(HoekMeten[1]);
    lcd.setCursor(0,1);
    lcd.print(HoekMeten[5]);
    break;
  case 3:
    if (Flag == false)
    {
    Flag = true;
    lcd.clear();
    }
    lcd.setCursor(0,0);
    lcd.print(HoekMeten[4]);
    lcd.setCursor(0,1);
    lcd.print(HoekMeten[2]);
    break;
            
  
  }

  if ( aantalMenuOpties == 1 && ButtonState < 10 && test == 1){

    Flag = false;
    state = 2;
  }
 

  
    if ( aantalMenuOpties == 2 && ButtonState < 10){

    Flag = false;
    state = 3;
  }

    if ( aantalMenuOpties == 3 && ButtonState < 10){
    Flag = false;
    state = 0;
    
  }
 

}
void Sub1Instellen_Van_Hoek(int aantalMenuOpties){
  if (ButtonState >50){
    test2 =1;
  }
   if (Flag == false)
  {
    Flag = true;
    lcd.clear();
  }
  ButtonState = analogRead(Button);
  lcd.setCursor(0, 0);
  lcd.print("Aantal Graden:");
  lcd.setCursor(0, 1);
  lcd.print(rotaryCount);
  if (test2 == 1){
  if (ButtonState < 10 && state == 2){
    Hoek = rotaryCount;
    state = 1;
    test=0;
    test2 = 0;
    
  }}
  

  
  
}
void Sub1Activeren_Van_Hoek(int aantalMenuOpties, int graden, Servo motor){
   ButtonState = analogRead(Button);
   motor.write(graden);
   state = 1;
 

}

First thing I notice is the indentation that's all over the place. Makes it very hard to read.

But I don't see a delay, which is good. But what you all want to do is very hard to read. To tackle this problem alone, make a MCVE.

PS:
Watch variable type. Is 'trigPin' ever going to be -30000? Aka, byte will do. Applies to almost all variables.

PPS:
Analog in is just an extra task for A pins. And they don't do PWM (analogWrite()). But the IDE does map analogWrite() to digital pins (HIGH for >512, LOW otherwise).

analogWrite(ledarray[0], 0);
// is the same as
digitalWrite(ledarray[0], LOW);

analogWrite(ledarray[0], 1023);
// is the same as 
digitalWrite(ledarray[0], HIGH);

PPPS:
Try to stick to a single name scheme. NowyouWrite_like_it. Common is cammelCaseForVariables (and functions), UpperCammelCaseForConstants (and classes).

PPPPS:
Try to use self explaining variable names. For example:
'Flag' => what does it flag?
'ledarray' => 'LedPins' (plural to indicate an array)
'test' => test what?
etc