I want to use a coin acceptor(Coin Acceptor - Programmable (6 coin types) - COM-11636 - SparkFun Electronics ) in a project but I am having difficulties getting it to work properly.
I have been following the hatermatic from Sparkfun to get the base of reading out the coin acceptor running.
Thus I have set the dip switch 3 to serial and the baudrate to 9600.
So far so good and after training the coins, the acceptor displays the correct values on the led of the machine itself, I imagined I would be ready to get this rolling.
However with the following code, all I am reading from my acceptor is 255 values, while googling this I saw other people ran into this issue as well, however never was a solution mentioned unfortunately.
I am also only getting these 255's after adding a couple coins first.
My charge amount is set to 1 (I believe that is correct?), my test coin value has been different values with always that same 255 result.
What is going on, what am I missing?
Thx for any help
#include <SoftwareSerial.h>
#define COIN_INPUT 7
SoftwareSerial coinInput(COIN_INPUT,3); //RX , TX
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("coin acceptor ready");
coinInput.begin(9600);
}
void loop() {
static unsigned int cashola=0;
if (coinInput.available() > 0)
{
// Oddly enough, for nickels and dimes, the data coming out of the coin
// acceptor has a framing error: the last bit is a little too long. The
// SoftwareSerial receive function doesn't seem to wait for the stop bit
// (which will always be one), so it starts another read when it sees the
// extra long final bit, then reads all ones, which is interpreted as -1.
// We want to reject any result which is -1, since that's a nonsense coin
// value anyway.
if (coinInput.peek() != -1)
{
cashola += coinInput.read();
Serial.println(cashola);
}
else coinInput.read();
}
}
Sounds like it didn't accepted the dip switch change. Another possibility is that you have dip switch 1 in the wrong position and the serial signal is inverted.
Post a wiring diagram of your setup and the state of all 4 dip switches!
Your link is broken due to the ) at the end of it; please fix.
The Sparkfun video glosses over the spurious appearances of the 255s without explaining their purpose.
The comment in the code about the framing error suggests an issue they haven't bothered investigating.
S:Start bit
B0 to B7:8 data bits
C:Even check
STOP:Stop bits
I'm going to guess you need to enable even parity.
I don't think choosing that is supported in SoftwareSerial, maybe other software implementations of serial?
You'd be better off using hardware serial anyway until you get it working properly; in which case you can use:
I have dip switch 3 on and all the others in the off position, I also switched dip 3 again to see if it made a difference.
I just have 1 wire going from signal to port 7.
@arduarn, SoftwareSerial does not accept the serial change you propose.
Do you have other libraries for serial you can propose that use this?
As with the hardware serial I would be limimited to only using my serial RX port 0 on my arduino uno if I am correct?
counfhou:
Do you have other libraries for serial you can propose that use this?
As with the hardware serial I would be limimited to only using my serial RX port 0 on my arduino uno if I am correct?
A quick scan of a couple of other software serial implementations shows that they don't support this either.
So:
get an Arduino that has multiple serial ports, eg. a Mega with 4, or a Micro with 2 (one serial over USB port, and one on the pins)
or use software serial for printing the output to the serial monitor and use a TTL serial to USB converter or another Arduino configured as a serial to USB converter using software serial as well.
Perhaps someone else can suggest more options.
EDIT: Since you won't be writing any serial to the coin acceptor, you could also just connect up the serial output from the coin acceptor to the RX pin on the Uno. Do the Serial.begin() with SERIAL_8E1 as mentioned. Read from the Serial, and write it back out to Serial. Instead of using the Arduino IDE's serial monitor to view the output from the Uno, install another terminal program, eg. RealTerm (shown in the Sparkfun video) and configure it for 8 bits, even parity, 1 stop bit, 9600 baud. That should be enough for test purposes.
BTW: Thanks for fixing the link.
I am using this code now, with my signal cable to port 0 (rx).
However I am not reading anything on Realterm(settings added as screenshot)
void setup() {
// put your setup code here, to run once:
Serial.begin(9600,SERIAL_8E1);
Serial.println("coin acceptor ready");
//coinInput.begin(9600);
}
void loop() {
static unsigned int cashola=0;
if (Serial.available() > 0)
{
// Oddly enough, for nickels and dimes, the data coming out of the coin
// acceptor has a framing error: the last bit is a little too long. The
// SoftwareSerial receive function doesn't seem to wait for the stop bit
// (which will always be one), so it starts another read when it sees the
// extra long final bit, then reads all ones, which is interpreted as -1.
// We want to reject any result which is -1, since that's a nonsense coin
// value anyway.
if (coinInput.peek() != -1)
{
cashola = Serial.read();
Serial.println(cashola);
}
else Serial.read();
}
//delay(250);
}
I am not the best with terminals, so is my code wrong or is my realterm setup not correct?
Thx!
You shouldn't need to check for -1 if things are working correctly; I think that was just Sparkfun being lazy and not investigating a problem; let's see...
counfhou: @pylon: I have the ground and dc connect ofcourse, otherwise the machine wouldn't power up, that is what you mean right?
@arduarn, I tried your code but now I am reading nothing, not even the 255's for some reason (signal wire now wired to port 0/RX)
Am I doing something very stupid wrong or is this thing broken?
What pylon means is do you have the GND of the coin acceptor connected to the GND of the Arduino? If they are both connected to the same 12V power supply, then the answer is yes. If you are powering the coin acceptor from a 12V supply and the Arduino from USB (for example), then you will likely need to manually connect the GNDs.
So you are not getting anything displayed in Realterm; go back to basics. Disconnect the coin acceptor, burn a test sketch to the Arduino (eg. examples->basics->readanalogvoltage), configure Realterm back to 8 bits, no parity, 1 stop bit, then tweak Realterm until you get it to display the output from the Arduino. You can compare the output to the equivalent output in the serial monitor.
Hmm so I am getting reads now in Real term, however they are not a single pair but 5 pair of numbers.
I am also not getting a read with every coin, only sometimes/after multiple coins -> I can see my RX light does not light up only once in a while so I am not just missing these readings.
Also my acceptor is just connected to to a seperate power source and my arduino with usb to my computer.
If I connect the ground of the coin acceptor to my arduino grnd is just does not power on, so not sure if that is what you are asking about but a different wiring there seems to be not an option thus?
Also my acceptor is just connected to to a seperate power source and my arduino with usb to my computer.
If I connect the ground of the coin acceptor to my arduino grnd is just does not power on, so not sure if that is what you are asking about but a different wiring there seems to be not an option thus?
You must connect the GND of the acceptor and the Arduino, otherwise there is no common level and the transmission will never work reliably.
You seem to have wired in rather a mess. So post a complete wiring diagram! We have to fix the wiring before we can concentrate on the software.
A photo of a simple pen and paper drawing would be better than the Fritzing. As shown it is not going to work properly due to the lack of common ground.
Tell you what: Take a (clear and in focus) photo of your hardware which shows how it is connected up. Then, without powering it up, connect the grounds the same as you did before and take another photo.
What I tried at some point, and the coin acceptor did no longer power on, board was fine.
Was connect the ground pin of the coin acceptor into the ground of the arduino.
What you are saying is to introduce an extra wire and run it froum ground of the arduino to my black alligator clip and add it there together right?
counfhou:
What I tried at some point, and the coin acceptor did no longer power on, board was fine.
Was connect the ground pin of the coin acceptor into the ground of the arduino.
What you are saying is to introduce an extra wire and run it froum ground of the arduino to my black alligator clip and add it there together right?
Ah, I see. So you removed the GND wire from the power supply and attached it to the Arduino - no surprise that that didn't work.
Yes, you need to add an additional wire from the GND of your 12V power supply to the Arduino GND. Both the coin acceptor and the Arduino need to be connected to a shared GND - ie. a common reference potential.