I have connected a HSV-300 bill acceptor and Adafruit 16X32 LED Matrix to my Arduino Uno. I will not include the LED stuff here as it is working. The problem is with the pulses from the bill collector. Due to the LED matrix, I can not use the hardware interrupt pins. I have my bill counter on pin 12.
When a bill is input into the bill acceptor, it sends a multiple of high pulses for each bill. So for a $1 bill, it would pulse once at 50ms. For a $5 bill, it would pulse 5 times at 50ms in between each pulse. So the pulses come in groups depending on the amount of the bill. The problem is that the pulses from the bill collector are noisy which it seems makes interrupts impossible.
I’m guessing are three approaches to my problem
- to use interrupts (preferred but unsure if it is possible due to the noisy inputs)
- create code to count the pulses in each group
- create a circuit to reduce the noisiness of the pulses
I have read this post and it is useful but not working as the output from my bill collector is noisy.
Counting pulses using interrupt and timer
When I use the interrupt solution (either on RISING, FALLING, or CHANGING), because of the noisy input, the ISR function it is continually called. Is there a way to prevent this?
// Connects a HSV-300 bill acceptor
//Includes
#include <EnableInterrupt.h>
//****************************************************************
// Constants //
// pin for the bill validator's credit(-) line
const int billValidatorPin = 12;
unsigned long duration;
//****************************************************************
// Variables //
volatile byte coinPulseCount=0; // a counter to see how many times the pin has changed - which coin inserted
byte newCoinInserted; // a place to put our last coin pulse count
volatile unsigned long pulseTime; //this stores the time of the last pulse.
//****************************************************************
//DEFINE FUNCTIONS
void coinpulse()
{
coinPulseCount++;
pulseTime = micros(); //store current time in pulseTime
Serial.println("Pulse");
}
//****************************************************************
void setup() {
// Pin setups for bill validator and button
pinMode(billValidatorPin, INPUT);
// Initialize serial ports for communication.
Serial.begin(9600);
Serial.println("Waiting for dollar...");
//attachInterrupt(digitalPinToInterrupt(billValidatorPin), coinpulse, RISING); // attach a PinChange Interrupt to our pin on the rising edge and execute the function burpcount when that pin changes - ALWAYS CHECKING CHANGES
enableInterrupt(billValidatorPin,coinpulse,FALLING);
}
//****************************************************************
void loop()
{
if (coinPulseCount >0 && micros() - pulseTime > 1000) //if there is a coin count & the time between now and the last pulse is greater than 1/4 of a second - THE END OF BANK OF PULSES
{
newCoinInserted = coinPulseCount; //new variable to free up coinPulseCount on the interrupt.
Serial.println("test");
coinPulseCount = 0; // clear pulse count ready for a new pulse on the interrupt.
}
//Proccess the coin inserted
switch (newCoinInserted)
{
case 1:
Serial.println("$1 inserted");
newCoinInserted = 0;
break;
case 5:
Serial.println("$5 inserted");
newCoinInserted = 0;
break;
case 10:
Serial.println("$10 inserted");
newCoinInserted = 0;
break;
case 20:
Serial.println("$20 inserted");
newCoinInserted = 0;
break;
}
}
This code works but can’t figure out how to program it to get the desired result. I have been able to get some success but not able to count the pulses in a group, just each pulse. Commenting out the enableInterrupt(billValidatorPin,coinpulse,FALLING); from above
void loop() {
// get the duration of the pulse
duration = pulseIn(billValidator, HIGH);
// Receiving a dollar bill will create a pulse with a duration of 50ms.
// NOTE: When there is no dollar bill pulse, I will receive a pulse of 8400 - 8600 on every loop.
// This is the noisy input
// Dollar received
if(duration > 12000)
{
// Count dollar
dollarCounter++;
// Checking for understanding
Serial.print("Dollar detected.\n Total: ");
// Display new dollar count
Serial.println(dollarCounter);
}
}
I am not a EE so option 3 is a little difficult for me (adding a resistor, etc).
Thank you for any help.