I am trying to use a TTL magnetic stripe reader (used to get info from cards with a black stripe on the back) to temporarily turn on an LED when a card with 11 characters is swiped. It only works before I integrate the Blynk code; one can toggle between the Blynk and Blynk-less code by commenting/uncommenting chunks of code preceded by “BLYNK VERSION ONLY” or “NON-BLYNK VERSION ONLY”.
When connecting to Blynk, over USB for Arduino Mega, pressing the virtual button that should allow me to start card swiping never resets the virtual button to 0 or turns on the LED as I would expect with my 11-character card.
I have had bad luck making functions and using serial for my hardware with previous projects, and shamefully but successfully threw most of my code in a function I call rest_of_code while avoiding the use of serial besides Blynk’s needs. I abstained from those decisions with the below code, but diagnosing any flaw(s) might begin with scrutinizing my code under those topics. Thank you.
// BLYNK VERSION ONLY
//#define BLYNK_PRINT Serial1 //print
//#include <BlynkSimpleStream.h>
//char auth[] = "7pWdjr_v_M66N6aK8yrB9_v2B9CLsm1Q";
int ccri_swipe;
int led=13;
int cld1Pin = 9; // Card status pin
int rdtPin = 19; // Data pin
int reading = 0; // Reading status
volatile int buffer[400]; // Buffer for data
volatile int i = 0; // Buffer counter
volatile int bit = 0; // global bit
char cardData[40]; // holds card info
int charCount = 0; // counter for info
int DBUG = 0;
void setup() {
//BLYNK VERSION ONLY
// Serial1.begin(9600);
// Serial.begin(9600);
// Blynk.begin(Serial, auth);
Serial2.begin(9600);
// The interrupts are key to reliable
// reading of the clock and data feed
pinMode(led, OUTPUT);
attachInterrupt(4, changeBit, CHANGE);
attachInterrupt(5, writeBit, FALLING);
}
void loop()
{
//Blynk.run(); //BLYNK VERSION ONLY
rest_of_code();//NON-BLYNK VERSION ONLY
}
void rest_of_code(){
// Active when card present
while (ccri_swipe!=1){
while(digitalRead(cld1Pin) == LOW){
reading = 1;
}
// Active when read is complete
// Reset the buffer
if(reading == 1) {
if (DBUG == 1) {
printBuffer();
}
charCount=decode();
if (charCount==11)
{
digitalWrite(led, HIGH);
delayMicroseconds(16383);
ccri_swipe=1;
}
reading = 0;
i = 0;
int l;
for (l = 0; l < 40; l = l + 1) {
cardData[l] = '\n';
}
charCount = 0;
}
}
ccri_swipe=0;
//Blynk.virtualWrite(V6, 0); //BLYNK VERSION ONLY
digitalWrite(led,LOW);
}
// Flips the global bit
void changeBit(){
if (bit == 0) {
bit = 1;
} else {
bit = 0;
}
}
// Writes the bit to the buffer
void writeBit(){
buffer[i] = bit;
i++;
}
// prints the buffer
void printBuffer(){
int j;
for (j = 0; j < 200; j = j + 1) {
Serial2.println(buffer[j]);
}
}
int getStartSentinal(){
int j;
int queue[]={0,0,0,0,0};
int sentinal = 0;
for (j = 0; j < 400; j = j + 1) {
queue[4] = queue[3];
queue[3] = queue[2];
queue[2] = queue[1];
queue[1] = queue[0];
queue[0] = buffer[j];
if (DBUG == 1) {
Serial2.print(queue[0]);
Serial2.print(queue[1]);
Serial2.print(queue[2]);
Serial2.print(queue[3]);
Serial2.println(queue[4]);
}
if (queue[0] == 0 & queue[1] == 1 & queue[2] == 0 & queue[3] == 1 & queue[4] == 1) {
sentinal = j - 4;
break;
}
}
if (DBUG == 1) {
Serial2.print("sentinal:");
Serial2.println(sentinal);
Serial2.println("");
}
return sentinal;
}
int decode() {
int sentinal = getStartSentinal();
int j;
int i = 0;
int k = 0;
int thisByte[5];
for (j = sentinal; j < 400 - sentinal; j = j + 1) {
thisByte[i] = buffer[j];
i++;
if (i % 5 == 0) {
i = 0;
if (thisByte[0] == 0 & thisByte[1] == 0 & thisByte[2] == 0 & thisByte[3] == 0 & thisByte[4] == 0) {
break;
}
printMyByte(thisByte);
}
}
Serial2.print("Stripe_Data:");
for (k = 0; k < charCount; k = k + 1) {
Serial2.print(cardData[k]);
}
Serial2.println("");
return charCount;
}
void printMyByte(int thisByte[]) {
int i;
for (i = 0; i < 5; i = i + 1) {
if (DBUG == 1) {
Serial2.print(thisByte[i]);
}
}
if (DBUG == 1) {
Serial2.print("\t");
Serial2.print(decodeByte(thisByte));
Serial2.println("");
}
cardData[charCount] = decodeByte(thisByte);
charCount ++;
}
char decodeByte(int thisByte[]) {
if (thisByte[0] == 0 & thisByte[1] == 0 & thisByte[2] == 0 & thisByte[3] == 0 & thisByte[4] == 1){return '0';}
if (thisByte[0] == 1 & thisByte[1] == 0 & thisByte[2] == 0 & thisByte[3] == 0 & thisByte[4] == 0){return '1';}
if (thisByte[0] == 0 & thisByte[1] == 1 & thisByte[2] == 0 & thisByte[3] == 0 & thisByte[4] == 0){return '2';}
if (thisByte[0] == 1 & thisByte[1] == 1 & thisByte[2] == 0 & thisByte[3] == 0 & thisByte[4] == 1){return '3';}
if (thisByte[0] == 0 & thisByte[1] == 0 & thisByte[2] == 1 & thisByte[3] == 0 & thisByte[4] == 0){return '4';}
if (thisByte[0] == 1 & thisByte[1] == 0 & thisByte[2] == 1 & thisByte[3] == 0 & thisByte[4] == 1){return '5';}
if (thisByte[0] == 0 & thisByte[1] == 1 & thisByte[2] == 1 & thisByte[3] == 0 & thisByte[4] == 1){return '6';}
if (thisByte[0] == 1 & thisByte[1] == 1 & thisByte[2] == 1 & thisByte[3] == 0 & thisByte[4] == 0){return '7';}
if (thisByte[0] == 0 & thisByte[1] == 0 & thisByte[2] == 0 & thisByte[3] == 1 & thisByte[4] == 0){return '8';}
if (thisByte[0] == 1 & thisByte[1] == 0 & thisByte[2] == 0 & thisByte[3] == 1 & thisByte[4] == 1){return '9';}
if (thisByte[0] == 0 & thisByte[1] == 1 & thisByte[2] == 0 & thisByte[3] == 1 & thisByte[4] == 1){return ':';}
if (thisByte[0] == 1 & thisByte[1] == 1 & thisByte[2] == 0 & thisByte[3] == 1 & thisByte[4] == 0){return ';';}
if (thisByte[0] == 0 & thisByte[1] == 0 & thisByte[2] == 1 & thisByte[3] == 1 & thisByte[4] == 1){return '<';}
if (thisByte[0] == 1 & thisByte[1] == 0 & thisByte[2] == 1 & thisByte[3] == 1 & thisByte[4] == 0){return '=';}
if (thisByte[0] == 0 & thisByte[1] == 1 & thisByte[2] == 1 & thisByte[3] == 1 & thisByte[4] == 0){return '>';}
if (thisByte[0] == 1 & thisByte[1] == 1 & thisByte[2] == 1 & thisByte[3] == 1 & thisByte[4] == 1){return '?';}
}
//BLYNK VERSION ONLY
//BLYNK_WRITE(V6)
//{
// int pinValue = param.asInt();
// if(pinValue==1){
// rest_of_code();
// }
//}