element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Arduino LCD 20x4 I2C problem
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Suggested Answer
  • Replies 17 replies
  • Answers 2 answers
  • Subscribers 393 subscribers
  • Views 3331 views
  • Users 0 members are here
  • i2c
  • 20x4
  • led
  • arduino
Related

Arduino LCD 20x4 I2C problem

Former Member
Former Member over 11 years ago

Hi

 

I have some problem with geting my code to work after a changed my LCD from a 16x2 to a 20x4 with I2C inteface.

 

If i try the sample code below the new LCD works:

 

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR    0x3F
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7

int n = 1;

LiquidCrystal_I2C        lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

void setup()
{
lcd.begin (20,4); // <<----- My LCD was 16x2

 
// Switch on the backlight
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
lcd.home (); // go home

lcd.print("SainSmartI2C16x2");
}

void loop()
{
// Backlight on/off every 3 seconds
lcd.setCursor (0,1);        // go to start of 2nd line
lcd.print(n++,DEC);
lcd.setBacklight(LOW);      // Backlight off
delay(3000);
lcd.setBacklight(HIGH);     // Backlight on
delay(3000);
}

 

 

But when I try to do it with my own code I just got 2 rows with "#"

 

I use this library:

LiquidCrystal_V1.2.1 - https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads

MenuBackEnd V1.4 - http://www.arduino.cc/playground/uploads/Profiles/MenuBackend_1-4.zip

 

 

Here is my code that I can't get to work.

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <MenuBackend.h>


#define I2C_ADDR    0x3F
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7


const int buttonPinLeft = 8;      // pin for the Up button
const int buttonPinRight = 9;    // pin for the Down button
const int buttonPinEsc = 10;     // pin for the Esc button
const int buttonPinEnter = 11;   // pin for the Enter button


int lastButtonPushed = 0;


int lastButtonEnterState = LOW;   // the previous reading from the Enter input pin
int lastButtonEscState = LOW;   // the previous reading from the Esc input pin
int lastButtonLeftState = LOW;   // the previous reading from the Left input pin
int lastButtonRightState = LOW;   // the previous reading from the Right input pin


long lastEnterDebounceTime = 0;  // the last time the output pin was toggled
long lastEscDebounceTime = 0;  // the last time the output pin was toggled
long lastLeftDebounceTime = 0;  // the last time the output pin was toggled
long lastRightDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 500;    // the debounce time


int Motor1=1;
int Motor2=0;
int Backlight=255;


String CWCCW, CWCCW2, TPD, TPD2;



LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);




//Menu variables


MenuBackend menu = MenuBackend(menuUsed,menuChanged);


//initialize menuitems
    MenuItem Item1 = MenuItem("Item1"); //Motor 1 Settings


  MenuItem Item1Sub1 = MenuItem("Item1Sub1"); //TPD
  MenuItem Item1Sub1Sub1 = MenuItem("Item1Sub1Sub1"); //650
  MenuItem Item1Sub1Sub2 = MenuItem("Item1Sub1Sub2"); //750
  MenuItem Item1Sub1Sub3 = MenuItem("Item1Sub1Sub3"); //850
  MenuItem Item1Sub1Sub4 = MenuItem("Item1Sub1Sub4"); //950


  MenuItem Item1Sub2 = MenuItem("Item1Sub2");
  MenuItem Item1Sub2Sub1 = MenuItem("Item1Sub2Sub1"); //CW
  MenuItem Item1Sub2Sub2 = MenuItem("Item1Sub2Sub2"); //CCW
  MenuItem Item1Sub2Sub3 = MenuItem("Item1Sub2Sub3"); //Both
     
  MenuItem Item1Sub3 = MenuItem("Item1Sub3"); 
  MenuItem Item1Sub3Sub1 = MenuItem("Item1Sub3Sub1"); //Stop
  MenuItem Item1Sub3Sub2 = MenuItem("Item1Sub3Sub2"); //Start
     
  MenuItem Item2 = MenuItem("Item2"); //Motor 2 Settings

  MenuItem Item2Sub1 = MenuItem("Item2Sub1"); //TPD
  MenuItem Item2Sub1Sub1 = MenuItem("Item2Sub1Sub1"); //650
  MenuItem Item2Sub1Sub2 = MenuItem("Item2Sub1Sub2"); //750
  MenuItem Item2Sub1Sub3 = MenuItem("Item2Sub1Sub3"); //850
  MenuItem Item2Sub1Sub4 = MenuItem("Item2Sub1Sub4"); //950


  MenuItem Item2Sub2 = MenuItem("Item2Sub2");
  MenuItem Item2Sub2Sub1 = MenuItem("Item2Sub2Sub1"); //CW
  MenuItem Item2Sub2Sub2 = MenuItem("Item2Sub2Sub2"); //CCW
  MenuItem Item2Sub2Sub3 = MenuItem("Item2Sub2Sub3"); //Both
     
  MenuItem Item2Sub3 = MenuItem("Item2Sub3");
  MenuItem Item2Sub3Sub1 = MenuItem("Item2Sub3Sub1"); //Stop
  MenuItem Item2Sub3Sub2 = MenuItem("Item2Sub3Sub2"); //Start


  MenuItem Item3 = MenuItem("Item3"); //Backlight
  MenuItem Item3Sub1 = MenuItem("Item3Sub1"); //25%
  MenuItem Item3Sub2 = MenuItem("Item3Sub2"); //50%
  MenuItem Item3Sub3 = MenuItem("Item3Sub3"); //75%
  MenuItem Item3Sub4 = MenuItem("Item3Sub4"); //100%



void setup()
{
  lcd.begin (20,4); //  <<----- My LCD was 16x2
  // Switch on the backlight
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.home (); // go home
  lcd.print("SainSmart"); 


  CWCCW = String("Both");
  CWCCW2 = String(" CCW");
    TPD = String("650");
  TPD2 = String("850");

  pinMode(buttonPinLeft, INPUT);
  pinMode(buttonPinRight, INPUT);
  pinMode(buttonPinEnter, INPUT);
  pinMode(buttonPinEsc, INPUT);




  //configure menu
  menu.getRoot().add(Item1);

  Item1.addRight(Item2).addRight(Item3);

  Item1.add(Item1Sub1).addRight(Item1Sub2).addRight(Item1Sub3); //Motor 1 Settings
    Item1Sub1.add(Item1Sub1Sub1).addRight(Item1Sub1Sub2).addRight(Item1Sub1Sub3).addRight(Item1Sub1Sub4);
    Item1Sub2.add(Item1Sub2Sub1).addRight(Item1Sub2Sub2).addRight(Item1Sub2Sub3);
    Item1Sub3.add(Item1Sub3Sub1).addRight(Item1Sub3Sub2);


  Item2.add(Item2Sub1).addRight(Item2Sub2).addRight(Item2Sub3); //Motor 2 Settings
    Item2Sub1.add(Item2Sub1Sub1).addRight(Item2Sub1Sub2).addRight(Item2Sub1Sub3).addRight(Item2Sub1Sub4);
    Item2Sub2.add(Item2Sub2Sub1).addRight(Item2Sub2Sub2).addRight(Item2Sub2Sub3);
    Item2Sub3.add(Item2Sub3Sub1).addRight(Item2Sub3Sub2);

  Item3.add(Item3Sub1).addRight(Item3Sub2).addRight(Item3Sub3).addRight(Item3Sub4); //Backlight


  menu.toRoot();


}


void loop()
{
  readButtons();  //I splitted button reading and navigation in two procedures because
  navigateMenus();  //in some situations I want to use the button for other purpose (eg. to change some settings)
}




