I try to change the code to recognize certain card numbers. but how to
//int Card1 = 39005A9C88;
//int Card2 = 39005A9C85;
#include <SoftwareSerial.h>
#define STX 2
#define ETX 3
SoftwareSerial softSerial(10, 11);
int rx_counter;
byte rx_data[14]; // 1+10+2+1
void setup() {
rx_counter = 0; // init counter
Serial.begin(9600);
softSerial.begin(9600);
Serial.print("online...");
}
void loop() {
if (softSerial.available() > 0) {
rx_data[rx_counter] = softSerial.read();
if (rx_counter == 0 && rx_data[0] != STX) {
Serial.println("Out of sync!"); // do not increment rx_counter
} else {
rx_counter++;
}
if (rx_counter >= 14) {
rx_counter = 0; // reset counter
if (rx_data[0] == STX && rx_data[13] == ETX) { // packet starts with STX and ends with ETX
byte calc_checksum = 0;
for (int i = 0; i < 6; i++) { // data with checksum
calc_checksum ^= ascii_char_to_num(rx_data[i*2+1]) * 16 + ascii_char_to_num(rx_data[i*2+2]);
}
if (calc_checksum == 0) {
Serial.print("ID: ");
for (int i = 1; i <= 10; i++) {
Serial.write(rx_data[i]);
}
Serial.println();
} else {
Serial.println("Checksum ERROR!");
}
} else {
Serial.println("Incorrect packet!");
}
}
}
}
byte ascii_char_to_num(byte a) {
a -= '0'; // 0..9
if (a > 9) a -= 7; // A..F
return a;
}
if card ID == Card1 || Card2 { Serial.print("ON!")} Else {Serial.print("OFF!")}
system
April 25, 2016, 10:46am
2
Serial.print("ID: ");
for (int i = 1; i <= 10; i++) {
Serial.write(rx_data[i]);
}
What does that actually output?
//int Card1 = 39005A9C88;
//int Card2 = 39005A9C85;
if card ID == Card1 || Card2 { Serial.print("ON!")} Else {Serial.print("OFF!")}
It looks like you may mean something like:
char Card1ID[] = "39005A9C88";
char Card2ID[] = "39005A9C85";
if (strncmp(rx_data+1, Card1ID, 10) == 0 || strncmp(rx_data+1, Card2ID, 10) == 0) {
Serial.print("ON!");
} else {
Serial.print("OFF!");
}
invalid conversion from 'byte* {aka unsigned char*}' to 'const char*' [-fpermissive]
#include <SoftwareSerial.h>
#define STX 2
#define ETX 3
SoftwareSerial softSerial(10, 11); // recommended pins for RX on Mega: 10, 11, 12...
int rx_counter;
byte rx_data[14]; // 1+10+2+1
char Card1ID[] = "39005A9C88";
char Card2ID[] = "39005A9C85";
void setup() {
rx_counter = 0; // init counter
Serial.begin(9600);
softSerial.begin(9600);
Serial.print("online...");
}
void loop() {
if (softSerial.available() > 0) {
rx_data[rx_counter] = softSerial.read();
if (rx_counter == 0 && rx_data[0] != STX) {
Serial.println("Out of sync!"); // do not increment rx_counter
} else {
rx_counter++;
}
if (rx_counter >= 14) {
rx_counter = 0; // reset counter
if (rx_data[0] == STX && rx_data[13] == ETX) { // packet starts with STX and ends with ETX
byte calc_checksum = 0;
for (int i = 0; i < 6; i++) { // data with checksum
calc_checksum ^= ascii_char_to_num(rx_data[i*2+1]) * 16 + ascii_char_to_num(rx_data[i*2+2]);
}
if (calc_checksum == 0) {
Serial.print("ID: ");
for (int i = 1; i <= 10; i++) {
Serial.write(rx_data[i]);
//
if (strncmp(rx_data+1, Card1ID, 10) == 0 || strncmp(rx_data+1, Card2ID, 10) == 0) {
Serial.print("ON!");
} else {
Serial.print("OFF!");
}
//
}
Serial.println();
} else {
Serial.println("Checksum ERROR!");
}
} else {
Serial.println("Incorrect packet!");
}
}
}
}
byte ascii_char_to_num(byte a) {
a -= '0'; // 0..9
if (a > 9) a -= 7; // A..F
return a;
}
if (strncmp(rx_data+1, Card1ID, 10) == 0 || strncmp(rx_data+1, Card2ID, 10) == 0) {
What is the data type of rx_data ?
What is the data type of Card1ID and Card2ID ?
int rx_counter;
byte rx_data[14]; // 1+10+2+1
char Card1ID[] = "39005A9C88";
char Card2ID[] = "39005A9C85";
It apparently doesn't like you using "unsigned char" in place of "char" when working with characters. If you change rx_data[] from 'byte' to 'char' it works fine.