Hey community, I am making some code for a small project. Basically each time a button is pressed, a counter counts down until it reaches zero, I am unsure however, what to do when I hit zero, I want tostop it from going to -1 etc. Can someone please help me on this. i am very new to arduino, come from a plc background. Ta. Trevor.
How to handle the issue depends on what else you are trying to do. If you are going to reset your count when you get to zero, then there is no reason to disable the button. You can disable the button, and the program be “done” when you hit zero. You can program the same button to reset to 10 when you reach zero.
One thing you almost certainly do NOT want to do is write to the pin that your button is connected to. If a button is configured for the pin, you want to read the status of the button. Writing to the pin is not going to be useful.
If you just want to disable the button, a gate node can be used to block the signal from the button. In your example, the gate would go between button and count. You would want to enable the gate when equal 0 is NOT true, so the output of the equal node could feed to “not”, then to “defer” (because we are going to loop back in the program), then to the gate-EN pin.
If you want to reset your count when you get to zero, there are multiple ways to do that. You can send a pulse to the count-RST pin when you get to zero, or on the next button press when you go below zero. An alternative is to replace the gate with a branch and have the button reset count when you get to zero.
There are probably multiple variations of each of these solutions, as well as other solutions. The “best” solution will depend on what else you are doing. If other required code is already providing half of one of these solutions, that is probably the solution you should use. What you want to happen on the edge cases might also determine which solution is “best”. If your count CANNOT go below zero, having the system reset when it goes below zero is not an option, even if it is only below zero for a split second.
Thankyou for the great resonse. The first example is very close to what I am trying to achieve. But I need to be able to reset the count with a second button. I tried sending a pulse to the count reset button (through a tweak), but it remains on zero count still.
I am trying to achieve a Counter displayed on a LCD that counts down from 50 each time a button is pressed. The reset will actually end up coming from RFID, which ill program in later, and once I hit zero I also want that pulse to stop another function which will happen on a seperate micro controller.
Thanks again, Trevor.
Never mind, I have sorted it now. Thankyou.
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.