void menuChanged(MenuChangeEvent changed){


  MenuItem newMenuItem=changed.to; //get the destination menu


  if(newMenuItem.getName()==menu.getRoot()){
        lcd.setCursor(6,0);
        lcd.print("23:00:00");
        lcd.setCursor(5,1);
        lcd.print("2014/12/31");
  lcd.setCursor(0,2);
        lcd.print("                    ");
  lcd.setCursor(0,3);
        lcd.print("                    ");

  if (Motor1==1){
  lcd.setCursor(0,2); //set the start position for lcd printing to the second row
        lcd.print(TPD);
  lcd.setCursor(3,2);
  lcd.print("TPD");
  lcd.setCursor(0,3);
  lcd.print(CWCCW);
        } else if (Motor1==0){
          lcd.setCursor(0,2); //set the start position for lcd printing to the second row
          lcd.print("Not");
          lcd.setCursor(0,3);
   lcd.print("Active");
        }
       
  if (Motor2==1){
  lcd.setCursor(14,2); //set the start position for lcd printing to the second row
        lcd.print(TPD2);
  lcd.setCursor(17,2);
  lcd.print("TPD");
  lcd.setCursor(14,3);
  lcd.print(CWCCW2);
  } else if (Motor2==0){
          lcd.setCursor(17,2);
   lcd.print("Not");
   lcd.setCursor(14,3);
   lcd.print("Active");
   lcd.setCursor(0,2);
        lcd.print("test");
  
    }
  }
  else if(newMenuItem.getName()=="Item1"){ //Motor 1 Settings
  lcd.setCursor(0,3);
        lcd.print("                    ");
  lcd.setCursor(0,2);
        lcd.print("Motor 1 Settings    ");
  }
  else if(newMenuItem.getName()=="Item1Sub1"){ //M1 TPD
  lcd.setCursor(0,2);
        lcd.print("Motor 1 Settings    ");
  lcd.setCursor(0,3);
  lcd.print("TPD                 ");
  }
  else if(newMenuItem.getName()=="Item1Sub1Sub1"){ //650
  lcd.setCursor(0,2);
        lcd.print("Motor 1> TPD        ");
        if(TPD=="650"){
  lcd.setCursor(0,3);
  lcd.print("650 *               ");
  }else{
  lcd.setCursor(0,3);
  lcd.print("650                 ");
  }
  }
  else if(newMenuItem.getName()=="Item1Sub1Sub2"){ //750
  lcd.setCursor(0,2);
    lcd.print("Motor 1> TPD        ");
  if(TPD=="750"){
  lcd.setCursor(0,3);
  lcd.print("750 *               ");
  }else{
  lcd.setCursor(0,3);
  lcd.print("750                 ");
  }
  }
  else if(newMenuItem.getName()=="Item1Sub1Sub3"){ //850
  lcd.setCursor(0,2);
    lcd.print("Motor 1> TPD        ");
  if(TPD=="850"){
  lcd.setCursor(0,3);
  lcd.print("850 *               ");
  }else{
  lcd.setCursor(0,3);
  lcd.print("850                 ");
  }
  }
  else if(newMenuItem.getName()=="Item1Sub1Sub4"){ //950
  lcd.setCursor(0,2);
    lcd.print("Motor 1> TPD        ");
  if(TPD=="950"){
  lcd.setCursor(0,3);
  lcd.print("950 *               ");
  }else{
  lcd.setCursor(0,3);
  lcd.print("950                 ");
  }
  }
  else if(newMenuItem.getName()=="Item1Sub2"){ //M1 Driection
  lcd.setCursor(0,2);
    lcd.print("Motor 1 Settings    ");
  lcd.setCursor(0,3);
    lcd.print("Direction           ");
  }
  else if(newMenuItem.getName()=="Item1Sub2Sub1"){ //CW
  lcd.setCursor(0,2);
        lcd.print("Motor 1> Direrection");
  if(CWCCW=="CW"){
  lcd.setCursor(0,3);
  lcd.print("CW *                ");
  }else{
  lcd.setCursor(0,3);
  lcd.print("CW                  ");
  }
  }
  else if(newMenuItem.getName()=="Item1Sub2Sub2"){ //CCW
  lcd.setCursor(0,2);
        lcd.print("Motor 1> Direrection");
  if(CWCCW=="CCW"){
  lcd.setCursor(0,3);
  lcd.print("CCW *               ");
  }else{
  lcd.setCursor(0,3);
  lcd.print("CCW                 ");
  }
  }
  else if(newMenuItem.getName()=="Item1Sub2Sub3"){ //Both
  lcd.setCursor(0,2);
        lcd.print("Motor 1> Direrection");
  if(CWCCW=="CW/CCW"){
  lcd.setCursor(0,3);
  lcd.print("Both *              ");
  }else{
  lcd.setCursor(0,3);
  lcd.print("Both                ");
  }
  }
  else if(newMenuItem.getName()=="Item1Sub3"){ // M1 START/STOP
  lcd.setCursor(0,2);
        lcd.print("Motor 1 Settings    ");
  lcd.setCursor(0,3);
  lcd.print("STOP/START          ");
  }
  else if(newMenuItem.getName()=="Item1Sub3Sub1"){
  lcd.setCursor(0,2);
        lcd.print("Motor 1> STOP/START ");
  lcd.setCursor(0,3);
  lcd.print("START               ");
  }
  else if(newMenuItem.getName()=="Item1Sub3Sub2"){
  lcd.setCursor(0,2);
        lcd.print("Motor 1> STOP/START ");
  lcd.setCursor(0,3);
  lcd.print("STOP                ");
  }
  else if(newMenuItem.getName()=="Item2"){ //Motor2 settings
  lcd.setCursor(0,3);
        lcd.print("                    ");
  lcd.setCursor(0,2);
        lcd.print("Motor 2 Settings    ");
  }
  else if(newMenuItem.getName()=="Item2Sub1"){ //M2 TPD
  lcd.setCursor(0,2);
    lcd.print("Motor 2 Settings    ");
  lcd.setCursor(0,3);
  lcd.print("TPD                 ");
  }
  else if(newMenuItem.getName()=="Item2Sub1Sub1"){ //650
        lcd.setCursor(0,2);
        lcd.print("Motor 2> TPD        ");
  if(TPD2=="650"){
  lcd.setCursor(0,3);
  lcd.print("650 *           ");
  }else{
  lcd.setCursor(0,3);
  lcd.print("650             ");
  }
  }
  else if(newMenuItem.getName()=="Item2Sub1Sub2"){ //750
  lcd.setCursor(0,2);
    lcd.print("Motor 2> TPD        ");
  if(TPD2=="750"){
  lcd.setCursor(0,3);
  lcd.print("750 *               ");
  }else{
  lcd.setCursor(0,3);
  lcd.print("750                 ");
  }
  }
  else if(newMenuItem.getName()=="Item2Sub1Sub3"){ //850
  lcd.setCursor(0,2);
        lcd.print("Motor 2> TPD        ");
  if(TPD2=="850"){
  lcd.setCursor(0,3);
  lcd.print("850 *               ");
  }else{
  lcd.setCursor(0,3);
  lcd.print("850                 ");
  }
  }
  else if(newMenuItem.getName()=="Item2Sub1Sub4"){ //950
  lcd.setCursor(0,2);
        lcd.print("Motor 2> TPD        ");
  if(TPD=="950"){
  lcd.setCursor(0,3);
  lcd.print("950 *               ");
  }else{
  lcd.setCursor(0,3);
  lcd.print("950                 ");
  }
  }
  else if(newMenuItem.getName()=="Item2Sub2"){ //M2 Direction
  lcd.setCursor(0,2);
        lcd.print("Motor 2> Direction  ");
  lcd.setCursor(0,3);
        lcd.print("Direction           ");
  }
  else if(newMenuItem.getName()=="Item2Sub2Sub1"){ //CW
  lcd.setCursor(0,2);
        lcd.print("Motor 2> Direction  ");
  if(CWCCW2=="    CW"){
  lcd.setCursor(0,3);
  lcd.print("CW *                ");
  }else{
  lcd.setCursor(0,3);
  lcd.print("CW                  ");
  }
  }
  else if(newMenuItem.getName()=="Item2Sub2Sub2"){ //CCW
  lcd.setCursor(0,2);
    lcd.print("Motor 2> Direction  ");
  if(CWCCW2=="   CCW"){
  lcd.setCursor(0,3);
  lcd.print("CCW *               ");
  }else{
  lcd.setCursor(0,3);
  lcd.print("CCW                 ");
  }
  }
  else if(newMenuItem.getName()=="Item2Sub2Sub3"){ //Both
  lcd.setCursor(0,2);
        lcd.print("Motor 2> Direction  ");
  if(CWCCW2=="CW/CCW"){
  lcd.setCursor(0,3);
  lcd.print("Both *              ");
  }else{
  lcd.setCursor(0,3);
  lcd.print("Both                ");
  }
  }
  else if(newMenuItem.getName()=="Item2Sub3"){ // M2 START/STOP
  lcd.setCursor(0,2);
        lcd.print("Motor 2 Settings    ");
  lcd.setCursor(0,3);
  lcd.print("STOP/START          ");
  }
  else if(newMenuItem.getName()=="Item2Sub3Sub1"){
  lcd.setCursor(0,2);
        lcd.print("Motor 2> STOP/START ");
  lcd.setCursor(0,3);
  lcd.print("START               ");
  }
  else if(newMenuItem.getName()=="Item2Sub3Sub2"){
  lcd.setCursor(0,2);
        lcd.print("Motor 2> STOP/START ");
  lcd.setCursor(0,3);
  lcd.print("STOP                ");
  }
  else if(newMenuItem.getName()=="Item3"){ //backlight
  lcd.setCursor(0,3);
    lcd.print("                    ");
  lcd.setCursor(0,2);
    lcd.print("LCD Backlight       ");
  }
  else if(newMenuItem.getName()=="Item3Sub1"){ //25%
  lcd.setCursor(0,2);
    lcd.print("LCD Backlight       ");
  if(Backlight==64){
  lcd.setCursor(0,3);
  lcd.print("25% *               ");
  }else{
  lcd.setCursor(0,3);
  lcd.print("25%                 ");
  }
  }
  else if(newMenuItem.getName()=="Item3Sub2"){ //50%
  lcd.setCursor(0,2);
    lcd.print("LCD Backlight       ");
  if(Backlight==128){
  lcd.setCursor(0,3);
  lcd.print("50% *               ");
  }else{
  lcd.setCursor(0,3);
  lcd.print("50%                 ");
  }
  }
  else if(newMenuItem.getName()=="Item3Sub3"){ //75%
  lcd.setCursor(0,2);
    lcd.print("LCD Backlight       ");
  if(Backlight==192){
  lcd.setCursor(0,3);
  lcd.print("75% *               ");
  }else{
  lcd.setCursor(0,3);
  lcd.print("75%                 ");
  }
  }
  else if(newMenuItem.getName()=="Item3Sub4"){ //100%
  lcd.setCursor(0,2);
    lcd.print("LCD Backlight       ");
  if(Backlight==255){
  lcd.setCursor(0,3);
  lcd.print("100% *              ");
  }else{
  lcd.setCursor(0,3);
  lcd.print("100%                ");
  }
  }
   
}


