Trouble with running RC522 RFID together with bill/coin acceptors
good day, i'm using arduino micro , can't seem merge rfid acceptors. if run rfid , acceptors independtly, work great. when combine them rfid 1 working fine , acceptors output serial, dont or output wrong count.
my goal whatever comes first, coin, bill, or tapped card, processed. not simultaneously comes first.
as connections, followed rc522 library irq not connected. , acceptors using 1 digital pin each. used 5v optocoupler clean pulse signal acceptors. connected gnd.
thanks help! appreciate it
code: [select]
#include <spi.h>
#include <mfrc522.h>
constexpr uint8_t rst_pin = 9;
constexpr uint8_t ss_pin = 10;
mfrc522 rfid(ss_pin, rst_pin);
const byte coinpin = 2;
const byte billpin = 3;
volatile int min_pulse_width; // minimum pulse width acccept
volatile int max_pulse_width; // maximum pulse width accept
volatile int debounce_speed; // ignore changes in input line state happen faster this
volatile int pulse_count; // how many pulses have been received far in pulse train
volatile unsigned long pulse_duration; // how long last pulse
volatile unsigned long pulse_begin; // when did last pulse begin
volatile unsigned long pulse_end; // if pulse within min , max pulse width, when did end
volatile unsigned long curtime; // current time
volatile int post_pulse_pause; // how long wait after last pulse before sending pulse count
volatile int pulse_state; // current input line state (1 high, 0 low)
volatile int last_state; // last input line state
void rfidcall()
{
if (rfid.picc_isnewcardpresent() && rfid.picc_readcardserial())
{
(byte = 0; < rfid.uid.size; i++)
{
serial.print(rfid.uid.uidbyte[i] < 0x10 ? "0" : "");
serial.print(rfid.uid.uidbyte[i], hex);
}
rfid.picc_halta();
serial.print("\n"); //to give newline seril output
}
}
void coin()
{
pulse_state = digitalread(coinpin);
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++;
}
}
}
void bill()
{
pulse_state = digitalread(billpin);
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++;
}
}
}
void count()
{
if ((pulse_end > 0) && (curtime - pulse_end > post_pulse_pause)) // check if we've waited long enough don't expect further pulses forthcoming
{
switch (pulse_count)
{
case 1:
serial.println(f("1"));
pulse_count = 0;
break;
case 5:
serial.println(f("5"));
pulse_count = 0;
break;
case 10:
serial.println(f("10"));
pulse_count = 0;
break;
case 20:
serial.println(f("20"));
pulse_count = 0;
break;
case 50:
serial.println(f("50"));
pulse_count = 0;
break;
case 100:
serial.println(f("100"));
pulse_count = 0;
break;
case 200:
serial.println(f("200"));
pulse_count = 0;
break;
case 500:
serial.println(f("500"));
pulse_count = 0;
break;
case 1000:
serial.println(f("1000"));
pulse_count = 0;
break;
}
pulse_end = 0;
}
}
void setup()
{
pinmode(coinpin, input_pullup);
serial.begin(9600);
// keyboard.begin();
pulse_begin = 0;
last_state = 0;
min_pulse_width = 40;
max_pulse_width = 60;
debounce_speed = 1;
post_pulse_pause = 150;
pulse_end = 0;
pulse_count = 0;
spi.begin();
rfid.pcd_init(); // init mfrc522
}
void loop()
{
curtime = millis();
coin();
bill();
count();
rfidcall();
}
my goal whatever comes first, coin, bill, or tapped card, processed. not simultaneously comes first.
as connections, followed rc522 library irq not connected. , acceptors using 1 digital pin each. used 5v optocoupler clean pulse signal acceptors. connected gnd.
thanks help! appreciate it
i've fixed somehow. seems rfid blocks pulse counting i've set rfid work when pulse count zero.
code: [select]
#include <spi.h>
#include <mfrc522.h>
constexpr uint8_t rst_pin = 9;
constexpr uint8_t ss_pin = 10;
mfrc522 rfid(ss_pin, rst_pin);
const byte coinpin = 2;
const byte billpin = 3;
volatile int min_pulse_width; // minimum pulse width acccept
volatile int max_pulse_width; // maximum pulse width accept
volatile int debounce_speed; // ignore changes in input line state happen faster this
volatile int pulse_count; // how many pulses have been received far in pulse train
volatile unsigned long pulse_duration; // how long last pulse
volatile unsigned long pulse_begin; // when did last pulse begin
volatile unsigned long pulse_end; // if pulse within min , max pulse width, when did end
volatile unsigned long curtime; // current time
volatile int post_pulse_pause; // how long wait after last pulse before sending pulse count
volatile int pulse_state; // current input line state (1 high, 0 low)
volatile int last_state; // last input line state
void rfidcall()
{
if (pulse_count == 0)
{
if (rfid.picc_isnewcardpresent() && rfid.picc_readcardserial())
{
(byte = 0; < rfid.uid.size; i++)
{
serial.print(rfid.uid.uidbyte[i] < 0x10 ? "0" : "");
serial.print(rfid.uid.uidbyte[i], hex);
}
rfid.picc_halta();
serial.print("\n"); //to give newline seril output
}
}
else
return;
}
void coin()
{
pulse_state = digitalread(coinpin);
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++;
}
}
if ((pulse_end > 0) && (curtime - pulse_end > post_pulse_pause)) // check if we've waited long enough don't expect further pulses forthcoming
{
if (pulse_count == 1)
{
serial.println(f("1"));
pulse_count = 0;
curtime = 0;
}
else if ((pulse_count > 1) && (pulse_count <= 6))
{
serial.println(f("5"));
pulse_count = 0;
curtime = 0;
}
else if ((pulse_count > 5) && (pulse_count <= 11))
{
serial.println(f("10"));
pulse_count = 0;
curtime = 0;
}
else if ((pulse_count > 16) && (pulse_count <= 21))
{
serial.println(f("20"));
pulse_count = 0;
curtime = 0;
}
else if ((pulse_count > 20) && (pulse_count <= 51))
{
serial.println(f("50"));
pulse_count = 0;
curtime = 0;
}
else if ((pulse_count > 50) && (pulse_count <= 101))
{
serial.println(f("100"));
pulse_count = 0;
curtime = 0;
}
else if ((pulse_count > 100) && (pulse_count <= 201))
{
serial.println(f("200"));
pulse_count = 0;
curtime = 0;
}
else if ((pulse_count > 200) && (pulse_count <= 401))
{
serial.println(f("500"));
pulse_count = 0;
curtime = 0;
}
else if ((pulse_count > 500) && (pulse_count <= 1001))
{
serial.println(f("1000"));
pulse_count = 0;
curtime = 0;
}
else
{
serial.println(f("f u"));
pulse_count = 0;
curtime = 0;
}
pulse_end = 0;
}
}
void bill()
{
pulse_state = digitalread(billpin);
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++;
}
}
if ((pulse_end > 0) && (curtime - pulse_end > post_pulse_pause)) // check if we've waited long enough don't expect further pulses forthcoming
{
if (pulse_count == 1)
{
serial.println(f("1"));
pulse_count = 0;
curtime = 0;
}
else if ((pulse_count > 1) && (pulse_count <= 6))
{
serial.println(f("5"));
pulse_count = 0;
curtime = 0;
}
else if ((pulse_count > 5) && (pulse_count <= 11))
{
serial.println(f("10"));
pulse_count = 0;
curtime = 0;
}
else if ((pulse_count > 10) && (pulse_count <= 21))
{
serial.println(f("20"));
pulse_count = 0;
curtime = 0;
}
else if ((pulse_count > 20) && (pulse_count <= 51))
{
serial.println(f("50"));
pulse_count = 0;
curtime = 0;
}
else if ((pulse_count > 50) && (pulse_count <= 101))
{
serial.println(f("100"));
pulse_count = 0;
curtime = 0;
}
else if ((pulse_count > 100) && (pulse_count <= 201))
{
serial.println(f("200"));
pulse_count = 0;
curtime = 0;
}
else if ((pulse_count > 200) && (pulse_count <= 401))
{
serial.println(f("500"));
pulse_count = 0;
curtime = 0;
}
else if ((pulse_count > 500) && (pulse_count <= 1001))
{
serial.println(f("1000"));
pulse_count = 0;
curtime = 0;
}
else
{
serial.println(f("f u"));
pulse_count = 0;
curtime = 0;
}
pulse_end = 0;
}
}
void setup()
{
pinmode(coinpin, input_pullup);
serial.begin(9600);
// keyboard.begin();
pulse_begin = 0;
last_state = 0;
min_pulse_width = 40;
max_pulse_width = 60;
debounce_speed = 1;
post_pulse_pause = 150;
pulse_end = 0;
pulse_count = 0;
spi.begin();
rfid.pcd_init(); // init mfrc522
}
void loop()
{
curtime = millis();
coin();
bill();
rfidcall();
}
Arduino Forum > Using Arduino > Project Guidance > Trouble with running RC522 RFID together with bill/coin acceptors
arduino
Comments
Post a Comment