Arduino+Stepper+LCD&Buttons

Guys can you help me. I'm sorry but i'm not good in speaking English. My system is consist of stepper motor, LCD and buttons. First I made them separately. I now have a menu in my LCD and I can navigate it using 4 buttons. I use this link "Tutorial: manage menu and LCD display with Arduino – Coagula – Giuseppe Di Cillo" as my reference. And i already know how to move or rotate my stepper motor. The problem is when i combine this two system.

My LCD menu has an option wherein the user will press right button or left button to increase or decrease a certain type of value. it goes from 0 to 1 to 2 to 3 and so on. But after i type the code

const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, 10,11,12,13);

My menu have a wrong code behavior. When i press the right button or left. The value that I want to increase goes from 0 to 2 to 4 to 6 to 8. It no increase 2 value which is wrong because I want it to increase by 1.

I'm using Arduino Duemilanove. Heres my code if you wanna look:

PART 1

#include <MenuBackend.h>
#include <LiquidCrystal.h>
#include <Stepper.h>

const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, 10,11,12,13);
const int Left = 6;
const int Right = 7;
const int Enter = 9;
const int Back = 8;
int lastButton = 0;

//VARIABLE FOR TIME INTERVAL
int startHour = 1; // start time in Time interval HOUR
int startMin = 0; // start time in Time interval MIN
int endHour=1;// end time in Time interval
int endMin = 0; // end time in Time interval
int numOfMeals=1; // number of meals in Time interval

//VARIABLE FOR SPECIFIC TIME
int numOfTime=1; // Number of time to feed the pet
int oneHour = 1;
int twoHour = 1;
int threeHour = 1;
int fourHour = 1;
int oneMin = 0;
int twoMin = 0;
int threeMin = 0;
int fourMin = 0;

int lastEnterState = LOW;
int lastBackState = LOW;
int lastLeftState = LOW;
int lastRightState = LOW;

long enterDebounceTime = 0;
long backDebounceTime = 0;
long leftDebounceTime = 0;
long rightDebounceTime = 0;
long debounceDelay = 300;


LiquidCrystal lcd(5,4,3,2,1,0);

MenuBackend menu = MenuBackend(menuUsed,menuChanged);
  MenuItem specificTime = MenuItem("Specific");
    MenuItem numberOfMeals = MenuItem("NumOfMeals");  
      MenuItem startTime = MenuItem("StartTime");
      MenuItem startTime2 = MenuItem("StartTime2");
        MenuItem endTime = MenuItem("EndTime");
        MenuItem endTime2 = MenuItem("EndTime2");
  MenuItem intervalTime = MenuItem("Interval");
    MenuItem numberOfTime = MenuItem("NumOfTime");
      MenuItem oneTime = MenuItem("OneTime");
        MenuItem oneTime2 = MenuItem("OneTime2");
      MenuItem twoTime = MenuItem("TwoTime");
        MenuItem twoTime2 = MenuItem("TwoTime2");
      MenuItem threeTime = MenuItem("ThreeTime");
         MenuItem threeTime2 = MenuItem("ThreeTime2");
      MenuItem fourTime = MenuItem("FourTime");
        MenuItem fourTime2 = MenuItem("FourTime2");
    
void setup(){
  pinMode(Left,INPUT);
  pinMode(Right,INPUT);
  pinMode(Enter,INPUT);
  pinMode(Back,INPUT);
  lcd.begin(16,2);
  
  menu.getRoot().add(specificTime).addRight(intervalTime);
  specificTime.add(numberOfMeals).add(startTime).add(startTime2).add(endTime).add(endTime2);
  intervalTime.add(numberOfTime).add(oneTime).add(oneTime2).add(twoTime).add(twoTime2).add(threeTime).add(threeTime2).add(fourTime).add(fourTime2);
  menu.toRoot();
}//end Setup

void loop()
{
  readButtons();
  navigateMenus();
}

