Continuing problem with OneButton.h, could really use some guidance


I have a piece of code running in my sketch that is not performing as it should be. It’s probably not a very efficient code either since I’m still learning.

The code works but there’s something going wrong with the timing. I have isolated the code into a new sketch as I though it might be something to do with the rest of the code. But the problem stays the same.

What it does is:

long hold to enter INIT mode [3]

→ enter initializing mode and move stepper1 to the far right until an endstop is found en set it to 0.
→ manually move stepper2 to a position and set # steps to 0.

double click to confirm

→ move stepper1 to the far left until an endstop is found and record that # of steps taken.
→ manually move stepper2 to a position and record that # of steps taken.

double click to confirm

→ confirmation message and set mode to idle

The problem I keep having and can’t get solved is that after entering the INT mode, the ‘double click’ is not tap-tap, but more of a tab-taaaaab. Sometimes it won’t respond right away and I have to so that 4 times… I have no delays that can interfere with this and maybe I should have done it in a another manner. I use ‘state machine’ code for another part in the sketch but I’m not sure I van use a ‘confirm’ button between the states…

I have stripped the code from the original sketch since it’s 5 tabs and a lot of comments by me, but completely the same otherwise. I checked this code of course and the problem is the same.

Code was too long to include, is an attachment now.


EDIT: Oh I forgot to mention that I also use long press and double-click in the same sketch for a rotary button and that works fine

test_sketch.ino (8.23 KB)

int ENDSTOP_LEFT = 0; // pin 4 state

So, why do you never actually read pin 4?

By convention, all capital letter names are reserved for constants. Constants have the const keyword in front of them.

How do you know that the double click function is not being called as soon as you double click the switch?

Hi Paul,

I have it pin4 set up as an endstop but not wired yet. You think that's causing the problem?

I didn't realize that there is a naming convention for caps and lowercase. Will change that too.

When I use the rotary button, the double-click is actually responding right away. With this button there's nothing happening or, at least, there's no real pattern of clicking other than tap-taaap to make it work. The rotary is tap-tap. It 'feels' like the code is busy with something else. I had a bunch of serial prints in there that I thought were causing harm but after removing them there's no change in behavior.

The debounce and click times can be set in the lib but I don't think I should be adjusting anything there as the rotary works fine.


  pinMode(4, INPUT); // ENDSTOP LEFT
  pinMode(5, INPUT); // ENDSTOP RIGHT
  pinMode(6, INPUT); // ROTATION LEFT
  pinMode(7, INPUT); // ROTATION RIGHT

  pinMode(11, OUTPUT); // CAM FOCUS
  pinMode(12, OUTPUT); // CAM SHUTTER

Problem solved.

Removed all functions and went with a state machine approach, works perfectly now.