Arduino not working correctly after correctly powering it via Vin w/ 9V battery

Hello,

This is my first time posting on here :confused:, 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

It is possible that I might have messed up gnd with pin 13 or aref which isnt good I guess.

This looks odd:

Serial2.write("oxAA");

Maybe is should be 0xAA?

Further
1)
9V battery blocks are quite useless
2)
Messing up pins can be a receipe for permanent damage; though you said that it's still basically working.

Yes oxAA is for starting the module and 0xBB is for stopping it from reading.

  1. I know just wanted to use it 1 time for testing quick.
  2. Okay already thaught so but still weird that serial sometimes works and sometimes doesn't.

ty for you comment

Bought a new arduino mega and it all works now so idk but It's fixed ty all!