Has anyone used the cubecell board plus display to make a user button selectable menu? I have tried this example but it doesn’t work.
Thank you
-
Example of a one button menu for Arduino by Thierry Guennou. https://pandauino.com July 2017.
-
This code uses these libraries
-
mathertel.de is OneButton Library http://www.mathertel.de/Arduino/OneButtonLibrary.aspx
-
orange-cat is ManuBackend library https://github.com/Orange-Cat/MenuBackend
-
It is an example, limited to the setting of these two parameters, using a menu diplayed on LCD screen:
-
Frequency : 1, 2 ,10
-
Intensity: low, middle, high
-
The menu is entered by a long press on a button
-
A single click moves to the next value
-
A double click selects the value
-
A long press selects the value and exits the menu
-
When out of the menu mode the selected parameters are shown on the LCD screen. A real application could be a “blink+” program with selectable frequency and Intensity.
-
The physical implementation is shown at https://www.tinkercad.com/things/1QASM4dFosj-one-button-menu-arduino-circuit
-
Licence CC BY NC SA
*/
#include <OneButton.h>
#include <MenuBackend.h>
//#include <LiquidCrystal.h>
#include <LoRaWan_APP_tera.h>
#include <Wire.h>
#include “HT_SH1107Wire.h”
void printToLCD(String message, byte row);
const char PUSHBUTTON = USER_KEY; // PD2/INT0 menu pushbutton pin
// instantiation of a OneButton object identified as “button”
OneButton button(PUSHBUTTON, true);
bool inMenu = false;
// our 2 parameters to set using the menu
int Frequency = 1;
String Intensity = “Low”;
String msg;
/* ************************************************************************************************************************************
MENU
**************************************************************************************************************************************/
/*
This is the structure of the menu
Frequency
1
2
10
Intensity
Low
Middle
High
*/
// These handlers are not used in our code but needed to construct the menu object
void menuUseEvent(MenuUseEvent used)
{
// nothing here, events will be handled by oneButton event handlers
}
void menuChangeEvent(MenuChangeEvent changed)
{
// nothing here, events will be handled by oneButton event handlers
}
// Menu items instantiation
MenuBackend menu = MenuBackend(menuUseEvent,menuChangeEvent);
//beneath is list of menu items needed to build the menu
MenuItem MFrequency = MenuItem("Frequency > ");
MenuItem MFreq1 = MenuItem("1 ");
MenuItem MFreq2 = MenuItem("2 ");
MenuItem MFreq10 = MenuItem("10 ");
MenuItem MIntensity = MenuItem("Intensity > ");
MenuItem MIntensityLow = MenuItem("Low ");
MenuItem MIntensityMiddle = MenuItem("Middle ");
MenuItem MIntensityHigh = MenuItem("High ");
void menuSetup()
{
menu.getRoot().add(MFrequency);
MFrequency.addRight(MFreq1);
MFreq1.addAfter(MFreq2);
MFreq1.addLeft(MFrequency);
MFreq2.addAfter(MFreq10);
MFreq2.addLeft(MFrequency);
MFreq10.addAfter(MFreq1);
MFreq10.addLeft(MFrequency);
MFrequency.addAfter(MIntensity);
MIntensity.addRight(MIntensityLow);
MIntensityLow.addAfter(MIntensityMiddle);
MIntensityLow.addLeft(MIntensity);
MIntensityMiddle.addAfter(MIntensityHigh);
MIntensityMiddle.addLeft(MIntensity);
MIntensityHigh.addAfter(MIntensityLow);
MIntensityHigh.addLeft(MIntensity);
MIntensity.addAfter(MFrequency);
}
/* ************************************************************************************************************************************
BUTTON EVENT HANDLERS
**************************************************************************************************************************************/
void buttonClick() {
if (inMenu) {
menu.moveDown();
printToLCD(menu.getCurrent().getName(),0);
}
}
void buttonDoubleclick() {
if (inMenu) {
MenuItem currentMenu = menu.getCurrent();
if (currentMenu == MFrequency) {
menu.moveRight();
}
if (currentMenu == MFreq1) {
Frequency = 1;
menu.moveLeft();
}
if (currentMenu == MFreq2) {
Frequency = 2;
menu.moveLeft();
}
if (currentMenu == MFreq10) {
Frequency = 10;
menu.moveLeft();
}
if (currentMenu == MIntensity) {
menu.moveRight();
}
if (currentMenu == MIntensityLow) {
Intensity = "Low";
menu.moveLeft();
}
if (currentMenu == MIntensityMiddle) {
Intensity = "Mid";
menu.moveLeft();
}
if (currentMenu == MIntensityHigh) {
Intensity = "High";
menu.moveLeft();
}
printToLCD(menu.getCurrent().getName(),0);
}
}
void buttonPress() {
// buttonPress enters or exists the menu, hence the boolean inMenu value changes
inMenu = !inMenu;
if (inMenu) {
LoRaWAN.displayClear();
// if we just entered the menu, we have to move to the first menu entry
if (menu.getCurrent().getName() == "MenuRoot") {
menu.moveDown();
}
printToLCD(menu.getCurrent().getName(),0);
}
else {
MenuItem currentMenu = menu.getCurrent();
if (currentMenu == MFreq1) {
Frequency = 1;
}
if (currentMenu == MFreq2) {
Frequency = 2;
}
if (currentMenu == MFreq10) {
Frequency = 10;
}
if (currentMenu == MIntensityLow) {
Intensity = "Low";
}
if (currentMenu == MIntensityMiddle) {
Intensity = "Mid";
}
if (currentMenu == MIntensityHigh) {
Intensity = "High";
}
menu.toRoot();
LoRaWAN.displayClear();
}
}
void buttonSetup() {
button.attachClick(buttonClick);
button.attachDoubleClick(buttonDoubleclick);
button.attachLongPressStart(buttonPress);
// these are optional parameters to fine-tune the behavior. Values are in ms. Given values here are default values.
button.setClickTicks(60);
button.setPressTicks(100);
}
/* ************************************************************************************************************************************
MISCELLANOUS FUNCTIONS
**************************************************************************************************************************************/
void printToLCD(String message, byte row){
// the code here may depend on the LCD model
// here for a 16*2 LCD that I have
LoRaWAN.displayString(message,row);
//lcd.print(message);
}
/* ************************************************************************************************************************************
PROGRAM
**************************************************************************************************************************************/
void setup() {
Serial.begin(115200);
pinMode(PUSHBUTTON,INPUT);
//lcd.begin(16, 2);
menuSetup();
buttonSetup();
}
void loop() {
button.tick(); // checks regularly if the button is pressed or not and raises events according to the finite state
if (!inMenu) {
// under normal operation put your code using Frequency and Intensity parameters here
// in this example we simply display them on the LCD screen
msg = "Frequency: " + String(Frequency);
printToLCD(msg, 0);
msg = "Intensity: " + Intensity;
printToLCD(msg, 1);
}
}