Register / Login  |  Desktop view  |  Jump to bottom of page

tcMenu Arduinio library » Navigation with 5 buttons to 1 analog pin

Author: kitsen13
29/07/2021 07:05:41
Hello,

I ´ m searching a way to configure a keypad k845037.
It’s a shield device with 5 digitals buttons to 1 analog output.
(wired to an esp32)

Please take a look at the wiring below.

Thanks in advance for your help.

[Thumb - 75FD1AFE-42D2-4C5E-81DF-F6D7CCB7FB8E.png]
Filename 75FD1AFE-42D2-4C5E-81DF-F6D7CCB7FB8E.png
Description wiring
Filesize 15 Kbytes
Downloaded 45178 time(s)
[Disk] Download

[Thumb - 3D135D18-4C84-45FF-836C-29AD62B8C690.jpeg]
Filename 3D135D18-4C84-45FF-836C-29AD62B8C690.jpeg
Description photo
Filesize 187 Kbytes
Downloaded 45480 time(s)
[Disk] Download


Author: davetcc
29/07/2021 16:33:33
Take a look at the documentation around the DfRobotInputAbstraction. It is configurable for 5 different levels in case your case does not use the same resistors as the DfRobot shield.

In the simplest case that you've used the same resistor values it's as easy as:

adding the include:

#include <DfRobotInputAbstraction.h>


then creating the abstraction:

IoAbstractionRef dfRobotKeys = inputFromDfRobotShield();


and telling switches to use that instead of Arduino pins:

switches.initialise(dfRobotKeys, false); // df robot is always false for 2nd parameter.


However, if you are using different resistor values, you would need to create the IoAbstractionRef as follows:

const PROGMEM DfRobotAnalogRanges myRanges { 0.0488F, 0.2441F, 0.4394F, 0.6347F, 0.8300F}; // right, up, down, left, select
DfRobotInputAbstraction dfRobotIo = DfRobotInputAbstraction(&myRanges, pin, device);
IoAbstractionRef dfRobotKeys = &dfRobotIo;


See the IoAbstraction example: dfRobotAnalogInSwitches


Author: davetcc
01/08/2021 09:53:51
Forgot to mention that if it's DfRobot compatible, there's a tcMenu example for that, the Uno example - analogDfRobot, but the plugin used should work equally well on ESP.

Author: kitsen13
01/08/2021 10:18:29
Hello,
Thanks for these informations, I will try to integrate and I will make you a feedback.

Author: kitsen13
06/08/2021 09:18:35
Hello,

I've a problem , when compiling the output feedback is :

Arduino : 1.8.13 (Windows 10), Carte : "WiFi Kit 32, Disabled, 240MHz (WiFi/BT), 921600, None"

exit status 1

'device' was not declared in this scope


When i replace "device" by "0" , esp32 reboot endless.

my code :

#include "tcmenu_menu.h"

// Debut AMA button
          #include <DfRobotInputAbstraction.h>
     //     IoAbstractionRef dfRobotKeys = inputFromDfRobotShield();
          
          const PROGMEM DfRobotAnalogRanges myRanges { 0.0488F, 0.2441F, 0.4394F, 0.6347F, 0.8300F}; // right, up, down, left, select
          DfRobotInputAbstraction dfRobotIo = DfRobotInputAbstraction(&myRanges, 35, device);
          IoAbstractionRef dfRobotKeys = &dfRobotIo;
          
          void logKeyPressed(const char* whichKey, bool heldDown) {
              Serial.print("Key ");
              Serial.print(whichKey);
              Serial.println(heldDown ? " Held" : " Pressed");
          }
          
          /**
           * Along with using functions to receive callbacks when a button is pressed, we can
           * also use a class that implements the SwitchListener interface. Here is an example
           * of implementing that interface. You have both choices, function callback or
           * interface implementation.
           */
          class MyKeyListener : public SwitchListener {
          private:
              const char* whatKey;
          public:
              // This is the constructor where we configure our instance
              MyKeyListener(const char* what) {
                  whatKey = what;
              }
          
              // when a key is pressed, this is called
              void onPressed(pinid_t /*pin*/, bool held) override {
                  logKeyPressed(whatKey, held);
              }
          
              // when a key is released this is called.
              void onReleased(pinid_t /*pin*/, bool held) override {
                  Serial.print("Release ");
                  logKeyPressed(whatKey, held);
              }
          };
          
          MyKeyListener selectKeyListener("SELECT");