// här sätter man vad som ska hända efter man har valt något


void menuUsed(MenuUseEvent used){
  if(used.item=="Item1Sub1Sub1"){ //sätter TPD 650
  lcd.clear();
  TPD = String("650");
  lcd.setCursor(0,1);
  lcd.print("M1 TPD set to 650");
  delay(3000);  //delay to allow message reading
  lcd.clear();
  menu.toRoot();  //back to Main
  }
  else if(used.item=="Item1Sub1Sub2"){ //sätter TPD 750
  lcd.clear();
  TPD = String("750");
  lcd.setCursor(0,1);
  lcd.print("M1 TPD set to 750");
  delay(3000);  //delay to allow message reading
  lcd.clear();
  menu.toRoot();  //back to Main
  }
  else if(used.item=="Item1Sub1Sub3"){ //sätter TPD 850
  lcd.clear();
  TPD = String("850");
  lcd.setCursor(0,1);
  lcd.print("M1 TPD set to 850");
  delay(3000);  //delay to allow message reading
  lcd.clear();
  menu.toRoot();  //back to Main
  }
        else if(used.item=="Item1Sub1Sub4"){ //sätter TPD 950
  lcd.clear();
  TPD = String("950");
  lcd.setCursor(0,1);
  lcd.print("M1 TPD set to 950");
  delay(3000);  //delay to allow message reading
  lcd.clear();
  menu.toRoot();  //back to Main
  }
  else if(used.item=="Item1Sub2Sub1"){ //sätter CW
  lcd.clear();
  CWCCW = String("CW");
  lcd.setCursor(0,1);
  lcd.print("M1 Direction = CW");
  delay(3000);  //delay to allow message reading
  lcd.clear();
  menu.toRoot();  //back to Main
  }
  else if(used.item=="Item1Sub2Sub2"){ //sätter CCW
  lcd.clear();
  CWCCW = String("CCW");
  lcd.setCursor(0,1);
  lcd.print("M1 Direction = CCW");
  delay(3000);  //delay to allow message reading
  lcd.clear();
  menu.toRoot();  //back to Main
  }
  else if(used.item=="Item1Sub2Sub3"){ //sätter CW&CCW
  lcd.clear();
  CWCCW = String("CW/CCW");
  //turnmode = 0;
  lcd.setCursor(0,1);
  lcd.print("M1 Direction = Both");
  delay(3000);  //delay to allow message reading
  lcd.clear();
  menu.toRoot();  //back to Main
  }
  else if(used.item=="Item1Sub3Sub1"){ //Startar M1
  lcd.clear();
  lcd.setCursor(0,1);
                Motor1=1;
  lcd.print("Start Motor 1   ");
  delay(3000);  //delay to allow message reading
  lcd.clear();
  menu.toRoot();  //back to Main
  }
  else if(used.item=="Item1Sub3Sub2"){ //Stoppar M1
  lcd.clear();
  lcd.setCursor(0,1);
                Motor1=0;
  lcd.print("Stop Motor 1    ");
  delay(3000);  //delay to allow message reading
  lcd.clear();
  menu.toRoot();  //back to Main
  }
  else if(used.item=="Item2Sub1Sub1"){ //sätter TPD 650
  lcd.clear();
  TPD2 = String("650");
  lcd.setCursor(0,1);
  lcd.print("M2 TPD set to 650");
  delay(3000);  //delay to allow message reading
  lcd.clear();
  menu.toRoot();  //back to Main
  }
  else if(used.item=="Item2Sub1Sub2"){ //sätter TPD 750
  lcd.clear();
  TPD2 = String("750");
  lcd.setCursor(0,1);
  lcd.print("M2 TPD set to 750");
  delay(3000);  //delay to allow message reading
  lcd.clear();
  menu.toRoot();  //back to Main
  }
  else if(used.item=="Item2Sub1Sub3"){ //sätter TPD 850
  lcd.clear();
  TPD2 = String("850");
  lcd.setCursor(0,1);
  lcd.print("M2 TPD set to 850");
  delay(3000);  //delay to allow message reading
  lcd.clear();
  menu.toRoot();  //back to Main
  }
    else if(used.item=="Item2Sub1Sub4"){ //sätter TPD 950
  lcd.clear();
  TPD2 = String("950");
  lcd.setCursor(0,1);
  lcd.print("M2 TPD set to 950");
  delay(3000);  //delay to allow message reading
  lcd.clear();
  menu.toRoot();  //back to Main
  }
  else if(used.item=="Item2Sub2Sub1"){ //sätter CW
  lcd.clear();
  CWCCW2 = String("    CW");
  lcd.setCursor(0,1);
  lcd.print("M2 Direction = CW");
  delay(3000);  //delay to allow message reading
  lcd.clear();
  menu.toRoot();  //back to Main
  }
  else if(used.item=="Item2Sub2Sub2"){ //sätter CCW
  lcd.clear();
  CWCCW2 = String("   CCW");
  lcd.setCursor(0,1);
  lcd.print("M2 Direction = CCW");
  delay(3000);  //delay to allow message reading
  lcd.clear();
  menu.toRoot();  //back to Main
  }
  else if(used.item=="Item2Sub2Sub3"){ //sätter CW&CCW
  lcd.clear();
  CWCCW2 = String("CW/CCW");
  lcd.setCursor(0,1);
  lcd.print("M2 Direction = Both");
  delay(3000);  //delay to allow message reading
  lcd.clear();
  menu.toRoot();  //back to Main
  }
  else if(used.item=="Item2Sub3Sub1"){ //Startar M1
  lcd.clear();
  lcd.setCursor(0,1);
                Motor2=1;
  lcd.print("Start Motor 2   ");
  delay(3000);  //delay to allow message reading
  lcd.clear();
  menu.toRoot();  //back to Main
  }
  else if(used.item=="Item2Sub3Sub2"){ //Stoppar M1
  lcd.clear();
  lcd.setCursor(0,1);
                Motor2=0;
  lcd.print("Stop Motor 2   ");
  delay(3000);  //delay to allow message reading
  lcd.clear();
  menu.toRoot();  //back to Main
  }
  else if(used.item=="Item3Sub1"){ //Blacklight 25%
  lcd.clear();
  Backlight=64;
  lcd.setBacklight(64);
  lcd.setCursor(0,1);
  lcd.print("Backlight = 25% ");
  delay(3000);  //delay to allow message reading
  lcd.clear();
  menu.toRoot();  //back to Main
  }
  else if(used.item=="Item3Sub2"){ //Blacklight 50%
  lcd.clear();
  Backlight=128;
  lcd.setBacklight(128);
  lcd.setCursor(0,1);
  lcd.print("Backlight = 50% ");
  delay(3000);  //delay to allow message reading
  lcd.clear();
  menu.toRoot();  //back to Main
  }
  else if(used.item=="Item3Sub3"){ //Blacklight 75%
  lcd.clear();
  Backlight=192;
  lcd.setBacklight(192);
  lcd.setCursor(0,1);
  lcd.print("Backlight = 75% ");
  delay(3000);  //delay to allow message reading
  lcd.clear();
  menu.toRoot();  //back to Main
  }
  else if(used.item=="Item3Sub4"){ //Blacklight 100%
  lcd.clear();
  Backlight=255;
  lcd.setBacklight(255);
  lcd.setCursor(0,1);
  lcd.print("Backlight = 100%");
  delay(3000);  //delay to allow message reading
  lcd.clear();
  menu.toRoot();  //back to Main
  }

}


