Hello, Im starting a proyect in wich I connect a coin acceptor to my arduino uno through a sofware serial port (pin 2) to see the inserted coin values in the serial monitor. Here is the code that doesn't work as I want:
#include <SoftwareSerial.h>
SoftwareSerial mon(2,4); // RX, TX, rx recibe
int valor=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
mon.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if(mon.available()>0)
{
valor+=(mon.read())*10;
Serial.println(valor);
}
else mon.read();
}
The problem I've been having is that when I put a coin, nothing shows on the serial monitor.
So I tried to put the Serial.println(valor); line outside the "if" to see if the value of the "valor" variable was really changing.
#include <SoftwareSerial.h>
SoftwareSerial mon(2,4); // RX, TX, rx recibe
int valor=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
mon.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if(mon.available()>0)
{
valor+=(mon.read())*10;
}
else mon.read();
Serial.println(valor);
}
In this case, with the code line outside it works with no problem as you can see in the image here.
My question is why it doesn't work. I wanted the value to show just once. And only printing on the serial monitor when I insert the coin, instead of that the monitor shows an endless stream of values.
Thanks