// Fin AMA button


void setup() {
    setupMenu();

// Début AMA cycle
// taskManager.scheduleFixedRate(1000, toggle);
// Fin AMA

// Début AMA button

    // start up the serial port in a way compatible with 32 bit boards.
    while(!Serial);
    Serial.begin(115200);

    // initialise the switches component with the DfRobot shield as the input method.
    switches.initialise(dfRobotKeys, false); // df robot is always false for 2nd parameter.

    // now we add the switches, each one just logs the key press, the last parameter to addSwitch
    // is the repeat frequency is optional, when not set it implies not repeating.
    switches.addSwitch(DF_KEY_DOWN, [](pinid_t /*pin*/, bool held) { logKeyPressed("DOWN", held);}, 20);
    switches.addSwitch(DF_KEY_UP, [](pinid_t /*pin*/, bool held) { logKeyPressed("UP", held);}, 20);
    switches.addSwitch(DF_KEY_LEFT, [](pinid_t /*pin*/, bool held) { logKeyPressed("LEFT", held);}, 20);
    switches.addSwitch(DF_KEY_RIGHT, [](pinid_t /*pin*/, bool held) { logKeyPressed("RIGHT", held);}, 20);
    switches.onRelease(DF_KEY_RIGHT, [](pinid_t /*pin*/, bool) { Serial.println("RIGHT has been released");});
    
    switches.addSwitchListener(DF_KEY_SELECT, &selectKeyListener);
// Fin AMA

}

void loop() {
    taskManager.runLoop();

}


void CALLBACK_FUNCTION onSaveSettings(int id) {
    // TODO - your menu change code
}



void CALLBACK_FUNCTION onStartToasting(int id) {
    // TODO - your menu change code
}


void CALLBACK_FUNCTION onNameChanged(int id) {
    // TODO - your menu change code
}

// Début AMA cycle
void toggle() {
       
  if (menuToasterPower.getCurrentValue() == 9)
      {
      menuToasterPower.setCurrentValue(3);
      }
      else
      {
      menuToasterPower.setCurrentValue(9);
      }
}
// Fin AMA


Thanks for your help,

Regards,

[Thumb - Capture.PNG]
Filename Capture.PNG
Description Analog pin wired on pin 35
Filesize 536 Kbytes
Downloaded 45046 time(s)
[Disk] Download


Author: davetcc
08/08/2021 08:49:40
Ah yes, my apologies I missed out the analog device initialistation. Can you replace 'device' with 'internalAnalogIo()' and make sure that you include the following:

#include <AnalogDeviceAbstraction.h>


--

Also as an aside, you can just declare the IO abstraction for the touch buttons in your sketch file, and reference that in tcMenu Designer if you wish. You'd just select to use up/down buttons for input. Then in the IoExpander Reference field in the code generator just provide the variable name for the expander. EG in your case it would be dfRobotKeys.

The keys are mapped as follows:

* * pin0 = right (DF_KEY_RIGHT)
* * pin1 = up (DF_KEY_UP)
* * pin2 = down (DF_KEY_DOWN)
* * pin3 = left (DF_KEY_LEFT)
* * pin4 = select (DF_KEY_SELECT)

Hope that helps.

Author: kitsen13
08/08/2021 10:47:32
Hello,

Thanks, that helps me. No reboot and the Analog Input seems be read.

But...I have tried to customise my voltage below without success

const PROGMEM DfRobotAnalogRanges myRanges { 0.0488F, 0.2441F, 0.4394F, 0.6347F, 0.8300F}; // right, up, down, left, select


My measure is :

Right button : 2.529 v
Up button : 0.724 v
Down button : 1.645 v
Left button : 0.000 v
Select button : 3.636 v

I have based my customs values on the fact than the max ADC is 3,3v. Based on this , Right button at 2.529 v is 76.75 % of the max value which give 0.7675F. I'm wrong?

The results are :

