Hi!
I have an UNO with a LCD display and rotary encoder with a push button and a push button to pin 2.
I playing with this example "Code used on demo project shown at (http://www.youtube.com/watch?v=a1M5kirA2_8) and (http://www.youtube.com/watch?v=epJ3aSzrsXQ) Includes climate sensing, logging to SD card, HTTP server with JSON output, LCD display with settings menus. · GitHub" after taxing what is not needed.
I have problem with push button in displayMenu as it get there but return back displayData nearly immediately.
Also the push button of the encoder need to leave push for a bit in order to work (a quick push does not return a selection)
here is the code, Thanks:
// Include Standard Library
#include <stdlib.h>
// Include LCD Library Code
#include <LiquidCrystal.h>
// Include Rotary Encoder Library
#include <QuadEncoder.h>
//#include <Button.h>
#include <Tap.h>
// input
#define TEMP 27
#define SND1 28
#define PIR1 29
#define RHT 30
#define PIR2 31
#define LIGHT 15 // A15
// control
#define ROTENCA 9 //43
#define ROTENCB 3 //45
#define BUTTONR 7
#define BUTTON 2
// LCD
#define LCD_RS 12
#define LCD_EN 11
#define LCD_D1 5
#define LCD_D2 4
#define LCD_D3 6
#define LCD_D4 10
#define LCD_BLR 5
#define LCD_BLG 6
#define LCD_BLB 7
// initialize the LCD with the numbers of the interface pins
LiquidCrystal lcd(LCD_RS, LCD_EN, LCD_D1, LCD_D2, LCD_D3, LCD_D4);
// initialize the encoder
QuadEncoder encoder(ROTENCA,ROTENCB); // initialize the encoder
//Button buttonR(BUTTONR);
Tap buttonR(BUTTONR);
// has the encoder moved on this loop?
boolean moved = false;
// modes
#define MODE_DISPLAY 0
#define MODE_COUNT 1
#define LAST_MODE 1
unsigned long timeSinceLastDisplay = 0;
// Custom character for a degree sign
byte degree[8] = {
B01100,
B10010,
B10010,
B01100,
B00000,
B00000,
B00000
};
void setup() {
// setup interrupts
attachInterrupt(0, displayMenu, RISING);
// set up the LCD's number of columns and rows:
lcd.begin(16, 4);
lcd.createChar(0, degree);
// print a welcome message
lcd.clear();
lcd.print("*** WELCOME! ***");
lcd.setCursor(0,1);
lcd.print("Initializing");
}
void loop() {
displayData();
}
// Displays the current values on the LCD
void displayData() {
if (displayOk()) {
lcd.clear();
lcd.print("CLIMATE L");
}
}
void displayMenu() {
int selected = 3;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Turn the knob to");
lcd.setCursor(0,1);
lcd.print("select an option");
// display options
while (!buttonR.isHit()) {
selected += readEncoder();
if (selected<0) selected=3;
if (selected>3) selected=0;
if (moved) {
lcd.clear();
lcd.setCursor(0,0);
switch(selected) {
case 0:
lcd.print("Change Units ");
break;
case 1:
lcd.print("Backlight On/Off");
break;
case 2:
lcd.print("Logging On/Off ");
break;
case 3:
lcd.print("Exit Settings ");
break;
}
}
}
// when button is pressed...
lcd.clear();
switch(selected) {
case 0:
settingsChangeUnits();
break;
case 1:
settingsChangeBacklight();
break;
case 2:
settingsChangeLogging();
}
}
boolean displayOk() {
unsigned long now = millis();
if (now - timeSinceLastDisplay >= 500) {
timeSinceLastDisplay = now;
return true;
} else return false;
}
// returns 1 for right, -1 for left, or 0 for no movement
int readEncoder() {
moved = true;
switch(encoder.hb()) {
case '>': return 1;
case '<': return -1;
}
moved = false;
return 0;
}
void settingsChangeUnits() {
int selected = 2;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Select Unit ");
lcd.setCursor(0,1);
lcd.print("Currently it's ");
lcd.write("temp");
while (!buttonR.isHit()) {
selected += readEncoder();
if (selected<0) selected=2;
if (selected>2) selected=0;
if (moved) {
lcd.setCursor(0,1);
switch(selected) {
case 0:
lcd.print("Celcius ");
break;
case 1:
lcd.print("Fahrenheit ");
break;
case 2:
lcd.print("Cancel ");
break;
}
}
}
// when button is pressed...
switch(selected) {
case 0:
//temperatureUnit = 'C';
break;
case 1:
//temperatureUnit = 'F';
break;
}
}
void settingsChangeBacklight() {
//boolean previousSetting = ledIsOn;
int selected = 2;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Backlight On/Off");
while (!buttonR.isHit()) {
selected += readEncoder();
if (selected<0) selected=2;
if (selected>2) selected=0;
if (moved) {
lcd.setCursor(0,1);
lcd.print(" On Off Cancel");
switch(selected) {
case 0:
lcd.setCursor(0,1);
//ledOn();
break;
case 1:
lcd.setCursor(4,1);
//ledOff();
break;
case 2:
lcd.setCursor(9,1);
//previousSetting ? ledOn() : ledOff();
break;
}
lcd.write((uint8_t)126); // right arrow
}
}
}
void settingsChangeLogging() {
int selected = 2;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Logging to SD ");
lcd.setCursor(0,1);
//if (sdcard) lcd.print("card is: On ");
//else lcd.print("card is: Off ");
while (!buttonR.isHit()) {
selected += readEncoder();
if (selected<0) selected=2;
if (selected>2) selected=0;
if (moved) {
lcd.setCursor(0,1);
switch(selected) {
case 0:
//if (sdcard) lcd.print("Keep Logging On ");
//else lcd.print("Turn Logging On ");
break;
case 1:
//if (sdcard) lcd.print("Turn Logging Off");
//else lcd.print("Keep Logging Off");
break;
case 2:
lcd.print("Cancel ");
break;
}
}
}
// when button is pressed...
switch(selected) {
case 0:
//sdcard = true;
break;
case 1:
//sdcard = false;
break;
}
}