while(Serial.available()){
int readByte = Serial.read(); //read next available byte
...
}
You are probably going to read one byte here ... unless there was a previous delay, which you had in setup.
while(Serial.available()){
int readByte = Serial.read(); //read next available byte
...
}
You are probably going to read one byte here ... unless there was a previous delay, which you had in setup.
PaulS:
if (rfid_val == 1|rfid_val==2){This is probably NOT doing what you think it is. Big difference between | and ||.
Thanks, dumb mistake! Though it didn't solve my problem yet, is it possible there is some sort of while loop it doesn't get out from or whatever?
Maybe I need to say it only reads an rfid tag right after having it reset, when the program runs it just doesn't, even when it hasn't read something yet.
Niels,
Maybe I need to say it only reads an rfid tag right after having it reset, when the program runs it just doesn't, even when it hasn't read something yet.
Then, you need to add more Serial.print() statements, to find out why the function is not being called.
If i put different Serial.print() command it only seems to run the void .... sequences in my program.
If i put different Serial.print() command it only seems to run the void .... sequences in my program.
There are no "void ... sequences" in your program. There are functions, some of which have a void return type.
Post your code!
This is the code i'm using.
#include <SPI.h>
#include <Ethernet.h>
IPAddress ip( );
IPAddress gateway( );
IPAddress subnet( );
byte mac[] = { };
IPAddress server( );
EthernetClient client;
int totalCount = 0;
int loopCount = 0;
int connectLed=5;
int buttonState=0;
int connectedToServer=6;
/////////////////RFID/////////////////
int RFIDResetPin = 13;
int rfid_val;
//Register your RFID tags here
char tag1[13] = "4500530E9F87";
char tag2[13] = "450052E8E51A";
int led1=2;
int led2=3;
void setup() {
Serial.begin(9600);
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
Ethernet.begin(mac, ip, gateway, gateway, subnet);
delay(2000);
Serial.println("Ready");
/////////////////RFID/////////////////
pinMode(RFIDResetPin, OUTPUT);
digitalWrite(RFIDResetPin, HIGH);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(connectedToServer, OUTPUT);
}
void loop()
{
////////////////Ethernet////////
Serial.print("RFID TAG ");
Serial.println(rfid_val);
if (rfid_val == 1||rfid_val==2){
digitalWrite(connectLed, HIGH);
Serial.print("If statement works");
if(!getPage(server,"/add_db.php")) Serial.print("Fail ");
else Serial.print("Pass ");
Serial.println(totalCount,DEC);
}
else {
// turn LED off:
digitalWrite(connectLed, LOW);
}
/////////////////RFID/////////////////
char tagString[13];
int index = 0;
boolean reading = false;
while(Serial.available()){
Serial.println("Serial.available");
int readByte = Serial.read(); //read next available byte
if(readByte == 2) reading = true; //begining of tag
if(readByte == 3) reading = false; //end of tag
Serial.print(readByte);
if(reading && readByte != 2 && readByte != 10 && readByte != 13){
//store the tag
tagString[index] = readByte;
Serial.println("(reading && readByte != 2 && readByte != 10 && readByte != 13)");
index ++;
Serial.print("index");Serial.println(index);
}
}
checkTag(tagString); //Check if it is a match
clearTag(tagString); //Clear the char of all value
resetReader(); //eset the RFID reader
}
byte getPage(IPAddress ipBuf,char *page)
{
int inChar;
char outBuf[64];
Serial.print("connecting...");
if(client.connect(ipBuf,80))
{
Serial.println("connected");
sprintf(outBuf,"GET /add_db.php?Rfid=%u HTTP/1.0\r\nHost: www.**************.nl\r\n\r\n",rfid_val);
client.write(outBuf);
}
else
{
Serial.println("failed");
return 0;
}
while(client.connected())
{
while(client.available())
{
inChar = client.read();
Serial.write(inChar);
}
digitalWrite(connectedToServer, HIGH);
delay(500);
digitalWrite(connectedToServer, LOW);
}
Serial.println();
Serial.println("disconnecting.");
client.stop();
return 1;
}
/////////////////RFID/////////////////
void checkTag(char tag[]){
Serial.println("begin of void checkTag");
///////////////////////////////////
//Check the read tag against known tags
///////////////////////////////////
if(strlen(tag) == 0){
digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
rfid_val=0;
//return; //empty, no need to contunue
}
if(compareTag(tag, tag1)){ // if matched tag1, do this
rfid_val=1;
digitalWrite(led1, HIGH);
delay(2000);
}
if(compareTag(tag, tag2)){ //if matched tag2, do this
rfid_val=2;
digitalWrite(led1, HIGH);
delay(2000);
}
Serial.println("end of void checkTag");
}
//void lightLED(int pin){
/////////////////////////////////////
////Turn on LED on pin "pin" for 250ms
/////////////////////////////////////
// Serial.println(pin);
//
// digitalWrite(pin, HIGH);
//
//}
void resetReader(){
///////////////////////////////////
//Reset the RFID reader to read again.
///////////////////////////////////
digitalWrite(RFIDResetPin, LOW);
digitalWrite(RFIDResetPin, HIGH);
delay(200);
Serial.println("End of void resetReader");
}
void clearTag(char one[]){
///////////////////////////////////
//clear the char array by filling with null - ASCII 0
//Will think same tag has been read otherwise
///////////////////////////////////
for(int i = 0; i < strlen(one); i++){
one[i] = 0;
Serial.print("void clearTag i value ");
Serial.println(i);
}
Serial.println("clearTag ");
Serial.println();
}
boolean compareTag(char one[], char two[]){
///////////////////////////////////
//compare two value to see if same,
//strcmp not working 100% so we do this
///////////////////////////////////
if(strlen(one) == 0) return false; //empty
for(int i = 0; i < 12; i++){
if(one[i] != two[i]) return false;
Serial.print("boolean compareTag i value ");
Serial.println(i);
}
return true; //no mismatches
}
Thanks,
Niels
How is the RFID reader really connected to the Arduino? You seem to be using Serial to read from/write to the Serial Monitor AND to read from the RFID reader. You can't do that.
PaulS:
How is the RFID reader really connected to the Arduino? You seem to be using Serial to read from/write to the Serial Monitor AND to read from the RFID reader. You can't do that.
Well, that rather strange because I did combine two programs, an ethernet part together with a rfid part.
This is the rfid code only, which works. This code continuously prints the a 0, a 1 or a 2.
0 for no rfid
1 for rfid 1
and
2 for rfid 2
int RFIDResetPin = 13;
int rfid_val;
//Register your RFID tags here
char tag1[13] = "4500530E9F87";
char tag2[13] = "450052E8E51A";
int led1=2;
int led2=3;
void setup(){
Serial.begin(9600);
pinMode(RFIDResetPin, OUTPUT);
digitalWrite(RFIDResetPin, HIGH);
//ONLY NEEDED IF CONTROLING THESE PINS - EG. LEDs
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
}
void loop(){
char tagString[13];
int index = 0;
boolean reading = false;
while(Serial.available()){
int readByte = Serial.read(); //read next available byte
if(readByte == 2) reading = true; //begining of tag
if(readByte == 3) reading = false; //end of tag
if(reading && readByte != 2 && readByte != 10 && readByte != 13){
//store the tag
tagString[index] = readByte;
index ++;
}
}
Serial.println(rfid_val);
checkTag(tagString); //Check if it is a match
clearTag(tagString); //Clear the char of all value
resetReader(); //eset the RFID reader
}
void checkTag(char tag[]){
///////////////////////////////////
//Check the read tag against known tags
///////////////////////////////////
if(strlen(tag) == 0){
digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
rfid_val=0;
return; //empty, no need to contunue
}
if(compareTag(tag, tag1)){ // if matched tag1, do this
rfid_val=1;
digitalWrite(led1, HIGH);
delay(2000);
}
if(compareTag(tag, tag2)){ //if matched tag2, do this
rfid_val=2;
digitalWrite(led1, HIGH);
delay(2000);
}
}
//void lightLED(int pin){
/////////////////////////////////////
////Turn on LED on pin "pin" for 250ms
/////////////////////////////////////
// Serial.println(pin);
//
// digitalWrite(pin, HIGH);
//
//}
void resetReader(){
///////////////////////////////////
//Reset the RFID reader to read again.
///////////////////////////////////
digitalWrite(RFIDResetPin, LOW);
digitalWrite(RFIDResetPin, HIGH);
delay(200);
}
void clearTag(char one[]){
///////////////////////////////////
//clear the char array by filling with null - ASCII 0
//Will think same tag has been read otherwise
///////////////////////////////////
for(int i = 0; i < strlen(one); i++){
one[i] = 0;
}
}
boolean compareTag(char one[], char two[]){
///////////////////////////////////
//compare two value to see if same,
//strcmp not working 100% so we do this
///////////////////////////////////
if(strlen(one) == 0) return false; //empty
for(int i = 0; i < 12; i++){
if(one[i] != two[i]) return false;
}
return true; //no mismatches
}
Why not use SoftwareSerial to read the RFID, using two other pins?
He can put the RFID reader on the Rx pin, and leave the Tx pin free for debugging.
Sorry guys, I can't seem to get grip on my problem. I tried a new sketch which makes use of the SoftwareSerial library, which doesn't work at all.
Though, I keep thinking the problem of my combined sketch isn't to big, it looks like it only run's the void functions and ignores all the rest.
I tried a new sketch which makes use of the SoftwareSerial library, which doesn't work at all.
Using something other than 0 point font when you post the code would be good.
it looks like it only run's the void functions and ignores all the rest.
So, use global variables, and make all the functions void. Not that that will help. Debug print statements and code reviews are what you really need.
Oke, i tried to debug my code, which I've already posted in this topic. And I have found a bug, though no idea at all of how solve it.
It seems to be within my; void setup(){..}
which contains the following code.
Serial.begin(9600);
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
Ethernet.begin(mac, ip, gateway, gateway, subnet);
delay(2000);
Serial.println("Ready");
/////////////////RFID/////////////////
pinMode(RFIDResetPin, OUTPUT);
digitalWrite(RFIDResetPin, HIGH);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(connectedToServer, OUTPUT);
}
The problems seems to be with the
Ethernet.begin(mac,ip, gateway, gateway, subnet);
If i comment this line, it works fine. Except from making the connection. Any ideas of how to solve this?
Regards,
Niels
Ethernet.begin(mac, ip, gateway, gateway, subnet);
Where are the definitions/initialisations of those parameters?
dxw00d:
Ethernet.begin(mac, ip, gateway, gateway, subnet);Where are the definitions/initialisations of those parameters?
Those are declared above the setup()
#include <SPI.h>
#include <Ethernet.h>
//Here Ethernet parameters
IPAddress ip(******************);
IPAddress gateway(******************);
IPAddress subnet(******************);
byte mac[] = {****************** };
IPAddress server(******************);
EthernetClient client;
int totalCount = 0;
int loopCount = 0;
int connectLed=5;
int buttonState=0;
int n;
int connectedToServer=6;
/////////////////RFID/////////////////
int RFIDResetPin = 13;
int rfid_val;
//Register your RFID tags here
char tag1[13] = "4500530E9F87";
char tag2[13] = "450052E8E51A";
int led1=2;
int led2=3;
void setup() {
Serial.begin(9600);
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
Ethernet.begin(mac, ip, gateway, gateway, subnet);
delay(2000);
Serial.println("Ready");
/////////////////RFID/////////////////
pinMode(RFIDResetPin, OUTPUT);
digitalWrite(RFIDResetPin, HIGH);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(connectedToServer, OUTPUT);
}
Never mind! Finally got it working! The problem, was as I expected very very silly! I used pin 13 as reset for the RFID, though it is used for the ethernet shield.
![]()
I would like to thank everyone who took the time to help me with my problems! I hope I haven't been a to big pain in the, well whatever!
Thanks guys!
Niels