void menuChanged(MenuChangeEvent changed){
  MenuItem newMenuItem = changed.to;
  lcd.setCursor(0,0);
  
  if(newMenuItem.getName() == menu.getRoot()){ //starting, Menu
    lcd.clear();
    lcd.print("Maja's PetFeeder");
    lcd.setCursor(0,1);
    lcd.print("Main Menu   v1.0");
  }else if(newMenuItem.getName() == "Specific"){ //When Specific TIme is choosed
    lcd.clear();
    lcd.print(">Time Interval");
    lcd.setCursor(0,1);
    lcd.print(" Specific Time");
  }else if(newMenuItem.getName() == "NumOfMeals"){//When press Enter while in Specific Time
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Number Of Meals");
    lcd.setCursor(0,1);
    lcd.print("N = 1");
  }else if(newMenuItem.getName() == "StartTime"){
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Start Time(Hour)");
    lcd.setCursor(5,1);
    lcd.print("01:00");
  }else if(newMenuItem.getName() == "StartTime2"){
    lcd.setCursor(0,0);
    lcd.print("Start Time(Min) ");
  }else if(newMenuItem.getName() == "EndTime"){
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("End Time(Hour)  ");
    lcd.setCursor(5,1);
    lcd.print("01:00");
  }else if(newMenuItem.getName() == "EndTime2"){
    lcd.setCursor(0,0);
    lcd.print("End Time(Min)   ");
  }else if(newMenuItem.getName() == "Interval"){
    lcd.clear();
    lcd.print(" Time Interval");
    lcd.setCursor(0,1);
    lcd.print(">Specific Time");
  }else if(newMenuItem.getName() == "NumOfTime"){
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Number Of Time");
    lcd.setCursor(0,1);
    lcd.print("T = 1");
  }else if(newMenuItem.getName() == "OneTime"){
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("FirstTime(Hour)");
    lcd.setCursor(5,1);
    lcd.print("01:00");
  }else if(newMenuItem.getName() == "OneTime2"){
    lcd.setCursor(0,0);
    lcd.print("FirstTime(Min)  ");
  }else if(newMenuItem.getName() == "TwoTime"){
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("SecondTime(Hour)");
    lcd.setCursor(5,1);
    lcd.print("01:00");
  }else if(newMenuItem.getName() == "TwoTime2"){
    lcd.setCursor(0,0);
    lcd.print("SecondTime(Min) ");
  }else if(newMenuItem.getName() == "ThreeTime"){
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("ThirdTime(Hour) ");
    lcd.setCursor(5,1);
    lcd.print("01:00");
  }else if(newMenuItem.getName() == "ThreeTime2"){
    lcd.setCursor(0,0);
    lcd.print("ThirdTime(Min)  ");
  }else if(newMenuItem.getName() == "FourTime"){
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("FourthTime(Hour)");
    lcd.setCursor(5,1);
    lcd.print("01:00");
  }else if(newMenuItem.getName() == "FourTime2"){
    lcd.setCursor(0,0);
    lcd.print("FourthTime(Min) ");
  }
}//END menuChanged

void menuUsed(MenuUseEvent used){
  lcd.clear();
   if(used.item.getName()=="EndTime2"){
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Meals = ");
    lcd.print(numOfMeals);
    lcd.setCursor(0,1);
    lcd.print("Time=");
    lcd.print(startHour);
    lcd.print(":");
    lcd.print(startMin);
    lcd.print("-");
    lcd.print(endHour);
    lcd.print(":");
    lcd.print(endMin);
    delay(5000);
  }
   if(startHour > endHour)
     {
       lcd.clear();
       lcd.print("Error!");
       delay(1000);
       menu.toRoot();
     }else{
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Thank You!      ");
  delay(2000);
  lcd.setCursor(0,1);
  lcd.print("         Maja :)");
  delay(3000);
  menu.toRoot();
     }
  
}//end menu used

PART2

