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 ![]()
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
![]()
Regards Itsu
