Uno R3 pushbutton interrupt pulse only

Hello, i am following this project:
https://drive.google.com/drive/folders/1_hUStY0KOqpF1ZPhrsTKZY1Fm-ZIoOBg

It's a simple program to count pulses when pushing some buttons.

But when the button is being pushed on all the time, the counts go up also.

I need to have only one count added (or subtracted) even when continue to push the button.
And after releasing the button it should be ready for the next push / one count, etc.

Can this simply be done by some extra code? And if so, how?

I presently have omitted this "problem" in an electronic way, but prefer to have it "fixed" in the code.

Thanks, Itsu

Welcome to the forum

You need to detect when the button becomes pressed rather than when it is pressed
See the StateChangeDetection example in the IDE

Hi Bob, thanks for the reply.

I get what you are saying, so there is some sort of detection possible, i will take a look into that.

Regards Itsu

Hi Delta,

you are right, silly of me, nowadays, links are dangerous.

I will attach the .ino file if i can find the way to to this.

Regards Itsu

Please don't do that either. Many forum members read the forum on tablets and phones and cannot open a .ino attachment.

Please read the forum guide in the sticky post at the top of most forum sections. That will tell you how to post code on the forum.

Thanks Paul,

i will look into that, but the FAQ already mentions: "Post Only Your Own Stuff", so as it is not my code i will not do so.

Thanks for now, Itsu

Where did you get the link to the pdf, code and hex file? If it wa something like an instructable or havackaday, you cou,d post the link to the website from which you are working.

On the other hand, code like this is trivial and I would not expect trouble if you posted it with attribution.

a7

Hi Alto,

i was / am working from a youtube video.

If you search for "Arduino Counter For Winding Machine (0-9999) ||Proteus 8.10" in youtube, you will find the video and in the comments there is the link to the PDF and code.

I am building such a coil winding machine and was looking for an up/down counter to count the number of turns on my coil using a magnet and a Hall sensor to track.

But if the magnet for some reason stops in front of the hall during winding, then it keeps on counting, like when you keep on pressing the button.

Itsu

Have you looked at the StateChangeDetection example as previously suggested ?

Hi Bob,

yes i did:

https://docs.arduino.cc/built-in-examples/digital/StateChangeDetection/

i guess this part of the code example (loop) is key:

// compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter

I will take a shot at it later tonight, thanks,

Itsu

There is a bit more to it because not only must you detect that the state of the input has changed, but also its current state

Note too that de-bouncing the button inputs is also necessary otherwise the bounces can be counted as multiple inputs

Thanks Bob,

not sure if de-bouncing is needed as i will use a Hall sensor as a switch instead of the buttons.
The Hall seems to switch cleanly, but i will see.

Itsu

Hi Delta,

i saw that in the FAQ section, see top of this page.

Anyway, as this code it pretty trivial as mentioned earlier, here it is:

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int up = A5;
int down = A4;
int reset = A3;
int count;
void setup() {
lcd.begin(16, 2);
pinMode(up, INPUT);
pinMode(down, INPUT);
pinMode(reset, INPUT);
lcd.setCursor(1, 0);
lcd.print("TECH SHUBHAM");
delay(2000);
lcd.clear();

}

void loop() {
if (digitalRead(up) == LOW || digitalRead(down) == LOW){
lcd.setCursor(2, 0);
lcd.print("Nbr of turns:");
lcd.setCursor(6, 1);
lcd.print(count);}
  
if (digitalRead(up) == HIGH){
count++;
delay(250);
lcd.clear();}
else if (digitalRead(down) == HIGH && count >= 1) {
count--;
delay(250);
lcd.clear();}

if (digitalRead(reset) == HIGH){
count = 0;
delay(250);
lcd.clear();}
else if (count == 10000){
delay(1000);
count = 0;
lcd.clear ();}


}

So the problem i have with this code is that when keeping on pushing the button, (or in my case keep on activating a Hall sensor) the count keeps counting up (or down) while it should act on a single impulse only.

I understand it could be fixed by inserting "edge detection" or "State Change Detection" code

Regards Itsu

Correctly, I believe. It's not clear you are on your way with that, see this post and maybe the thread it finds itself on:

HTH

a7

Thanks A7,

nice simulation in that thread :+1:

I will see what i can do with all this information, i need to handle 2 interrupts (A4 (Down) and A5 (Up)) with this edge detection (leave the A3 (reset) button as it is) and update the Up and Down counters.

Itsu

You've used the word interrupt in the title, and only now speak of "hand [ing]" interrupts.

The code you have provided does not use interrupts.