void  readButtons(){  //read buttons status


  int reading;
  int buttonEnterState=LOW;             // the current reading from the Enter input pin
  int buttonEscState=LOW;             // the current reading from the input pin
  int buttonLeftState=LOW;             // the current reading from the input pin
  int buttonRightState=LOW;             // the current reading from the input pin


  //Enter button
                  // read the state of the switch into a local variable:
                  reading = digitalRead(buttonPinEnter);
                  // check to see if you just pressed the enter button
                  // (i.e. the input went from LOW to HIGH),  and you've waited
                  // long enough since the last press to ignore any noise:
                  // If the switch changed, due to noise or pressing:
                  if (reading != lastButtonEnterState) {
                    // reset the debouncing timer
                    lastEnterDebounceTime = millis();
                  }
                  if ((millis() - lastEnterDebounceTime) > debounceDelay) {
                    // whatever the reading is at, it's been there for longer
                    // than the debounce delay, so take it as the actual current state:
                    buttonEnterState=reading;
                    lastEnterDebounceTime=millis();
                  }
                  // save the reading.  Next time through the loop,
                  // it'll be the lastButtonState:
                  lastButtonEnterState = reading;


    //Esc button
                  // read the state of the switch into a local variable:
                  reading = digitalRead(buttonPinEsc);
                  // check to see if you just pressed the Down button
                  // (i.e. the input went from LOW to HIGH),  and you've waited
                  // long enough since the last press to ignore any noise:
                  // If the switch changed, due to noise or pressing:
                  if (reading != lastButtonEscState) {
                    // reset the debouncing timer
                    lastEscDebounceTime = millis();
                  }
                  if ((millis() - lastEscDebounceTime) > debounceDelay) {
                    // whatever the reading is at, it's been there for longer
                    // than the debounce delay, so take it as the actual current state:
                    buttonEscState = reading;
                    lastEscDebounceTime=millis();
                  }
                  // save the reading.  Next time through the loop,
                  // it'll be the lastButtonState:
                  lastButtonEscState = reading;
   //Down button
                  // read the state of the switch into a local variable:
                  reading = digitalRead(buttonPinRight);
                  // check to see if you just pressed the Down button
                  // (i.e. the input went from LOW to HIGH),  and you've waited
                  // long enough since the last press to ignore any noise:
                  // If the switch changed, due to noise or pressing:
                  if (reading != lastButtonRightState) {
                    // reset the debouncing timer
                    lastRightDebounceTime = millis();
                  }
                  if ((millis() - lastRightDebounceTime) > debounceDelay) {
                    // whatever the reading is at, it's been there for longer
                    // than the debounce delay, so take it as the actual current state:
                    buttonRightState = reading;
  lastRightDebounceTime =millis();
                  }
                  // save the reading.  Next time through the loop,
                  // it'll be the lastButtonState:
                  lastButtonRightState = reading;
    //Up button
                  // read the state of the switch into a local variable:
                  reading = digitalRead(buttonPinLeft);
                  // check to see if you just pressed the Down button
                  // (i.e. the input went from LOW to HIGH),  and you've waited
                  // long enough since the last press to ignore any noise:
                  // If the switch changed, due to noise or pressing:
                  if (reading != lastButtonLeftState) {
                    // reset the debouncing timer
                    lastLeftDebounceTime = millis();
                  }
                  if ((millis() - lastLeftDebounceTime) > debounceDelay) {
                    // whatever the reading is at, it's been there for longer
                    // than the debounce delay, so take it as the actual current state:
                    buttonLeftState = reading;
                    lastLeftDebounceTime=millis();;
                  }
                  // save the reading.  Next time through the loop,
                  // it'll be the lastButtonState:
                  lastButtonLeftState = reading;
                  //records which button has been pressed
                  if (buttonEnterState==HIGH){
                    lastButtonPushed=buttonPinEnter;
                  }else if(buttonEscState==HIGH){
                    lastButtonPushed=buttonPinEsc;
                  }else if(buttonRightState==HIGH){
                    lastButtonPushed=buttonPinRight;
                  }else if(buttonLeftState==HIGH){
                    lastButtonPushed=buttonPinLeft;
                  }else{
     lastButtonPushed=0;
                  }
}


void navigateMenus() {
  MenuItem currentMenu=menu.getCurrent();
  switch (lastButtonPushed){
  case buttonPinEnter:
  if(!(currentMenu.moveDown())){  //if the current menu has a child and has been pressed enter then menu navigate to item below
  menu.use();
  }else{  //otherwise, if menu has no child and has been pressed enter the current menu is used
  menu.moveDown();
  }
  break;
  case buttonPinEsc:
  menu.toRoot();  //back to main
  break;
  case buttonPinRight:
  menu.moveRight();
  break;
  case buttonPinLeft:
  menu.moveLeft();
  break;
  }
  lastButtonPushed=0; //reset the lastButtonPushed variable
}