const PROGMEM DfRobotAnalogRanges myRanges { 0.7675F, 0.2200F, 0.4993F, 0.0001F, 0.9999F}; // right, up, down, left, select


On the serial listener , when i press :

Right button : -> Key RIGHT Pressed
Up button : -> Key RIGHT Pressed
Down button : -> Key RIGHT Pressed
Left button : -> Key RIGHT Pressed
Select button : No output...

That is wrong with my custom buttons?

Regards,





Author: davetcc
08/08/2021 12:42:15
0.9999F seems a bit high, and the ADC on the ESP32 is not entirely linear IIRC.

It may be worth actually using some code that just prints the values from the ADC read to get the actual values, and then base your values on that.

Author: kitsen13
08/08/2021 14:21:21
0 buttons pressed :

16:03:17.774 -> Voltage = 3.30volts ADC VALUE = 4095

Right buuton :
16:04:11.779 -> Voltage = 2.35volts ADC VALUE = 2931
16:04:13.778 -> Voltage = 2.36volts ADC VALUE = 2847
16:04:15.779 -> Voltage = 2.29volts ADC VALUE = 2863
16:04:17.775 -> Voltage = 2.31volts ADC VALUE = 2869
16:04:19.770 -> Voltage = 2.31volts ADC VALUE = 2815
16:04:21.775 -> Voltage = 2.27volts ADC VALUE = 2887

Up button :

16:05:11.786 -> Voltage = 0.50volts ADC VALUE = 701
16:05:13.784 -> Voltage = 0.56volts ADC VALUE = 675
16:05:15.784 -> Voltage = 0.54volts ADC VALUE = 680
16:05:17.794 -> Voltage = 0.55volts ADC VALUE = 714
16:05:19.769 -> Voltage = 0.58volts ADC VALUE = 744
16:05:21.778 -> Voltage = 0.60volts ADC VALUE = 656

Down button :

16:06:01.793 -> Voltage = 1.41volts ADC VALUE = 1840
16:06:03.761 -> Voltage = 1.48volts ADC VALUE = 1776
16:06:05.766 -> Voltage = 1.43volts ADC VALUE = 1840
16:06:07.768 -> Voltage = 1.48volts ADC VALUE = 1776
16:06:09.766 -> Voltage = 1.43volts ADC VALUE = 1778

Left button :


16:06:53.765 -> Voltage = 0.00volts ADC VALUE = 0
16:06:55.770 -> Voltage = 0.00volts ADC VALUE = 0
16:06:57.764 -> Voltage = 0.00volts ADC VALUE = 0
16:06:59.768 -> Voltage = 0.00volts ADC VALUE = 0

Select button :

16:09:23.792 -> Voltage = 3.30volts ADC VALUE = 4095
16:09:25.790 -> Voltage = 3.30volts ADC VALUE = 4095
16:09:27.792 -> Voltage = 3.30volts ADC VALUE = 4095
16:09:29.764 -> Voltage = 3.30volts ADC VALUE = 4095

Select button seems no effect on voltage .... seems a device fauft. Ok i will try with 4 buttons only.
Adjusting with 3.3v -> 100% or 1.0000F, line below become :

const PROGMEM DfRobotAnalogRanges myRanges { 0.7030F, 0.1666F, 0.4363F, 0.0001F}; // right, up, down, left, select


