arduino qr

I have some issues with my code, I’m using waveshare QR code camera. its compile fine with no issues, but sometimes it read the bar code fine and some times doesn’t read, can someone help?

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>

#define RELAY 3 //relay pin
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
char targetChars = {“X0022OK6E3”}; //This is the reference array that we’re looking for from MySQL or whatever source we end up using (eventually)
boolean newData = false;
boolean acceptData = false;

void setup() {
Serial.begin(9600);
Serial.println("");

}

void loop() {
recvWithEndMarker();
showNewData();
compareNewData();
}

void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = ‘\n’;
char rc;

while (Serial.available() > 0 && newData == false) {
rc = Serial.read();

if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = ‘\0’; // terminate the string
ndx = 0;
newData = true;
}
}
}

void showNewData() {
if (newData == true) {
Serial.print("This just in … ");
Serial.println(receivedChars);
newData = false;
}
}
void compareNewData() {
if (strcmp(receivedChars, targetChars) == 0) {
Serial.print(“Accepted”);
digitalWrite(RELAY, HIGH);
delay(500);
digitalWrite(RELAY, LOW);
delay(500);
}
else {
Serial.print(“Rejected”);
delay(1500);
while (Serial.available() > 0) {
Serial.read();
}
}
}

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

hayo:
I have some issues with my code, I'm using waveshare QR code camera. its compile fine with no issues, but sometimes it read the bar code fine and some times doesn't read, can someone help?

The Arduino sketch doesn't seem to be doing anything that would prevent it from receiving messages. If the camera is not sending messages when you expect it to send messages it sounds like the camera is faulty.

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>

#define RELAY 3 //relay pin
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
char targetChars[] = {"X0022OK6E3"}; //This is the reference array that we're looking for from MySQL or whatever source we end up using (eventually)
boolean newData = false;
boolean acceptData = false;

void setup() {
   Serial.begin(9600);
   Serial.println("<Scan the package>");
  
}

void loop() {
   recvWithEndMarker();
   showNewData();
   compareNewData();
}

void recvWithEndMarker() {
   static byte ndx = 0;
   char endMarker = '\n';
   char rc;
  
   while (Serial.available() > 0 && newData == false) {
       rc = Serial.read();

       if (rc != endMarker) {
           receivedChars[ndx] = rc;
           ndx++;
           if (ndx >= numChars) {
               ndx = numChars - 1;
           }
       }
       else {
           receivedChars[ndx] = '\0'; // terminate the string
           ndx = 0;
           newData = true;
       }
   }
}

void showNewData() {
   if (newData == true) {
       Serial.print("This just in ... ");
       Serial.println(receivedChars);
       newData = false;
   }
}
void compareNewData() {
   if (strcmp(receivedChars, targetChars) == 0) {
   Serial.print("Accepted");
   digitalWrite(RELAY, HIGH);
   delay(500);
   digitalWrite(RELAY, LOW);
   delay(500);
           }
       else {
   Serial.print("Rejected");                                                      
   delay(1500); 
   while (Serial.available() > 0) {
   Serial.read();
     }
   }
}

//}