If you know nothing about interrupts, or are using the word casually, forget about interrupts.

If you know all about interrupts, you would probably not consider using them for this.

One question may let us definitively rule out the need for interrupts, and that is at what maximum rate do you expect to press the button, or whatever the equivalent is to the pulses up and down?

At this time, with the delay() you are using in your attempt to solve the issue you have observed, the maximum speed of counting is 4 steps per second.

At that rate, interrupts are not appropriate.

So, how fast do you need to count?

Is the code in #15 your code or the code from the video? Is the code in the video meant to be a coil winder counter sketch? It is limited to 4 turns per second, it seems, is that slow for coil winding? I know nothing about coil winding, so.

a7

A7,

yes, you might be right about me using the word interrupts casually.

The rate of pressing the button would indeed probably around 1 to 4 times a second, which is OK for winding a coil.

Yes, the code in #15 is from the video (i slightly modified the LCD screen text).

Here i again modified the sketch with the edge detection code (upbuttonState, lastupbuttonState, downbuttonState and lastdownbuttonState), but it still continues to count up when activating the up button.

Could it be the fact that the edge detection / StateChangeDetection does not work on analog inputs (A4 and A5)?

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int up = A5;
int down = A4;
int upbuttonState = 0;
int lastupbuttonState = 0;
int downbuttonState = 0;
int lastdownbuttonState = 0;
int reset = A3;
int count;
void setup() {
lcd.begin(16, 2);
pinMode(up, INPUT);
pinMode(down, INPUT);
pinMode(reset, INPUT);
lcd.setCursor(1, 0);
lcd.print("TECH SHUBHAM");
delay(2000);
lcd.clear();

}

void loop() {
upbuttonState = digitalRead(up);
downbuttonState = digitalRead(down);  
if (digitalRead(up) == LOW || digitalRead(down) == LOW){
lcd.setCursor(2, 0);
lcd.print("Nbr of turns:");
lcd.setCursor(6, 1);
lcd.print(count);}

if (upbuttonState != lastupbuttonState) {  
count++;
delay(250);
lcd.clear();}
else if (downbuttonState != lastdownbuttonState && count >= 1) {
count--;
delay(250);
lcd.clear();}

if (digitalRead(reset) == HIGH){
count = 0;
delay(250);
lcd.clear();}
else if (count == 10000){
delay(1000);
count = 0;
lcd.clear ();}


}

I'm in transit, and not headed to the lab, so I will be of little help.

But.

Please format your code using the IDE Auto Dormat tool. It is hard to see the { structure } otherwise.

Next, please read the code in the simulation, it has few steps but anll are necessary, you have failed to implement the pattern correctly.

As something that jumps off the page even through the tiniest of winows, you have

int lastupbuttonState = 0;

int lastdownbuttonState = 0;

but nowhere do you save the current button states so that the next loop can compare them to the newly read button states.

Also, once there is edge detection, there will be no need for the delay! Since the code will react only to the detected edge, it can examine the inputs 1000s of times a second. Only when a change since the previous examination is observed ("detected") will a count up or down be taken.

Without the delay, this code could count at very higher rates.

I suggest again that you study the simulation - put your finger in the code and "execute" it step by step to see the dance which is reading, comparing to the previous reading, reacting to any difference and finally… preparing for the next loop.

Post the code you have, or if you make progress and are still unhappy the new code, formatted. I think it is close, but as I say, it is hard to read.

a7

Hi A7,

no hurries, i will take a look at the simulation again, although it's slightly different from the code i use.

Please format your code using the IDE Auto Dormat tool. It is hard to see the { structure } otherwise.

I did format the code using the IDE (edit - copy for forum (markdown) which put it to the clipboard).

but nowhere do you save the current button states so that the next loop can compare them to the newly read button states.

I thought i did that by these statements under loop:

upbuttonState = digitalRead(up);
downbuttonState = digitalRead(down); 

Thanks for now, Itsu

Yeah no. Those read the current state of the buttons. Somewhere you have to save what was read the previous time.

Referring to the code in the simulation, these are the steps:

read the button   line #28

see if it is different to last time   line #33

  it is different, is it now pressed?   line #35

    act on the press   lines #37 to #39

save this button reading for next time   line #47

Lines #41 to #44 are when the switch is released. You have nothing that needs be done for that edge.

Srsly, read the code and watch the printout.

Where it prints "switch is pressed." would be the place to increment the counter. Increment the counter when the up button gets pressed. Becomes pressed.

In the similar code for the down button, do everything the same way, but do the action only if the count is not zero already

      if (count > 0) {
        count--;
        lcd.clear();
      }

a7