void readButtons(){
  int reading;
  int enterState = HIGH;
  int backState = HIGH;
  int leftState = HIGH;
  int rightState = HIGH;
  
  //Enter button
    reading = digitalRead(Enter);//if button enter is pressed
    if(reading != lastEnterState){
      enterDebounceTime = millis();
    }
    if((millis() - enterDebounceTime) > debounceDelay){
      enterState = reading;
      enterDebounceTime = millis();
    }
    lastEnterState = reading;
    
  //Esc button
    reading = digitalRead(Back);
    if(reading != lastBackState){
      backDebounceTime = millis();
    }
    if((millis() - backDebounceTime) > debounceDelay){
      backState = reading;
      backDebounceTime = millis();
    }
    lastBackState = reading;
    
   //Right button
    reading = digitalRead(Right);
    if(reading != lastRightState){
      rightDebounceTime = millis();
    }
    if((millis() - rightDebounceTime) > debounceDelay){
      rightState = reading;
      rightDebounceTime = millis();
    }
    lastRightState = reading;
    
   //Left button
    reading = digitalRead(Left);
    if(reading != lastLeftState){
      leftDebounceTime = millis();
    }
    if((millis() - leftDebounceTime) > debounceDelay){
      leftState = reading;
      leftDebounceTime = millis();
    }
    lastLeftState = reading;
    
    if(enterState ==LOW){
      lastButton = Enter;
    }
    else if(backState==LOW){
      lastButton = Back;
    }
    else if(rightState==LOW){
      lastButton = Right;
    }
    else if(leftState==LOW){
      lastButton = Left;
    }
    else{
      lastButton = 0;
    }
}//end read button

PART3

