johnwasser:
Let's start with the File->Examples->02.Digital->StateChangeDetection example. Make a slight change to count 10 for each input 'button press'. Then send an output pulse if the count is > 0.
// this constant won't change:
const int ButtonPin = 2; // the pin that the pushbutton is attached to
const int OutputPin = 13; // the pin that the LED is attached to
// Variables will change:
int OutputPulseCounter = 0; // counter for the number of button presses
boolean LastButtonState = HIGH; // previous state of the button
void setup()
{
// initialize the button pin as a input:
pinMode(ButtonPin, INPUT_PULLUP); // Use PULLUP in case the signal is Open Collector.
// initialize the output:
pinMode(OutputPin, OUTPUT);
}
void loop()
{
static unsigned long lastButtonChangeTime = 0;
unsigned long currentTime = millis();
// read the pushbutton input pin:
boolean buttonState = digitalRead(ButtonPin);
// compare the buttonState to its previous state (ignore bounces)
if (buttonState != LastButtonState && currentTime - lastButtonChangeTime > 10)
{
lastButtonChangeTime = currentTime;
// if the state has changed, increment the counter
if (buttonState == LOW)
{
// if the current state is LOW then the input went from off to on:
OutputPulseCounter += 10;
}
}
// save the current state as the last state, for next time through the loop
LastButtonState = buttonState;
if (OutputPulseCounter > 0)
{
// Output one of the pulses
digitalWrite(OutputPin, HIGH);
digitalWrite(OutputPin, LOW);
OutputPulseCounter--;
}
}
johnwasser:
Let's start with the File->Examples->02.Digital->StateChangeDetection example. Make a slight change to count 10 for each input 'button press'. Then send an output pulse if the count is > 0.
// this constant won't change:
const int ButtonPin = 2; // the pin that the pushbutton is attached to
const int OutputPin = 13; // the pin that the LED is attached to
// Variables will change:
int OutputPulseCounter = 0; // counter for the number of button presses
boolean LastButtonState = HIGH; // previous state of the button
void setup()
{
// initialize the button pin as a input:
pinMode(ButtonPin, INPUT_PULLUP); // Use PULLUP in case the signal is Open Collector.
// initialize the output:
pinMode(OutputPin, OUTPUT);
}
void loop()
{
static unsigned long lastButtonChangeTime = 0;
unsigned long currentTime = millis();
// read the pushbutton input pin:
boolean buttonState = digitalRead(ButtonPin);
// compare the buttonState to its previous state (ignore bounces)
if (buttonState != LastButtonState && currentTime - lastButtonChangeTime > 10)
{
lastButtonChangeTime = currentTime;
// if the state has changed, increment the counter
if (buttonState == LOW)
{
// if the current state is LOW then the input went from off to on:
OutputPulseCounter += 10;
}
}
// save the current state as the last state, for next time through the loop
LastButtonState = buttonState;
if (OutputPulseCounter > 0)
{
// Output one of the pulses
digitalWrite(OutputPin, HIGH);
digitalWrite(OutputPin, LOW);
OutputPulseCounter--;
}
}
Hi All,
Johnwasser had helped me to write this code for my project, but after finally getting it to upload to the MiniPro and connect it to the gaming board to add credits, it (coin jam) the the game board. I am using a micro switch on Pin 2 input to create a one punch input and on pin 13 output to create 10 pulse output. I cant seems to get it correct. so I am posting what I have this far to see if anyone could help to correct it.
Thanks
Harold
// this constant won't change
const int ButtonPin = 2; // the pin that micro switch or bill acceptor is connected to
const int OutputPin = 13; // the pin that is attached to game pcb edge connector
// Variables will change:
int OutputPulseCounter = 10; // counter for the number of pulse from mini pro to game pcb
boolean LastButtonState = HIGH; // previous state of pulse
void setup() {
// initialize the button pin as an input:
pinMode(ButtonPin, INPUT_PULLUP); // Use PULLUP in case the signal is Open Collector:
// initialize the output:
pinMode(OutputPin, OUTPUT);
}
void loop()
{
static unsigned long LastButtonChangeTime = 0;
unsigned long CurrentTime = 50;
// read the pushbuttoninput pin:
boolean buttonState = digitalRead(ButtonPin);
// compare the buttonState to its previous state (ignore bounces)
if (buttonState !=LastButtonState && CurrentTime - LastButtonChangeTime > 10);
{
LastButtonChangeTime = CurrentTime;
// if the state has changed, increment the counter if (buttonState ==Low)
{
// if the current state is LOW then the input went from off to on:
OutputPulseCounter += 10;
}
}
// save the current state as the last state, for next time through the loop
LastButtonState = buttonState;
if (OutputPulseCounter > 0)
{
// Output one of the pulses
digitalWrite(OutputPin, HIGH);
digitalWrite(OutputPin, LOW);
OutputPulseCounter--;
}
}