Uno R3 pushbutton interrupt pulse only

OK, i got it working the way i want it, just 1 count up no matter if the switch (hall sensor) is pressed (activated) all the time, same for the down count :+1:

I indeed had to add these statements:

lastupbuttonState = upbuttonState;
lastdownbuttonState = downbuttonState;

Here the full code formatted and copied using the IDE:

#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) {
    if (upbuttonState == HIGH) {
      count++;
      lcd.clear();
    }
  } else if (downbuttonState != lastdownbuttonState && count >= 1) {
    if (downbuttonState == HIGH) {
      count--;
      lcd.clear();
    }
  }

  lastupbuttonState = upbuttonState;
  lastdownbuttonState = downbuttonState;

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

Next is to clean up some things like the

lcd.print("TECH SHUBHAM");

and what is more important to change the polarity of the edge detection, as the Hall sensor output is negative when active (when the magnet passes).

Presently i have a Hex inverter chip (CD40106B) changing this polarity, but its better to run it without.

A big thanks to all pointing me into the right direction, UKHeliBob, Delta_G, PaulRB and alto777 :+1: :+1:

Regards Itsu

@itsu104, nice.

This part, "it changed, but did it change to the state we want to act on"

is now in your code like

    if (upbuttonState == HIGH) {

so Imma leave it to your imagination how you might change the test of the current (latest read) state of the button so it would react instead in the other edge.

This is a bit different but I think does the same thing; I like to use simple logic and use less ink where possible:

  if (upbuttonState != lastupbuttonState) {
    if (upbuttonState == HIGH) {
      count++;
      lcd.clear();
    }
  }

  if (downbuttonState != lastdownbuttonState) {
    if (downbuttonState == HIGH) {
      if (count > 0) {
        count--;
        lcd.clear();
      }
    }
  }

which is just a personal preference. If only because each if condition is just one test. But like I say, it works, and if you happy I'm happy.

The Hall effect sensors may be what means you need no denouncing, but oddly in my tests neither did regular pushbuttons!

The culprit turned out to be an implicit delay in the code, caused be the surprisingly long time it takes to clear the LCD, around 3000 microseconds, long enough to effectively ignore the short bouncing period of a simulated switch.

If bouncing is ever a problem, a cheap trick of just putting

  delay(20);
}

as the last line of the loop function should work, the loop woukd still run very fast enough to keep up with the target counting rate of 4 per second.

a7

Hi a7,

thanks for the alternative for the "else" statement, i can try that.

I did modify the code to account for the negative going active state of the Hall sensor output, so the Hall sensor now directly feeds into the analog inputs A4 and A5 and does it without causing any bouncing so it looks like.

Itsu

OK, good.

That's fine, and this seems like a complete project ready to go,into a box, but one thing that happened to me when I was working with the code is that I used an I2C LCD device, which i've preferred for some time as it takes fewer wires, and uses fewer i/o pins.

And the pins it does use can be shared with other I2C devices.

But. I2C uses A4 and A5! It wa easy to change your code as it has the pins specified at the top. I just moved them off the I2C bus (A4, A5).

Another trick I used was to set the count to 9990

int count = 9990;

which makes testing the reset at 10000 steps a little more convenient. Obvsly I changed it back

int count;

and by habit left off the "= 0", saving ink and relying on the fact that global int variables are automatically initialised to 0.

Oh, what's it going to say instead of TECH SHUBHAM?

No matter where the code came from or who helped you with it, it is now yours! What you have is a big improvement. There's too much code floating around out there that sorta kinda works written hastily by ppl who sorta kinda should stick to things they are better at.

Happy winding.

a7

Hi a7,

yes, i can now start designing and 3D printing the control box which houses the arduino, the display, the reverse switch for the motor (and the switch for the down counter) and the reset pushbutton.

Here is an overview on where i am right now.
Besides the control box, i need the magnet wire spool holder and perhaps some electronics to guide / steer the magnet wire so i have a nice tight coil.

I tested the reset at 10000 count earlier by leaving the 5V on the "up" pin, and worked fine.

During startup, it now momentarily shows "TECH ITSU", i know, not so original, but hey It's only for me :grinning:

Regards Itsu