void navigateMenus(){
  MenuItem currentMenu=menu.getCurrent();
  
  switch(lastButton){
    case Enter:
   /* if(currentMenu.getName()=="EndTime2" && startHour>=endHour){
          lcd.clear();
          lcd.print("Error! Try again!");
          lcd.setCursor(0,1);
          lcd.print("Start > Time");
         
        }
      else */if(currentMenu.getName()=="NumOfTime" ||currentMenu.getName()=="OneTime2" || currentMenu.getName()=="TwoTime2" || currentMenu.getName()=="ThreeTime2" || currentMenu.getName()=="FourTime2"){
      numOfTime = numOfTime - 1;
      menu.moveDown();
      if(numOfTime < 0){
        menu.use();
      }
      }else if(!(currentMenu.moveDown())){
        menu.use();
      }else{
        menu.moveDown();
      }
      break;//end case Enter
    case Back:
      menu.toRoot();
      break;//end case Back
    case Right:
      if(currentMenu.getName() == "NumOfMeals" && numOfMeals<4) // In setting the number of meals
      {
        numOfMeals=numOfMeals+1; //if Right button is press Meals+1
        lcd.setCursor(4,1);
        lcd.print(numOfMeals);
      }
      else if(currentMenu.getName() == "StartTime" && startHour<24){//Starting Time of time interval
          int x;
          if(startHour>8){
          x=5;
          }else{
            x=6;
          }
         startHour = startHour + 1; 
         lcd.setCursor(x,1);
         lcd.print(startHour);
          
      }else if(currentMenu.getName() == "StartTime2" && startMin<50){ // setting the minutes
        startMin = startMin +10;
        lcd.setCursor(8,1);
        lcd.print(startMin);
      }else if(currentMenu.getName() == "EndTime" && endHour<24){//Starting Time of time interval
          int x;
          if(endHour>8){
          x=5;
          }else{
            x=6;
          }
         endHour = endHour + 1; 
         lcd.setCursor(x,1);
         lcd.print(endHour);
          
      }else if(currentMenu.getName() == "EndTime2" && endMin<50){ // setting the minutes
        endMin = endMin +10;
        lcd.setCursor(8,1);
        lcd.print(endMin);
      }else if(currentMenu.getName() == "NumOfTime" && numOfTime<4){ // In setting the number of Time
        numOfTime=numOfTime+1; //if Right button is press Time+1
        lcd.setCursor(4,1);
        lcd.print(numOfTime);
      }else if(currentMenu.getName() == "OneTime" && oneHour<24){//Starting Time of time interval
          int x;
          if(oneHour>8){
          x=5;
          }else{
            x=6;
          }
         oneHour = oneHour + 1; 
         lcd.setCursor(x,1);
         lcd.print(oneHour);
          
      }else if(currentMenu.getName() == "OneTime2" && oneMin<50){ // setting the minutes
        oneMin = oneMin +10;
        lcd.setCursor(8,1);
        lcd.print(oneMin);
      }else if(currentMenu.getName() == "TwoTime" && twoHour<24){//Starting Time of time interval
          int x;
          if(twoHour>8){
          x=5;
          }else{
            x=6;
          }
         twoHour = twoHour + 1; 
         lcd.setCursor(x,1);
         lcd.print(twoHour);
          
      }else if(currentMenu.getName() == "TwoTime2" && twoMin<50){ // setting the minutes
        twoMin = twoMin +10;
        lcd.setCursor(8,1);
        lcd.print(twoMin);
      }else if(currentMenu.getName() == "ThreeTime" && threeHour<24){//Starting Time of time interval
          int x;
          if(threeHour>8){
          x=5;
          }else{
            x=6;
          }
         threeHour = threeHour + 1; 
         lcd.setCursor(x,1);
         lcd.print(threeHour);
          
      }else if(currentMenu.getName() == "ThreeTime2" && threeMin<50){ // setting the minutes
        threeMin = threeMin +10;
        lcd.setCursor(8,1);
        lcd.print(threeMin);
      }else if(currentMenu.getName() == "FourTime" && fourHour<24){//Starting Time of time interval
          int x;
          if(fourHour>8){
          x=5;
          }else{
            x=6;
          }
         fourHour = fourHour + 1; 
         lcd.setCursor(x,1);
         lcd.print(fourHour);
          
      }else if(currentMenu.getName() == "FourTime2" && fourMin<50){ // setting the minutes
        fourMin = fourMin +10;
        lcd.setCursor(8,1);
        lcd.print(fourMin);
      }else{
        menu.moveRight();
      }
     
      break;//end case Right
    case Left:
     if(currentMenu.getName() == "NumOfMeals" && numOfMeals>1) // Int setting the number of Meals
      {
        numOfMeals=numOfMeals-1; //if Left button is press Meals-1
        lcd.setCursor(4,1);
        lcd.print(numOfMeals);
      }  
      else if(currentMenu.getName() == "StartTime" && startHour>1){//Starting Time of time interval
          int x;
          if(startHour==10){
           lcd.setCursor(5,1);
           lcd.print("0");
           x=6;
          }
          else if(startHour<11){
          x=6;
          }else if(startHour>8){
            x=5;
          }
         startHour = startHour - 1; 
         lcd.setCursor(x,1);
         lcd.print(startHour);
      }else if(currentMenu.getName() == "StartTime2" && startMin>0){ // setting the minutes
        startMin = startMin -10;
        lcd.setCursor(8,1);
        lcd.print(startMin);
      }else if(currentMenu.getName() == "EndTime" && endHour>1){//Starting Time of time interval
          int x;
          if(endHour==10){
           lcd.setCursor(5,1);
           lcd.print("0");
           x=6;
          }
          else if(endHour<11){
          x=6;
          }else if(endHour>8){
            x=5;
          }
         endHour = endHour - 1; 
         lcd.setCursor(x,1);
         lcd.print(endHour);
      }else if(currentMenu.getName() == "EndTime2" && endMin>0){ // setting the minutes
        endMin = endMin -10;
        lcd.setCursor(8,1);
        lcd.print(endMin);
      }else if(currentMenu.getName() == "OneTime" && oneHour>1){//Starting Time of time interval
          int x;
          if(oneHour==10){
           lcd.setCursor(5,1);
           lcd.print("0");
           x=6;
          }
          else if(oneHour<11){
          x=6;
          }else if(oneHour>8){
            x=5;
          }
         oneHour = oneHour - 1; 
         lcd.setCursor(x,1);
         lcd.print(oneHour);
      }else if(currentMenu.getName() == "OneTime2" && oneMin>0){ // setting the minutes
        oneMin = oneMin -10;
        lcd.setCursor(8,1);
        lcd.print(oneMin);
      }else if(currentMenu.getName() == "TwoTime" && twoHour>1){//Starting Time of time interval
          int x;
          if(twoHour==10){
           lcd.setCursor(5,1);
           lcd.print("0");
           x=6;
          }
          else if(twoHour<11){
          x=6;
          }else if(twoHour>8){
            x=5;
          }
         twoHour = twoHour - 1; 
         lcd.setCursor(x,1);
         lcd.print(twoHour);
      }else if(currentMenu.getName() == "TwoTime2" && twoMin>0){ // setting the minutes
        twoMin = twoMin -10;
        lcd.setCursor(8,1);
        lcd.print(twoMin);
      }else if(currentMenu.getName() == "ThreeTime" && threeHour>1){//Starting Time of time interval
          int x;
          if(threeHour==10){
           lcd.setCursor(5,1);
           lcd.print("0");
           x=6;
          }
          else if(threeHour<11){
          x=6;
          }else if(threeHour>8){
            x=5;
          }
         threeHour = threeHour - 1; 
         lcd.setCursor(x,1);
         lcd.print(threeHour);
      }else if(currentMenu.getName() == "ThreeTime2" && threeMin>0){ // setting the minutes
        threeMin = threeMin -10;
        lcd.setCursor(8,1);
        lcd.print(threeMin);
      }else if(currentMenu.getName() == "FourTime" && fourHour>1){//Starting Time of time interval
          int x;
          if(fourHour==10){
           lcd.setCursor(5,1);
           lcd.print("0");
           x=6;
          }
          else if(fourHour<11){
          x=6;
          }else if(fourHour>8){
            x=5;
          }
         fourHour = fourHour - 1; 
         lcd.setCursor(x,1);
         lcd.print(fourHour);
      }else if(currentMenu.getName() == "FourTime2" && fourMin>0){ // setting the minutes
        fourMin = fourMin -10;
        lcd.setCursor(8,1);
        lcd.print(fourMin);
      }else if(currentMenu.getName() == "NumOfTime" && numOfTime>1){ // In setting the number of Time
        numOfTime=numOfTime-1; //if Right button is press Time+1
        lcd.setCursor(4,1);
        lcd.print(numOfTime);
      }else{
      menu.moveLeft();
      }
      break;//end case Right
  }//end switch
  
  lastButton=0;
}//end navigate Menu

