Hello,
This is my first time posting on here , so I have a problem. Let me explain the situation.
I started an project for reading rfid 134.2 khz tags, which was working etc. Now I had the code working just like my arduino Mega 2560. I had tested it on some tags I brought with me, now I wanted to test it on real animals. So I got my arduino ready to go (connected an 20x4 lcd display) etc.
Here come's the problem, which is really weird. It was still working after connecting the display. Now I grabbed an 9V block battery which I checked worked, and check polarity. Connected - to Gnd and + to Vin on the arduino board, guess what happened: correct nothing. :o now I disconnected it quickly making sure not to touch anything with the wires. And wired it as I did before to my computer via the USB wire. Still not working!!! Checked the voltages on my arduino which all were normal. Software runs, Serial monitor outputs the correct stuff except for my 134.2 khz rfid reader.
I used Serial2 for that and the little board works on 3.3V and I didn't change any connections at the time of testing and adding the battery. But now I don't recieve anything on my Serial monitor from Serial2. Tested Serial 1 and 3 too still no signs of working. I used the thing with and without Serial1.available() > 0 still nothing!!
Please help me I'm really confused now!
Short Code:
void setup() {
 Serial.begin(9600);
 Serial2.begin(9600);
 Serial2.write("oxAA");
}
void loop() {
 if (Serial2.available() > 0) { // check if reader serial is avaible
  while (Serial2.available() > 0) {
   int message = Serial2.read();
   Serial.print(message, HEX);
   Serial.print(",");
}}}
Normal code wich worked the last time before the battery (sorry for the inefficientie!) :
int pos = 0;Â Â Â Â // position in 8 byte long string
String hexcode;Â Â // string for complete hex code
String kar1;Â Â Â Â // character 1...7 in 8 byte long string in hex
String kar2;
String kar3;
String kar4;
String kar5;
String kar6;
String kar7;
String countrycode; // country code string
String animalnumberfinal;
void setup() {
 Serial.begin(9600);  // Serial monitor start
 Serial1.begin(9600);  // 16 & 17 start for serial reader
 Serial.println("Start");
 Serial1.write("oxAA");
 if (Serial1.available() > 0) { // check if reader serial is avaible
  //Serial1.write("oxAA"); // cmd for reader to start reading (0xBB = stop)
  Serial.println("serial1 available");
 } else {
  Serial.println("Serial1 not availble!!");
 }
}
void loop() {
 if (Serial1.available() > 0) { // check if reader serial is avaible
  while (Serial1.available() > 0) {
   int message = Serial1.read();
   Serial.print(message, HEX);
   Serial.print(",");
   pos = pos + 1;    // every cycle ads pos
   hexcode = hexcode + String (message, HEX) + ",";
   if (pos == 1) {            // every byte recieved except for 8th
    kar1 = kar1 + String(message, HEX);
   }
   if (pos == 2) {
    kar2 = kar2 + String(message, HEX);
   }
   if (pos == 3) {
    kar3 = kar3 + String(message, HEX);
   }
   if (pos == 4) {
    kar4 = kar4 + String(message, HEX);
   }
   if (pos == 5) {
    kar5 = kar5 + String(message, HEX);
   }
   if (pos == 6) {
    kar6 = kar6 + String(message, HEX);
   }
   if (pos == 7) {
    kar7 = kar7 + String(message, HEX);
   }
  }
 }
 if (pos == 8) {     // when last byte recieved
  Serial.println();
  Serial.println(hexcode);
  pos = 0;       // reset pos
  hexcode = "";
  countrycode = kar2 + kar1;   // first 2 characters invert
 Â
  // start of hex to dec decoding
  unsigned int hexToDec(String countrycode);
  unsigned int countrydecValue = 0;
  int nextInt;
  for (int i = 0; i < countrycode.length(); i++) {
   nextInt = int(countrycode.charAt(i));
   if (nextInt >= 48 && nextInt <= 57) nextInt = map(nextInt, 48, 57, 0, 9);
   if (nextInt >= 65 && nextInt <= 70) nextInt = map(nextInt, 65, 70, 10, 15);
   if (nextInt >= 97 && nextInt <= 102) nextInt = map(nextInt, 97, 102, 10, 15);
   nextInt = constrain(nextInt, 0, 15);
   countrydecValue = (countrydecValue * 16) + nextInt;
  }
  // end of hex to dec decoding
 Â
  Serial.println();
  Serial.print("Country code in decimal: ");
  Serial.println(countrydecValue);
  // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Â
  // start animal nr decoding
 Â
  animalnumberfinal = kar7 + kar6 + kar5 + kar4 + kar3;
  unsigned long hexToDec1(String animalnumberfinal);
  unsigned long animaldecValue = 0;
  int nextInt3;
  for (int i = 0; i < animalnumberfinal.length(); i++) {
   nextInt3 = int(animalnumberfinal.charAt(i));
   if (nextInt3 >= 48 && nextInt3 <= 57) nextInt3 = map(nextInt3, 48, 57, 0, 9);
   if (nextInt3 >= 65 && nextInt3 <= 70) nextInt3 = map(nextInt3, 65, 70, 10, 15);
   if (nextInt3 >= 97 && nextInt3 <= 102) nextInt3 = map(nextInt3, 97, 102, 10, 15);
   nextInt3 = constrain(nextInt3, 0, 15);
   animaldecValue = (animaldecValue * 16) + nextInt3;
  }Â
  Serial.println(animalnumberfinal);
  Serial.println(animaldecValue);
  }
 Â
}
Hope we can come to an conclusion together!
Sincerely,
Johannes