Attachments:
image
  • Sign in to reply
  • Cancel
  • mcb1
    0 mcb1 over 11 years ago

    Filip

    Your LiquidCrystal_I2C libary should have provided you some examples.

    File Open Examples and find LiquidCrystal_I2C

    image

     

     

    You'll find that your line

    LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

    is incorrect.

     

    You need to change it to the I2C address (usually 0x27) which is hex 27, then the lcd specs

    I have changed it to 20x4 from the example.

     

      LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 20 chars and 4 line display

     

     

    Also if you choose the >> on the editor and syntax highlighting C++ you can enter the code in like this.

     

    #include <Wire.h>
    #include <LCD.h>
    #include <LiquidCrystal_I2C.h>
    #include <MenuBackend.h>
    
    
    #define I2C_ADDR    0x3F
    #define BACKLIGHT_PIN     3
    #define En_pin  2
    #define Rw_pin  1
    #define Rs_pin  0
    #define D4_pin  4
    #define D5_pin  5
    #define D6_pin  6
    #define D7_pin  7
    
    
    const int buttonPinLeft = 8;      // pin for the Up button
    const int buttonPinRight = 9;    // pin for the Down button
    const int buttonPinEsc = 10;     // pin for the Esc button
    const int buttonPinEnter = 11;   // pin for the Enter button
    
    
    int lastButtonPushed = 0;
    
    
    int lastButtonEnterState = LOW;   // the previous reading from the Enter input pin
    int lastButtonEscState = LOW;   // the previous reading from the Esc input pin
    int lastButtonLeftState = LOW;   // the previous reading from the Left input pin
    int lastButtonRightState = LOW;   // the previous reading from the Right input pin
    
    
    long lastEnterDebounceTime = 0;  // the last time the output pin was toggled
    long lastEscDebounceTime = 0;  // the last time the output pin was toggled
    long lastLeftDebounceTime = 0;  // the last time the output pin was toggled
    long lastRightDebounceTime = 0;  // the last time the output pin was toggled
    long debounceDelay = 500;    // the debounce time
    
    
    int Motor1=1;
    int Motor2=0;
    int Backlight=255;
    
    
    String CWCCW, CWCCW2, TPD, TPD2;
    
    
    
    LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
    
    
    
    
    //Menu variables
    
    
    MenuBackend menu = MenuBackend(menuUsed,menuChanged);
    
    
    //initialize menuitems
        MenuItem Item1 = MenuItem("Item1"); //Motor 1 Settings
    
    
      MenuItem Item1Sub1 = MenuItem("Item1Sub1"); //TPD
      MenuItem Item1Sub1Sub1 = MenuItem("Item1Sub1Sub1"); //650
      MenuItem Item1Sub1Sub2 = MenuItem("Item1Sub1Sub2"); //750
      MenuItem Item1Sub1Sub3 = MenuItem("Item1Sub1Sub3"); //850
      MenuItem Item1Sub1Sub4 = MenuItem("Item1Sub1Sub4"); //950
    
    
      MenuItem Item1Sub2 = MenuItem("Item1Sub2");
      MenuItem Item1Sub2Sub1 = MenuItem("Item1Sub2Sub1"); //CW
      MenuItem Item1Sub2Sub2 = MenuItem("Item1Sub2Sub2"); //CCW
      MenuItem Item1Sub2Sub3 = MenuItem("Item1Sub2Sub3"); //Both
        
      MenuItem Item1Sub3 = MenuItem("Item1Sub3");
      MenuItem Item1Sub3Sub1 = MenuItem("Item1Sub3Sub1"); //Stop
      MenuItem Item1Sub3Sub2 = MenuItem("Item1Sub3Sub2"); //Start
        
      MenuItem Item2 = MenuItem("Item2"); //Motor 2 Settings
    
      MenuItem Item2Sub1 = MenuItem("Item2Sub1"); //TPD
      MenuItem Item2Sub1Sub1 = MenuItem("Item2Sub1Sub1"); //650
      MenuItem Item2Sub1Sub2 = MenuItem("Item2Sub1Sub2"); //750
      MenuItem Item2Sub1Sub3 = MenuItem("Item2Sub1Sub3"); //850
      MenuItem Item2Sub1Sub4 = MenuItem("Item2Sub1Sub4"); //950
    
    
      MenuItem Item2Sub2 = MenuItem("Item2Sub2");
      MenuItem Item2Sub2Sub1 = MenuItem("Item2Sub2Sub1"); //CW
      MenuItem Item2Sub2Sub2 = MenuItem("Item2Sub2Sub2"); //CCW
      MenuItem Item2Sub2Sub3 = MenuItem("Item2Sub2Sub3"); //Both
        
      MenuItem Item2Sub3 = MenuItem("Item2Sub3");
      MenuItem Item2Sub3Sub1 = MenuItem("Item2Sub3Sub1"); //Stop
      MenuItem Item2Sub3Sub2 = MenuItem("Item2Sub3Sub2"); //Start
    
    
      MenuItem Item3 = MenuItem("Item3"); //Backlight
      MenuItem Item3Sub1 = MenuItem("Item3Sub1"); //25%
      MenuItem Item3Sub2 = MenuItem("Item3Sub2"); //50%
      MenuItem Item3Sub3 = MenuItem("Item3Sub3"); //75%
      MenuItem Item3Sub4 = MenuItem("Item3Sub4"); //100%
    
    
    
    void setup()
    {
      lcd.begin (20,4); //  <<----- My LCD was 16x2
      // Switch on the backlight
      lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
      lcd.setBacklight(HIGH);
      lcd.home (); // go home
      lcd.print("SainSmart");
    
    
      CWCCW = String("Both");
      CWCCW2 = String(" CCW");
        TPD = String("650");
      TPD2 = String("850");
    
      pinMode(buttonPinLeft, INPUT);
      pinMode(buttonPinRight, INPUT);
      pinMode(buttonPinEnter, INPUT);
      pinMode(buttonPinEsc, INPUT);
    
    
    
    
      //configure menu
      menu.getRoot().add(Item1);
    
      Item1.addRight(Item2).addRight(Item3);
    
      Item1.add(Item1Sub1).addRight(Item1Sub2).addRight(Item1Sub3); //Motor 1 Settings
        Item1Sub1.add(Item1Sub1Sub1).addRight(Item1Sub1Sub2).addRight(Item1Sub1Sub3).addRight(Item1Sub1Sub4);
        Item1Sub2.add(Item1Sub2Sub1).addRight(Item1Sub2Sub2).addRight(Item1Sub2Sub3);
        Item1Sub3.add(Item1Sub3Sub1).addRight(Item1Sub3Sub2);
    
    
      Item2.add(Item2Sub1).addRight(Item2Sub2).addRight(Item2Sub3); //Motor 2 Settings
        Item2Sub1.add(Item2Sub1Sub1).addRight(Item2Sub1Sub2).addRight(Item2Sub1Sub3).addRight(Item2Sub1Sub4);
        Item2Sub2.add(Item2Sub2Sub1).addRight(Item2Sub2Sub2).addRight(Item2Sub2Sub3);
        Item2Sub3.add(Item2Sub3Sub1).addRight(Item2Sub3Sub2);
    
      Item3.add(Item3Sub1).addRight(Item3Sub2).addRight(Item3Sub3).addRight(Item3Sub4); //Backlight
    
    
      menu.toRoot();
    
    
    }
    
    
    void loop()
    {
      readButtons();  //I splitted button reading and navigation in two procedures because
      navigateMenus();  //in some situations I want to use the button for other purpose (eg. to change some settings)
    }
    
    
    
    
    void menuChanged(MenuChangeEvent changed){
    
    
      MenuItem newMenuItem=changed.to; //get the destination menu
    
    
      if(newMenuItem.getName()==menu.getRoot()){
            lcd.setCursor(6,0);
            lcd.print("23:00:00");
            lcd.setCursor(5,1);
            lcd.print("2014/12/31");
      lcd.setCursor(0,2);
            lcd.print("                    ");
      lcd.setCursor(0,3);
            lcd.print("                    ");
    
      if (Motor1==1){
      lcd.setCursor(0,2); //set the start position for lcd printing to the second row
            lcd.print(TPD);
      lcd.setCursor(3,2);
      lcd.print("TPD");
      lcd.setCursor(0,3);
      lcd.print(CWCCW);
            } else if (Motor1==0){
              lcd.setCursor(0,2); //set the start position for lcd printing to the second row
              lcd.print("Not");
              lcd.setCursor(0,3);
       lcd.print("Active");
            }
          
      if (Motor2==1){
      lcd.setCursor(14,2); //set the start position for lcd printing to the second row
            lcd.print(TPD2);
      lcd.setCursor(17,2);
      lcd.print("TPD");
      lcd.setCursor(14,3);
      lcd.print(CWCCW2);
      } else if (Motor2==0){
              lcd.setCursor(17,2);
       lcd.print("Not");
       lcd.setCursor(14,3);
       lcd.print("Active");
       lcd.setCursor(0,2);
            lcd.print("test");
    
        }
      }
      else if(newMenuItem.getName()=="Item1"){ //Motor 1 Settings
      lcd.setCursor(0,3);
            lcd.print("                    ");
      lcd.setCursor(0,2);
            lcd.print("Motor 1 Settings    ");
      }
      else if(newMenuItem.getName()=="Item1Sub1"){ //M1 TPD
      lcd.setCursor(0,2);
            lcd.print("Motor 1 Settings    ");
      lcd.setCursor(0,3);
      lcd.print("TPD                 ");
      }
      else if(newMenuItem.getName()=="Item1Sub1Sub1"){ //650
      lcd.setCursor(0,2);
            lcd.print("Motor 1> TPD        ");
            if(TPD=="650"){
      lcd.setCursor(0,3);
      lcd.print("650 *               ");
      }else{
      lcd.setCursor(0,3);
      lcd.print("650                 ");
      }
      }
      else if(newMenuItem.getName()=="Item1Sub1Sub2"){ //750
      lcd.setCursor(0,2);
        lcd.print("Motor 1> TPD        ");
      if(TPD=="750"){
      lcd.setCursor(0,3);
      lcd.print("750 *               ");
      }else{
      lcd.setCursor(0,3);
      lcd.print("750                 ");
      }
      }
      else if(newMenuItem.getName()=="Item1Sub1Sub3"){ //850
      lcd.setCursor(0,2);
        lcd.print("Motor 1> TPD        ");
      if(TPD=="850"){
      lcd.setCursor(0,3);
      lcd.print("850 *               ");
      }else{
      lcd.setCursor(0,3);
      lcd.print("850                 ");
      }
      }
      else if(newMenuItem.getName()=="Item1Sub1Sub4"){ //950
      lcd.setCursor(0,2);
        lcd.print("Motor 1> TPD        ");
      if(TPD=="950"){
      lcd.setCursor(0,3);
      lcd.print("950 *               ");
      }else{
      lcd.setCursor(0,3);
      lcd.print("950                 ");
      }
      }
      else if(newMenuItem.getName()=="Item1Sub2"){ //M1 Driection
      lcd.setCursor(0,2);
        lcd.print("Motor 1 Settings    ");
      lcd.setCursor(0,3);
        lcd.print("Direction           ");
      }
      else if(newMenuItem.getName()=="Item1Sub2Sub1"){ //CW
      lcd.setCursor(0,2);
            lcd.print("Motor 1> Direrection");
      if(CWCCW=="CW"){
      lcd.setCursor(0,3);
      lcd.print("CW *                ");
      }else{
      lcd.setCursor(0,3);
      lcd.print("CW                  ");
      }
      }
      else if(newMenuItem.getName()=="Item1Sub2Sub2"){ //CCW
      lcd.setCursor(0,2);
            lcd.print("Motor 1> Direrection");
      if(CWCCW=="CCW"){
      lcd.setCursor(0,3);
      lcd.print("CCW *               ");
      }else{
      lcd.setCursor(0,3);
      lcd.print("CCW                 ");
      }
      }
      else if(newMenuItem.getName()=="Item1Sub2Sub3"){ //Both
      lcd.setCursor(0,2);
            lcd.print("Motor 1> Direrection");
      if(CWCCW=="CW/CCW"){
      lcd.setCursor(0,3);
      lcd.print("Both *              ");
      }else{
      lcd.setCursor(0,3);
      lcd.print("Both                ");
      }
      }
      else if(newMenuItem.getName()=="Item1Sub3"){ // M1 START/STOP
      lcd.setCursor(0,2);
            lcd.print("Motor 1 Settings    ");
      lcd.setCursor(0,3);
      lcd.print("STOP/START          ");
      }
      else if(newMenuItem.getName()=="Item1Sub3Sub1"){
      lcd.setCursor(0,2);
            lcd.print("Motor 1> STOP/START ");
      lcd.setCursor(0,3);
      lcd.print("START               ");
      }
      else if(newMenuItem.getName()=="Item1Sub3Sub2"){
      lcd.setCursor(0,2);
            lcd.print("Motor 1> STOP/START ");
      lcd.setCursor(0,3);
      lcd.print("STOP                ");
      }
      else if(newMenuItem.getName()=="Item2"){ //Motor2 settings
      lcd.setCursor(0,3);
            lcd.print("                    ");
      lcd.setCursor(0,2);
            lcd.print("Motor 2 Settings    ");
      }
      else if(newMenuItem.getName()=="Item2Sub1"){ //M2 TPD
      lcd.setCursor(0,2);
        lcd.print("Motor 2 Settings    ");
      lcd.setCursor(0,3);
      lcd.print("TPD                 ");
      }
      else if(newMenuItem.getName()=="Item2Sub1Sub1"){ //650
            lcd.setCursor(0,2);
            lcd.print("Motor 2> TPD        ");
      if(TPD2=="650"){
      lcd.setCursor(0,3);
      lcd.print("650 *           ");
      }else{
      lcd.setCursor(0,3);
      lcd.print("650             ");
      }
      }
      else if(newMenuItem.getName()=="Item2Sub1Sub2"){ //750
      lcd.setCursor(0,2);
        lcd.print("Motor 2> TPD        ");
      if(TPD2=="750"){
      lcd.setCursor(0,3);
      lcd.print("750 *               ");
      }else{
      lcd.setCursor(0,3);
      lcd.print("750                 ");
      }
      }
      else if(newMenuItem.getName()=="Item2Sub1Sub3"){ //850
      lcd.setCursor(0,2);
            lcd.print("Motor 2> TPD        ");
      if(TPD2=="850"){
      lcd.setCursor(0,3);
      lcd.print("850 *               ");
      }else{
      lcd.setCursor(0,3);
      lcd.print("850                 ");
      }
      }
      else if(newMenuItem.getName()=="Item2Sub1Sub4"){ //950
      lcd.setCursor(0,2);
            lcd.print("Motor 2> TPD        ");
      if(TPD=="950"){
      lcd.setCursor(0,3);
      lcd.print("950 *               ");
      }else{
      lcd.setCursor(0,3);
      lcd.print("950                 ");
      }
      }
      else if(newMenuItem.getName()=="Item2Sub2"){ //M2 Direction
      lcd.setCursor(0,2);
            lcd.print("Motor 2> Direction  ");
      lcd.setCursor(0,3);
            lcd.print("Direction           ");
      }
      else if(newMenuItem.getName()=="Item2Sub2Sub1"){ //CW
      lcd.setCursor(0,2);
            lcd.print("Motor 2> Direction  ");
      if(CWCCW2=="    CW"){
      lcd.setCursor(0,3);
      lcd.print("CW *                ");
      }else{
      lcd.setCursor(0,3);
      lcd.print("CW                  ");
      }
      }
      else if(newMenuItem.getName()=="Item2Sub2Sub2"){ //CCW
      lcd.setCursor(0,2);
        lcd.print("Motor 2> Direction  ");
      if(CWCCW2=="   CCW"){
      lcd.setCursor(0,3);
      lcd.print("CCW *               ");
      }else{
      lcd.setCursor(0,3);
      lcd.print("CCW                 ");
      }
      }
      else if(newMenuItem.getName()=="Item2Sub2Sub3"){ //Both
      lcd.setCursor(0,2);
            lcd.print("Motor 2> Direction  ");
      if(CWCCW2=="CW/CCW"){
      lcd.setCursor(0,3);
      lcd.print("Both *              ");
      }else{
      lcd.setCursor(0,3);
      lcd.print("Both                ");
      }
      }
      else if(newMenuItem.getName()=="Item2Sub3"){ // M2 START/STOP
      lcd.setCursor(0,2);
            lcd.print("Motor 2 Settings    ");
      lcd.setCursor(0,3);
      lcd.print("STOP/START          ");
      }
      else if(newMenuItem.getName()=="Item2Sub3Sub1"){
      lcd.setCursor(0,2);
            lcd.print("Motor 2> STOP/START ");
      lcd.setCursor(0,3);
      lcd.print("START               ");
      }
      else if(newMenuItem.getName()=="Item2Sub3Sub2"){
      lcd.setCursor(0,2);
            lcd.print("Motor 2> STOP/START ");
      lcd.setCursor(0,3);
      lcd.print("STOP                ");
      }
      else if(newMenuItem.getName()=="Item3"){ //backlight
      lcd.setCursor(0,3);
        lcd.print("                    ");
      lcd.setCursor(0,2);
        lcd.print("LCD Backlight       ");
      }
      else if(newMenuItem.getName()=="Item3Sub1"){ //25%
      lcd.setCursor(0,2);
        lcd.print("LCD Backlight       ");
      if(Backlight==64){
      lcd.setCursor(0,3);
      lcd.print("25% *               ");
      }else{
      lcd.setCursor(0,3);
      lcd.print("25%                 ");
      }
      }
      else if(newMenuItem.getName()=="Item3Sub2"){ //50%
      lcd.setCursor(0,2);
        lcd.print("LCD Backlight       ");
      if(Backlight==128){
      lcd.setCursor(0,3);
      lcd.print("50% *               ");
      }else{
      lcd.setCursor(0,3);
      lcd.print("50%                 ");
      }
      }
      else if(newMenuItem.getName()=="Item3Sub3"){ //75%
      lcd.setCursor(0,2);
        lcd.print("LCD Backlight       ");
      if(Backlight==192){
      lcd.setCursor(0,3);
      lcd.print("75% *               ");
      }else{
      lcd.setCursor(0,3);
      lcd.print("75%                 ");
      }
      }
      else if(newMenuItem.getName()=="Item3Sub4"){ //100%
      lcd.setCursor(0,2);
        lcd.print("LCD Backlight       ");
      if(Backlight==255){
      lcd.setCursor(0,3);
      lcd.print("100% *              ");
      }else{
      lcd.setCursor(0,3);
      lcd.print("100%                ");
      }
      }
      
    }
    
    
    // här sätter man vad som ska hända efter man har valt något
    
    
    void menuUsed(MenuUseEvent used){
      if(used.item=="Item1Sub1Sub1"){ //sätter TPD 650
      lcd.clear();
      TPD = String("650");
      lcd.setCursor(0,1);
      lcd.print("M1 TPD set to 650");
      delay(3000);  //delay to allow message reading
      lcd.clear();
      menu.toRoot();  //back to Main
      }
      else if(used.item=="Item1Sub1Sub2"){ //sätter TPD 750
      lcd.clear();
      TPD = String("750");
      lcd.setCursor(0,1);
      lcd.print("M1 TPD set to 750");
      delay(3000);  //delay to allow message reading
      lcd.clear();
      menu.toRoot();  //back to Main
      }
      else if(used.item=="Item1Sub1Sub3"){ //sätter TPD 850
      lcd.clear();
      TPD = String("850");
      lcd.setCursor(0,1);
      lcd.print("M1 TPD set to 850");
      delay(3000);  //delay to allow message reading
      lcd.clear();
      menu.toRoot();  //back to Main
      }
            else if(used.item=="Item1Sub1Sub4"){ //sätter TPD 950
      lcd.clear();
      TPD = String("950");
      lcd.setCursor(0,1);
      lcd.print("M1 TPD set to 950");
      delay(3000);  //delay to allow message reading
      lcd.clear();
      menu.toRoot();  //back to Main
      }
      else if(used.item=="Item1Sub2Sub1"){ //sätter CW
      lcd.clear();
      CWCCW = String("CW");
      lcd.setCursor(0,1);
      lcd.print("M1 Direction = CW");
      delay(3000);  //delay to allow message reading
      lcd.clear();
      menu.toRoot();  //back to Main
      }
      else if(used.item=="Item1Sub2Sub2"){ //sätter CCW
      lcd.clear();
      CWCCW = String("CCW");
      lcd.setCursor(0,1);
      lcd.print("M1 Direction = CCW");
      delay(3000);  //delay to allow message reading
      lcd.clear();
      menu.toRoot();  //back to Main
      }
      else if(used.item=="Item1Sub2Sub3"){ //sätter CW&CCW
      lcd.clear();
      CWCCW = String("CW/CCW");
      //turnmode = 0;
      lcd.setCursor(0,1);
      lcd.print("M1 Direction = Both");
      delay(3000);  //delay to allow message reading
      lcd.clear();
      menu.toRoot();  //back to Main
      }
      else if(used.item=="Item1Sub3Sub1"){ //Startar M1
      lcd.clear();
      lcd.setCursor(0,1);
                    Motor1=1;
      lcd.print("Start Motor 1   ");
      delay(3000);  //delay to allow message reading
      lcd.clear();
      menu.toRoot();  //back to Main
      }
      else if(used.item=="Item1Sub3Sub2"){ //Stoppar M1
      lcd.clear();
      lcd.setCursor(0,1);
                    Motor1=0;
      lcd.print("Stop Motor 1    ");
      delay(3000);  //delay to allow message reading
      lcd.clear();
      menu.toRoot();  //back to Main
      }
      else if(used.item=="Item2Sub1Sub1"){ //sätter TPD 650
      lcd.clear();
      TPD2 = String("650");
      lcd.setCursor(0,1);
      lcd.print("M2 TPD set to 650");
      delay(3000);  //delay to allow message reading
      lcd.clear();
      menu.toRoot();  //back to Main
      }
      else if(used.item=="Item2Sub1Sub2"){ //sätter TPD 750
      lcd.clear();
      TPD2 = String("750");
      lcd.setCursor(0,1);
      lcd.print("M2 TPD set to 750");
      delay(3000);  //delay to allow message reading
      lcd.clear();
      menu.toRoot();  //back to Main
      }
      else if(used.item=="Item2Sub1Sub3"){ //sätter TPD 850
      lcd.clear();
      TPD2 = String("850");
      lcd.setCursor(0,1);
      lcd.print("M2 TPD set to 850");
      delay(3000);  //delay to allow message reading
      lcd.clear();
      menu.toRoot();  //back to Main
      }
        else if(used.item=="Item2Sub1Sub4"){ //sätter TPD 950
      lcd.clear();
      TPD2 = String("950");
      lcd.setCursor(0,1);
      lcd.print("M2 TPD set to 950");
      delay(3000);  //delay to allow message reading
      lcd.clear();
      menu.toRoot();  //back to Main
      }
      else if(used.item=="Item2Sub2Sub1"){ //sätter CW
      lcd.clear();
      CWCCW2 = String("    CW");
      lcd.setCursor(0,1);
      lcd.print("M2 Direction = CW");
      delay(3000);  //delay to allow message reading
      lcd.clear();
      menu.toRoot();  //back to Main
      }
      else if(used.item=="Item2Sub2Sub2"){ //sätter CCW
      lcd.clear();
      CWCCW2 = String("   CCW");
      lcd.setCursor(0,1);
      lcd.print("M2 Direction = CCW");
      delay(3000);  //delay to allow message reading
      lcd.clear();
      menu.toRoot();  //back to Main
      }
      else if(used.item=="Item2Sub2Sub3"){ //sätter CW&CCW
      lcd.clear();
      CWCCW2 = String("CW/CCW");
      lcd.setCursor(0,1);
      lcd.print("M2 Direction = Both");
      delay(3000);  //delay to allow message reading
      lcd.clear();
      menu.toRoot();  //back to Main
      }
      else if(used.item=="Item2Sub3Sub1"){ //Startar M1
      lcd.clear();
      lcd.setCursor(0,1);
                    Motor2=1;
      lcd.print("Start Motor 2   ");
      delay(3000);  //delay to allow message reading
      lcd.clear();
      menu.toRoot();  //back to Main
      }
      else if(used.item=="Item2Sub3Sub2"){ //Stoppar M1
      lcd.clear();
      lcd.setCursor(0,1);
                    Motor2=0;
      lcd.print("Stop Motor 2   ");
      delay(3000);  //delay to allow message reading
      lcd.clear();
      menu.toRoot();  //back to Main
      }
      else if(used.item=="Item3Sub1"){ //Blacklight 25%
      lcd.clear();
      Backlight=64;
      lcd.setBacklight(64);
      lcd.setCursor(0,1);
      lcd.print("Backlight = 25% ");
      delay(3000);  //delay to allow message reading
      lcd.clear();
      menu.toRoot();  //back to Main
      }
      else if(used.item=="Item3Sub2"){ //Blacklight 50%
      lcd.clear();
      Backlight=128;
      lcd.setBacklight(128);
      lcd.setCursor(0,1);
      lcd.print("Backlight = 50% ");
      delay(3000);  //delay to allow message reading
      lcd.clear();
      menu.toRoot();  //back to Main
      }
      else if(used.item=="Item3Sub3"){ //Blacklight 75%
      lcd.clear();
      Backlight=192;
      lcd.setBacklight(192);
      lcd.setCursor(0,1);
      lcd.print("Backlight = 75% ");
      delay(3000);  //delay to allow message reading
      lcd.clear();
      menu.toRoot();  //back to Main
      }
      else if(used.item=="Item3Sub4"){ //Blacklight 100%
      lcd.clear();
      Backlight=255;
      lcd.setBacklight(255);
      lcd.setCursor(0,1);
      lcd.print("Backlight = 100%");
      delay(3000);  //delay to allow message reading
      lcd.clear();
      menu.toRoot();  //back to Main
      }
    
    }
    
    
    void  readButtons(){  //read buttons status
    
    
      int reading;
      int buttonEnterState=LOW;             // the current reading from the Enter input pin
      int buttonEscState=LOW;             // the current reading from the input pin
      int buttonLeftState=LOW;             // the current reading from the input pin
      int buttonRightState=LOW;             // the current reading from the input pin
    
    
      //Enter button
                      // read the state of the switch into a local variable:
                      reading = digitalRead(buttonPinEnter);
                      // check to see if you just pressed the enter button
                      // (i.e. the input went from LOW to HIGH),  and you've waited
                      // long enough since the last press to ignore any noise:
                      // If the switch changed, due to noise or pressing:
                      if (reading != lastButtonEnterState) {
                        // reset the debouncing timer
                        lastEnterDebounceTime = millis();
                      }
                      if ((millis() - lastEnterDebounceTime) > debounceDelay) {
                        // whatever the reading is at, it's been there for longer
                        // than the debounce delay, so take it as the actual current state:
                        buttonEnterState=reading;
                        lastEnterDebounceTime=millis();
                      }
                      // save the reading.  Next time through the loop,
                      // it'll be the lastButtonState:
                      lastButtonEnterState = reading;
    
    
        //Esc button
                      // read the state of the switch into a local variable:
                      reading = digitalRead(buttonPinEsc);
                      // check to see if you just pressed the Down button
                      // (i.e. the input went from LOW to HIGH),  and you've waited
                      // long enough since the last press to ignore any noise:
                      // If the switch changed, due to noise or pressing:
                      if (reading != lastButtonEscState) {
                        // reset the debouncing timer
                        lastEscDebounceTime = millis();
                      }
                      if ((millis() - lastEscDebounceTime) > debounceDelay) {
                        // whatever the reading is at, it's been there for longer
                        // than the debounce delay, so take it as the actual current state:
                        buttonEscState = reading;
                        lastEscDebounceTime=millis();
                      }
                      // save the reading.  Next time through the loop,
                      // it'll be the lastButtonState:
                      lastButtonEscState = reading;
       //Down button
                      // read the state of the switch into a local variable:
                      reading = digitalRead(buttonPinRight);
                      // check to see if you just pressed the Down button
                      // (i.e. the input went from LOW to HIGH),  and you've waited
                      // long enough since the last press to ignore any noise:
                      // If the switch changed, due to noise or pressing:
                      if (reading != lastButtonRightState) {
                        // reset the debouncing timer
                        lastRightDebounceTime = millis();
                      }
                      if ((millis() - lastRightDebounceTime) > debounceDelay) {
                        // whatever the reading is at, it's been there for longer
                        // than the debounce delay, so take it as the actual current state:
                        buttonRightState = reading;
      lastRightDebounceTime =millis();
                      }
                      // save the reading.  Next time through the loop,
                      // it'll be the lastButtonState:
                      lastButtonRightState = reading;
        //Up button
                      // read the state of the switch into a local variable:
                      reading = digitalRead(buttonPinLeft);
                      // check to see if you just pressed the Down button
                      // (i.e. the input went from LOW to HIGH),  and you've waited
                      // long enough since the last press to ignore any noise:
                      // If the switch changed, due to noise or pressing:
                      if (reading != lastButtonLeftState) {
                        // reset the debouncing timer
                        lastLeftDebounceTime = millis();
                      }
                      if ((millis() - lastLeftDebounceTime) > debounceDelay) {
                        // whatever the reading is at, it's been there for longer
                        // than the debounce delay, so take it as the actual current state:
                        buttonLeftState = reading;
                        lastLeftDebounceTime=millis();;
                      }
                      // save the reading.  Next time through the loop,
                      // it'll be the lastButtonState:
                      lastButtonLeftState = reading;
                      //records which button has been pressed
                      if (buttonEnterState==HIGH){
                        lastButtonPushed=buttonPinEnter;
                      }else if(buttonEscState==HIGH){
                        lastButtonPushed=buttonPinEsc;
                      }else if(buttonRightState==HIGH){
                        lastButtonPushed=buttonPinRight;
                      }else if(buttonLeftState==HIGH){
                        lastButtonPushed=buttonPinLeft;
                      }else{
         lastButtonPushed=0;
                      }
    }
    
    
    void navigateMenus() {
      MenuItem currentMenu=menu.getCurrent();
      switch (lastButtonPushed){
      case buttonPinEnter:
      if(!(currentMenu.moveDown())){  //if the current menu has a child and has been pressed enter then menu navigate to item below
      menu.use();
      }else{  //otherwise, if menu has no child and has been pressed enter the current menu is used
      menu.moveDown();
      }
      break;
      case buttonPinEsc:
      menu.toRoot();  //back to main
      break;
      case buttonPinRight:
      menu.moveRight();
      break;
      case buttonPinLeft:
      menu.moveLeft();
      break;
      }
      lastButtonPushed=0; //reset the lastButtonPushed variable
    }

     

     

    mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to mcb1

    Hi and thx for fast reply.

     

    I have try with 0x27 but it dosn't work.

    When I use a I2C scanner, I get the information that a device is connected to 0x3F

     

    And when I test the first code it works, so why dosn't it work with the second code?

     

    //Filip

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to mcb1

    I tested you example and it worked with the "hello, World" but I get the same problem with my own code after I change


    LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

     

    To

     

    LiquidCrystal_I2C lcd(0x3F,20,4)


    :/

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 11 years ago

    One of the things you also need to be careful of is running out of RAM, it will just starting behaving badly if you do run out or just go unresponsive .

     

    The reason I say this is because you have a huge volume of strings in your code and they will ALL be moved into the RAM space at run time

     

    Watch Tutorials 3 here for tips on how to correct this and optimize other areas in your code http://www.element14.com/community/groups/arduino/blog/2014/06/09/fast-track-to-arduino-programming

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 11 years ago in reply to Former Member

    Do you get a compile error or simple nothing on the display ?

     

    What is the constant I2C_ADDR set to, you should be using the same

    If an initialization of the LCD with the pins being specified works and your version does not, you should look elsewhere in the sample for something like this

     

    lcd.begin(16, 2);

     

    and change it to

     

    lcd.begin(20, 4);

     

    not all libraries are created equal unfortunately and so you have to account for the differences

     

    some libraries specefy the pins being used for the I2C to parallel chip to LCD, yours could be none standard ?

     

    post the part used for the adapter, a picture, etc so we can help more

     

    Thanks

     

    Peter

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to Robert Peter Oakes

    Okey, I think it is a RAM problem.

    Because when I add the code for "MenuBackend" the LCD stops to work.

     

    I will try it later today.

    And thanks for all help.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mcb1
    0 mcb1 over 11 years ago in reply to Former Member

    Filip

    Ram issues are a pain.

     

    You can save yourself some memory if you change the variables to something more suitable.

    image

    The first 4 are simply on or off so could be a boolean.

    The debounceDelay doesn't require to be a long.

     

    You seem to have a lot of text that could be saved into a single variable and then reused each time you want to display it

    image

    The "Item1Sub" is used extensively and only needs to be stored once.

    You could also make up the text with adding the numbers if you still need more room.

     

    Theses parts here look for a variable text then print the same text, when you could simply print the variable (you already checks is that)

    image

    Line 4 (0,3) of you display mostly seems to print 5 chars  ie CCW or CCW * (CCW space *)

    You don't need to print the whole line, and could also just print the * by using

    lcd.setCursor(4.3);

    lcd.print("*") ;

     

     

    So you may be able to tidy what you have and save enough memory to get it to work.

     

     

    Mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 11 years ago

    The most ram will be saved though if the strings are pushed into PROGMEM, other optimizations can also be gained in the if then else statements, converting into a "Switch Case" structure

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to Former Member

    After I did some PROGMEM fixes the LCD started to work agian. image

     

    thanks for all tips and tricks guys.

     

    Is there any recommended limit of free RAM?

    My code is is not finished yet so it will grow a bit more before I'm done.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 11 years ago

    Well you can convert some parts of your code to actual C rather than "Wiring", this will also massively shrink you code

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube