I have a multi-coin acceptor and I am trying to get this thing working and since I don't have the necessary programming or "arduino working principle" knowledge sinked in enough, I can't "bend" the program to my needs at the moment. The problem I will ask about here is actually a different thing occuring in the setup.
Searching through the forum (even though I can't remember where I got it from) I've found the code and modified it (just a little) for my use.
#define INPUTPIN (3)
int min_pulse_width; // the minimum pulse width to acccept
int max_pulse_width; // the maximum pulse width to accept
int debounce_speed; // ignore changes in input line state that happen faster than this
int pulse_count; // how many pulses have been received so far in this pulse train
int counter = 0;
unsigned long pulse_duration; // how long was the last pulse
unsigned long pulse_begin; // when did the last pulse begin
unsigned long pulse_end; // if they pulse was within min and max pulse width, when did it end
unsigned long curtime; // what is the current time
unsigned long curtime1; // what is the current time
int post_pulse_pause; // how long to wait after last pulse before sending pulse count
int pulse_state; // what is the current input line state (1 for high, 0 for low)
int last_state; // what was the last input line state
float balance;
int entry_coin;
float entry_coin1;
unsigned long time_now;
char rx_byte = 0;
void setup() {
pinMode(INPUTPIN, INPUT_PULLUP);
Serial.begin(9600);
Serial.println("... System On ... ");
// Keyboard.begin();
pulse_begin = 0;
last_state = 0;
min_pulse_width = 8;
max_pulse_width = 1000;
debounce_speed = 4;
post_pulse_pause = 2000;
pulse_end = 0;
pulse_count = 0;
}
void loop() {
pulse_state = digitalRead(INPUTPIN);
coinPulse();
coinPrint();
curtime = millis();
rx_byte = Serial.read();
if (rx_byte>= '0'){
Serial.println(balance);
}
}
void coinPulse(){
if((pulse_state == 1) && (last_state == 0)) {
pulse_begin = curtime;
last_state = 1;
} else if((pulse_state == 0) && (last_state == 1)) {
pulse_duration = curtime - pulse_begin;
if(pulse_duration > debounce_speed) {
last_state = 0;
}
if(pulse_duration > min_pulse_width) {
pulse_end = curtime;
pulse_count++;
balance = (float)pulse_count/4;
}
}
}
void coinPrint(){
if ((pulse_state == 1)){
time_now = curtime;
}
if (curtime-time_now == 150){
Serial.print("Balance: ");
Serial.print(balance);
Serial.println(" coins");
}
}
So far there are no problems with the detection and seems to be working well but I don't know if I am missing something but shouldn't it just print 1 time ?
I'm getting 3 lines of print as shown in the picture attachment.
Should I try a different setup for using the "print" codes, I want to make the code write a few things more but I believe first I have to understand why this happens.
The program is doing exactly as you write it to do, and there are lots of problems with it. Post the original program so forum members can see what you have done to it.
We recommend that you work through some of the examples that come with the development software, as you lack the background and experience to make sensible modifications, especially for the serial input routines. See Serial Input Basics for more information about them.
I've found it, the original code is used to find the pulse width:
/*
This program will print the width of each pulse it receives on a new line.
Use it to find the pulse width of a bill acceptor.
Just set the min_pulse_width to something lower than what you expect the pulse width to be.
It is not common to have pulses of less than 8 ms so I set it to 8.
*/
#define INPUTPIN (15)
int min_pulse_width; // the minimum pulse width to acccept
int max_pulse_width; // the maximum pulse width to accept
int debounce_speed; // ignore changes in input line state that happen faster than this
int pulse_count; // how many pulses have been received so far in this pulse train
unsigned long pulse_duration; // how long was the last pulse
unsigned long pulse_begin; // when did the last pulse begin
unsigned long pulse_end; // if they pulse was within min and max pulse width, when did it end
unsigned long curtime; // what is the current time
int post_pulse_pause; // how long to wait after last pulse before sending pulse count
int pulse_state; // what is the current input line state (1 for high, 0 for low)
int last_state; // what was the last input line state
void setup() {
delay(2000);
pinMode(INPUTPIN, INPUT);
Serial.begin(115200);
// Keyboard.begin();
pulse_begin = 0;
last_state = 0;
min_pulse_width = 8;
max_pulse_width = 1000;
debounce_speed = 4;
post_pulse_pause = 2000;
pulse_end = 0;
pulse_count = 0;
}
void loop() {
pulse_state = digitalRead(INPUTPIN);
curtime = millis();
if((pulse_state == 1) && (last_state == 0)) {
pulse_begin = curtime;
last_state = 1;
} else if((pulse_state == 0) && (last_state == 1)) {
pulse_duration = curtime - pulse_begin;
if(pulse_duration > debounce_speed) {
last_state = 0;
}
if(pulse_duration > min_pulse_width) {
pulse_end = curtime;
Serial.print(pulse_duration);
Serial.println();
pulse_count++;
}
}
}
I didn't discard some of the statements (the time calculation for pulse) as I viewed it to not oppose me anyway.
The serial In was just something random I've added the see the "balance" value, it does not interfere or change what the program does (as I see it).
What about the 3 lines of print I'm getting ?
I know that if I've written the serial.print lines inside the pulse detection parts it would have given me an output whenever the HIGH was detected (in that case for "1" coin it will give 4 outputs) and I don't want that, after it has read the coin that I've inputted, I want it to display me the message only one time. If a pulse is 20 ms, that would give me 80 ms waiting time that is why I've written the:
if (curtime-time_now == 150)
line
Can you elaborate a little ? Since I'm new to these concepts. The setup I've used is like this
I directly connected the Coin acceptors COIN pin (White cable) to the arduino (pin 3), connected the ground of the arduino and the coin acceptor together, changed the contact type (NO-NC -> to NC) so that it was suited to use with PULL_UP, used the INPUT_PULLUP function on it.
The others I've tried were:
I couldn't make the system work with the COUNTER pin on the coin acceptor.
I tried NO contact, so as it detected the coin it would send the signal, but the arduino did not get that as I believe the signal did not reach the voltage required for Arduino to consider HIGH.
I used the interrupt function, that worked but still it was effected by the electrical equipment around and was not stable.
I don't know if the setup I'm using now is bad for the devices but it is the best one working among the methods I've tried before. It is accurate, the only thing I couldn't manage was the display part.