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.
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.
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.
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
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.
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.
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.
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