If you want to know how my proper menu work please remove this code:

const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, 10,11,12,13);

Please help me. :frowning:

With all those string literals, I suspect you are running out of RAM. To test this theory, try making some of the strings shorter as a temporary measure. If this fixes it, you need to move as many of those string literals into PROGMEM as you reasonably can.

dc42:
With all those string literals, I suspect you are running out of RAM. To test this theory, try making some of the strings shorter as a temporary measure. If this fixes it, you need to move as many of those string literals into PROGMEM as you reasonably can.

How can i make strings shorter. So my problem is the memory of my Arduino? What if i use Arduino Uno which has higher memory? will it fix my problem?

janno0718:
How can i make strings shorter.

For example, temporarily change "TwoTime" to "2T" throughout, and similarly for some of the other strings.

janno0718:
So my problem is the memory of my Arduino?

My strong suspicion is that you are running out of RAM.

janno0718:
What if i use Arduino Uno which has higher memory? will it fix my problem?

If your Duemilanove is the older variety with the atmega168 processor, then the Uno does indeed have twice the amount of RAM, and upgrading to it will probably fix your problem. Or you might be able to upgrade your Duemilanove to atmega328p.

Sir another problem. When i use both system it seems like the voltage that is delivered is being divided into two. When the stepper is not rotating the LCD is working in its full power, but when the time comes that the stepper motor will rotate the LCD screen will become dimmer and sometimes its display will be gone.

I'm using an adaptor as my external power. 6V. Do you know what is the problem?
I'm using stepper driver L293D

6v is not really enough to power the Arduino, because its voltage regulator is not of the low-dropout variety. My guess is that with the stepper not energised, you are getting 7v or more from the adapter, but this is dropping to 6v or less when the stepper is energised. You can check this with a multimeter.

Either use an external supply that can provide more voltage (e.g. 7.5v or 9v), or use a low dropout regulator to provide 5v to the Arduino from the 6v supply instead of powering the Arduino directly from the 6v supply.

Did you have a resistor in series with the LCD back light? Typically the LCD back light should be dim. It is only for reading in the dark, you should not use the back light as a color lamp for visual appeal. If it is the LCD back light that gets dimmer (you didn't say if the back light or the contrast gets changed), then you should reduce current to the back light. What dc42 mentioned should be first addressed.