(I have deleted the 5th term as the "select button" doesn't change voltage measured.)

But the same problem : only the right button in the log when I try right, up, down, left button.

I don't know why?


16:18:16.123 -> Key RIGHT Pressed
16:18:16.227 -> RIGHT has been released
16:18:16.528 -> Key RIGHT Pressed
16:18:16.695 -> RIGHT has been released
16:18:17.002 -> Key RIGHT Pressed
16:18:17.173 -> RIGHT has been released
16:18:17.341 -> Key RIGHT Pressed
16:18:17.509 -> RIGHT has been released
16:18:17.743 -> Key RIGHT Pressed
16:18:17.914 -> RIGHT has been released
16:18:18.088 -> Key RIGHT Pressed
16:18:18.223 -> RIGHT has been released
16:18:18.533 -> Key RIGHT Pressed
16:18:18.533 -> RIGHT has been released
16:18:18.801 -> Key RIGHT Pressed
16:18:18.903 -> RIGHT has been released
16:18:19.208 -> Key RIGHT Pressed
16:18:19.309 -> RIGHT has been released

Thanks in advance for the help...


Author: davetcc
08/08/2021 19:45:16
 
Select button seems no effect on voltage .... seems a device fauft. Ok i will try with 4 buttons only.


You probably don't have a large enough voltage difference between rail & the switch pressed, there is a limit near the top of the ESP analog input range IIRC.

(I have deleted the 5th term as the "select button" doesn't change voltage measured.)


Leave it in and set it to .9999, otherwise the value may be somewhat undefined and cause problems.

Got it now, looking at the code, you need to define them in order of voltage, because it works like

button 0 > 0 and < button0 threshold
button 1 > button0 threshold and < button1 threshold

So you can treat the buttons as 0, 1, 2, 3, 4 without considering if they are left right etc. In your case button 0 will be the one with the lowest value etc.

Hope that makes sense.

EDIT: take a look at https://github.com/davetcc/IoAbstraction/blob/c153a32350454b32c0e78d6cfd21f052ee6f4818/src/DfRobotInputAbstraction.h#L90

Author: kitsen13
09/08/2021 11:44:21
 
davetcc wrote:
Leave it in and set it to .9999, otherwise the value may be somewhat undefined and cause problems.


Hello,
Yes, to make sure "select" is disabled , I have to put " -2.9000F" because .9999 refer to 3.3v and I have this voltage when no butttons pressed.
It works.


davetcc wrote:
Got it now, looking at the code, you need to define them in order of voltage, because it works like

button 0 > 0 and < button0 threshold
button 1 > button0 threshold and < button1 threshold

So you can treat the buttons as 0, 1, 2, 3, 4 without considering if they are left right etc. In your case button 0 will be the one with the lowest value etc.

Hope that makes sense.

It works better. Now I have 4 distinct détections. (I have to modify https://github.com/davetcc/IoAbstraction/blob/c153.../DfRobotInputAbstraction.h#L90 to re-assign buttons)

Now I'm not sure which choice I have to configure in the designer.
Which control I have to choose? Control menu with rotary encoder? Up / down buttons? Analog joystick?

If I choose "Up / down button" how to set :
IoAbstractionRef for switches
Up button Pin
Down button Pin
etc...?

Thanks, in advance,
Regards


[Thumb - Capture2.PNG]
Filename Capture2.PNG
Description No description given
Filesize 10 Kbytes
Downloaded 44719 time(s)
[Disk] Download

[Thumb - Capture.PNG]
Filename Capture.PNG
Description No description given
Filesize 136 Kbytes
Downloaded 43909 time(s)
[Disk] Download


Author: davetcc
10/08/2021 09:29:26
In the code generator, you would set IoExpander ref to your instance of IoExpanderRef.

And then each of the buttons would be configured as 0..3. Where the pin with the lowest analog value would be 0 and so on.

Author: kitsen13
10/08/2021 16:39:00
 
davetcc wrote:In the code generator, you would set IoExpander ref to your instance of IoExpanderRef.

And then each of the buttons would be configured as 0..3. Where the pin with the lowest analog value would be 0 and so on.


Hello,

I don't see any "IoExpander ref" in the code, no instance configured. No "IoExpander ref" in the designer.


But, i see an "IoAbstractionRef for Switches" in the designer. Is it that you means?

If I select "Control menu with Up / down buttons" and I set "IoAbstractionRef for Switches" to "dfRobotKeys" with and each of the buttons configured as 0..3. -> don't work

If I set "IoAbstractionRef for Switches" to "dfRobotIo" -> don't work too.


Author: davetcc
11/08/2021 05:45:24
For the first part you were right on the reference you should use. Apologies for the typo.

Second part. You don’t provide the percentages, you provide the index of the button. The first being 0 and so on.

If it still does not work please zip up a minimal example project here and I’ll load it onto a board.

Author: kitsen13
11/08/2021 18:24:27
Hello,

I have retry to set "IoAbstractionRef for Switches" to "dfRobotKeys" with and each of the buttons configured as 0..3.
It works now!

Thank you for the help.




Register / Login  |  Desktop view  |  Jump to top of page