I regularly use your forum just about anytime I do anything with Arduino and usually can find enough info for my project by simply searching the forums. This time however I can't seem to find anyone with my particular dilemma and decided it was finally time to formally join the forum and ask for help.
I am attempting to build an arcade cabinet that uses roms that are not arcade games (no credit aspect) but still wanted it to be coin operated. Ultimately I chose to use an Arduino Uno to run a timer that activated a USB port (thereby enabling the controller) when it receives input from a coin op mechanism.
I've been able to write the code to do everything I need except I have a while loop running while the "timer" is active and I attempted to add an if statement to check if additional coins are added to add more time, the problem is it isn't working. The timer initiates when the first coin is added but adding more coins doesn't do anything as if the "if" statement wasn't there.
I assume it is a simple syntax mistake as I am not a very proficient programmer, but I've tried everything I could think of and couldn't figure it out.
Thanks
My code:
#include <TM1637Display.h>
const int CLK = 9; //Set the CLK pin connection to the display
const int DIO = 8; //Set the DIO pin connection to the display
int NumStep = 0; //Variable to interate
int buttonPin;
TM1637Display display(CLK, DIO); //set up the 4-Digit Display.
void setup()
{
display.setBrightness(0x0a); //set the diplay to maximum brightness
buttonPin = 7;
pinMode(buttonPin, INPUT_PULLUP);
}
void loop()
{
if(digitalRead(buttonPin) == LOW) //functions based off of button pulling input pin LOW
{ NumStep = 300; //Add 300 s when coin is added
}
while (NumStep > 0){ //Code to run while timer is operating
display.showNumberDec(NumStep); //Display the Variable value;
delay(1000); //A second delay between steps.
if(digitalRead(buttonPin) == LOW) //Check if additional coins are added
{NumStep = NumStep + 300; //If coin is added, add 300s
}
The IDE can format the code using the CTRL T keys. This leads to a more readable code:
#include <TM1637Display.h>
const int CLK = 9; //Set the CLK pin connection to the display
const int DIO = 8; //Set the DIO pin connection to the display
int NumStep = 0; //Variable to interate
int buttonPin;
TM1637Display display(CLK, DIO); //set up the 4-Digit Display.
void setup()
{
display.setBrightness(0x0a); //set the diplay to maximum brightness
buttonPin = 7;
pinMode(buttonPin, INPUT_PULLUP);
}
void loop()
{
if (digitalRead(buttonPin) == LOW) NumStep = 300; //Add 300 s when coin is added
while (NumStep > 0) { //Code to run while timer is operating
display.showNumberDec(NumStep); //Display the Variable value;
delay(1000); //A second delay between steps.
if (digitalRead(buttonPin) == LOW) NumStep = NumStep + 300; //If coin is added, add 300s
NumStep = NumStep - 1;
}
NumStep = 0;
display.showNumberDec(NumStep);
}
In your case, NumStep is initialized to 0. In the loop you compare it to 0 and do thing if it is greater than 0, but this never happens. So I believe that you only see a 0 on your LCD. It's just as if your code was:
void loop()
{
if (digitalRead(buttonPin) == LOW) NumStep = 300; //Add 300 s when coin is added
NumStep = 0;
display.showNumberDec(NumStep);
}
So just initalize NumStep to something else such as 10, and you'll see what you expect.
Even though it initiates at 0 it does recognize the first button press which sets the timer to 300. It doesn't recognize any additional button presses however, which my intention is for additional button presses when inside the while loop will add an additional 300s to the timer.
Additionally, when I initialized NumStep to 10 as you suggested, it no longer recognizes any button press not even the first one.
Well it's not really a button it just acts like one. It is a pulse coming from a mechanism that recognizes a coin being deposited. I imagine they are consistent though, but it is pretty quick.
I think the delay is my issue. I attempted to pump a few coins through as quickly as possible and was able to get it to read it. Do you have any suggestions for someway to either count down the timer differently or some way to extend the pulse?
It definitely was the issue with the delay. Thankfully I was able to simply add a capacitor to the coin mechanism circuit to prolong the pulse long enough for the code to receive the signal. Thank you all for the assistance and advice.
While you seem to have gotten it to work, it may not be consistent or maybe you will encounter problems if you decide to modify it in the future. The best idea is to get rid of the delay, and the theory behind it and why using delay is a bad idea is laid out in very good and clear detail here.