Encoder Library / Menu Advice

Status
Not open for further replies.

Dizzwold

New member
Hi,

I'm using an Arduino Mega and 20,4 Lcd, using the built-in Lcd Library and Paul's PJRC Encoder Library and would like to ask for some advice.

I'm a novice at C++.

I'm tring to use a Rotary Encoder to scroll through a menu. Although there are currently only 2 items in the menu, I may wish to increase this.

The idea is on start-up, the Lcd displays "Temperature and Humidity". With a click of the encoder switch, this then takes me to the menu, where I would like to have displayed a cursor ">" in column 0, and also displayed are the menu choices "Set Temp" & "Set Humid", where I can scroll the cursor and select one to then take me to other menu's.

As a novice, I think I've made a reasonable effort" although bias", but at the limit of my knowledge.

I'd be greatful for some advice.

Dizzwold.
Code:
/* Encoder Library - Basic Example
 * http://www.pjrc.com/teensy/td_libs_Encoder.html
 *
 * This example code is in the public domain.
 */

#include <LiquidCrystal.h>
#include <Encoder.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
Encoder myEnc(20, 21);
int button = 53;
int sw_val = 0;

char* menu[] =
{
  "Set Temp",
  "Set Humid",
};

const int numMenuItems = sizeof menu / sizeof *menu;
const int numLcdRows = 2;
int newPosition = 0;

byte menuCursor[8] = {
  B01000, //  *
  B00100, //   *
  B00010, //    *
  B00001, //     *
  B00010, //    *
  B00100, //   *
  B01000, //  *
  B00000  //
};

void setup()
{
  Serial.begin(9600);

  lcd.begin(20, 4);
  lcd.createChar(0, menuCursor);
  lcd.clear();
  pinMode(button, INPUT);
  digitalWrite(button, HIGH);
  lcd.setCursor(4, 0);
  lcd.print("Temperature");
  lcd.setCursor(8, 1);
  lcd.print("and");
  lcd.setCursor(6, 2);
  lcd.print("Humidity");
}

long oldPosition = -999;

void loop()
{
  sw_val = digitalRead(button);
  Serial.println(sw_val);
  if (sw_val == LOW)
  {
    mainMenu();
  } 
}

void mainMenu()
{
  long newPosition = myEnc.read();
  if (newPosition /4 != oldPosition)
  {
    oldPosition = newPosition;
  lcd.clear(); 
  lcd.setCursor(0, newPosition);
  lcd.write(byte(0));  
  lcd.setCursor(1, 0);
  lcd.print(menu[newPosition]);
  }
}
 
Status
Not open for further replies.
Back
Top