Replies: 1 comment 3 replies
-
@ezed413 Sorry, I have never used EasyButton but It's possible you haven't wired your buttons correctly. Check out this tutorial on buttons for the ESP32. I suggest you try and get your buttons working without GUIslice first than move on to trying EasyButton. Paul-- |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have three push buttons attached to GPIO 21, 22, and 23. I am using the Easybutton class but I can't get it to work. Can anyone tell me what I am doing wrong by looking at my code?
`#include <Arduino.h>
//<App !Start!>
// FILE: [Map_Btns.ino]
// Created by GUIslice Builder version: [0.17.0]
//
// GUIslice Builder Generated File
//
// For the latest guides, updates and support view:
// https://github.com/ImpulseAdventure/GUIslice
//
//<App !End!>
// ------------------------------------------------
// Headers to include
// ------------------------------------------------
#include "Map_Btns_GSLC.h"
#define GSLC_FEATURE_INPUT 1
// Button Control
// - Define the pin connections for the external buttons.
// - NOTE: The defaults provided assume an ATmega 2560, and will need to be
// changed according to your board / wiring.
// - NOTE: The EasyButton constructor defaults to pull-up enabled with inverted
// polarity, meaning that the switches will connect the pin to ground
// when closed, and pull-up (via ATmega internal pullup resistors) when
// open. If your device does not have internal pullup resistors, then
// you must provide them externally.
// EasyButton(uint8_t pin, uint32_t dbTime = 35, bool puEnable = true, bool invert = true);
#include "EasyButton.h"
#define PIN_PREV 21
#define PIN_SEL 22
#define PIN_NEXT 23
EasyButton btn_prev(PIN_PREV, 35, true, true);
EasyButton btn_sel(PIN_SEL, 35, true, true);
EasyButton btn_next(PIN_NEXT, 35, true, true);
#define GSLC_FEATURE_INPUT 1
// Ensure config settings are correct for the sketch
#if !(GSLC_FEATURE_INPUT)
#warning "This sketch requires config: #define GSLC_FEATURE_INPUT 1"
#endif
#define MAX_INPUT_MAP 3
gslc_tsInputMap m_asInputMap[MAX_INPUT_MAP];
#define PWM 4 // PWM pin for motor controller
// setting PWM properties
const int freq = 1000;
const int ledChannel = 0;
const int resolution = 8;
// ------------------------------------------------
// Program Globals
// ------------------------------------------------
int btn1State = 0;
int btn2State = 0;
int btn3State = 0;
// Save some element references for direct access
//<Save_References !Start!>
gslc_tsElemRef *m_pElemToggle1 = NULL;
//<Save_References !End!>
// Define debug message function
static int16_t DebugOut(char ch)
{
if (ch == (char)'\n')
Serial.println("");
else
Serial.write(ch);
return 0;
}
// ------------------------------------------------
// Callback Methods
// ------------------------------------------------
// Common Button callback
bool CbBtnCommon(void *pvGui, void *pvElemRef, gslc_teTouch eTouch, int16_t nX, int16_t nY)
{
// Typecast the parameters to match the GUI and element types
gslc_tsGui *pGui = (gslc_tsGui *)(pvGui);
gslc_tsElemRef *pElemRef = (gslc_tsElemRef *)(pvElemRef);
gslc_tsElem *pElem = gslc_GetElemFromRef(pGui, pElemRef);
if (eTouch == GSLC_TOUCH_UP_IN)
{
// From the element's ID we can determine which button was pressed.
switch (pElem->nId)
{
//<Button Enums !Start!>
case E_ELEM_BTN1:
break;
case E_ELEM_BTN2:
break;
case E_ELEM_BTN3:
break;
case E_ELEM_TOGGLE1:
// TODO Add code for Toggle button ON/OFF state
if (gslc_ElemXTogglebtnGetState(&m_gui, m_pElemToggle1))
{
;
}
break;
//<Button Enums !End!>
default:
break;
}
}
return true;
}
// Pin Input polling callback function
bool CbPinPoll(void *pvGui, int16_t *pnPinInd, int16_t *pnPinVal)
{
// Sample all pin inputs
btn_prev.read();
btn_sel.read();
btn_next.read();
// Determine if any pin edge events occur
// - If multiple pin events occur, they will be handled in consecutive CbPinPoll() calls
if (btn_prev.wasPressed())
{
*pnPinInd = PIN_PREV;
*pnPinVal = 1;
btn1State = 1;
}
else if (btn_sel.wasPressed())
{
*pnPinInd = PIN_SEL;
*pnPinVal = 1;
btn2State = 1;
}
else if (btn_next.wasPressed())
{
*pnPinInd = PIN_NEXT;
*pnPinVal = 1;
btn3State = 1;
}
else
return false; // No pin event detected
// If we reach here, then an pin event was detected
return true;
}
//<Checkbox Callback !Start!>
//<Checkbox Callback !End!>
//<Keypad Callback !Start!>
//<Keypad Callback !End!>
//<Spinner Callback !Start!>
//<Spinner Callback !End!>
//<Listbox Callback !Start!>
//<Listbox Callback !End!>
//<Draw Callback !Start!>
//<Draw Callback !End!>
//<Slider Callback !Start!>
//<Slider Callback !End!>
//<Tick Callback !Start!>
//<Tick Callback !End!>
void setup()
{
// ------------------------------------------------
// Initialize
// ------------------------------------------------
Serial.begin(9600);
// Wait for USB Serial
// delay(1000); // NOTE: Some devices require a delay after Serial.begin() before serial port can be used
// Initialize pins controls. Note that this calls pinMode()
btn_prev.begin();
btn_sel.begin();
btn_next.begin();
// Set the pin poll callback function
gslc_SetPinPollFunc(&m_gui, CbPinPoll);
// Create the GUI input mapping (pin event to GUI action)
gslc_InitInputMap(&m_gui, m_asInputMap, MAX_INPUT_MAP);
gslc_InputMapAdd(&m_gui, GSLC_INPUT_PIN_ASSERT, PIN_PREV, GSLC_ACTION_NONE, 0);
gslc_InputMapAdd(&m_gui, GSLC_INPUT_PIN_ASSERT, PIN_SEL, GSLC_ACTION_SELECT, 0);
gslc_InputMapAdd(&m_gui, GSLC_INPUT_PIN_ASSERT, PIN_NEXT, GSLC_ACTION_NONE, 0);
gslc_InitDebug(&DebugOut);
btn_sel.begin();
// attach the channel to the GPIO to be controlled
ledcAttachPin(PWM, ledChannel);
// configure PWM functionalitites
ledcSetup(ledChannel, freq, resolution);
// ------------------------------------------------
// Create graphic elements
// ------------------------------------------------
InitGUIslice_gen();
}
// -----------------------------------
// Main event loop
// -----------------------------------
void loop()
{
// ------------------------------------------------
// Update GUI Elements
// ------------------------------------------------
// TODO - Add update code for any text, gauges, or sliders
if (btn2State)
{
gslc_ElemXTogglebtnSetState(&m_gui, m_pElemToggle1, 1);
btn2State = 0;
Serial.print("Button 2 pressed");
}
else
gslc_ElemXTogglebtnSetState(&m_gui, m_pElemToggle1, 0);
// ------------------------------------------------
// Periodically call GUIslice update function
// ------------------------------------------------
gslc_Update(&m_gui);
}
`
Beta Was this translation helpful? Give feedback.
All reactions