Arduino pro micro - not working after turning on and off

Hello,

I have got arduino pro mico (leonardo) clone 5V version 16 mhz (i have got 20 of them from different batches, also i do own a standard "big" oryginal Leonardo.
There is Atmega 32u4 on them.

Ihave build a RFID reader with it.

RFID RC522 connected over SPI, pins 9,10,15,16,GND,VCC just standard configuration for this module.
Piezo beeper with generator build in connected to PIN 8 and gnd

I use software serial, pins 2TX, 3RX, baud 9600, HC-06 bluetooth module connected to that serial interface.

I'm also using keyboard.h to turn the USB controleer in to the HID keyboard to print out the serial number on computer .

I'm using library for RC522 to dump the UID of NFC tag and then using Keyboard.print, and myserial.print sending that UID to the keyboard and serial (to the Bluetooth module)

Also im doing BEEP :slight_smile: setting the PIN8 for 200 milisec high, there is piezo beeper with build in generator, so i only need to power it up with 5V to get sound.

Ewerything was working well 3 weeks ago, and on some readers i have programmed in that time it still works, Now using the same sketch I do expierience the problem:

When i upload the sketch weverything works well.

When i unplug the device form USB (power) and i plug it again there is a problem:

The keyboard part (printing to HID keayboard) works fine.

The BEEP part works... but the delay 200ms is ignored, it beeps over a second.

And the softwareserial part does not work , it sends some trash to serial instead of proper UID number, exactly 80 80 80 80 80 80 80 80 80 when I'm checking on serial monitor over bluetooth.

Same problem occures on the devices that do work with firmware flashed 3 weeks ago (the same sketch, no changes), and when i upload the sketch now it has problems as described.

I have 20 boards and all have the same syptoms. Tested on clean installed OS with clean installed versions of arduino IDE, tested with different combinations of versions of library es and IDE....

So problem is not a hardware because the hardware works fine... until flashing the sketch and turning off power.

Does anyone have similar problem ?

#include <SPI.h>
#include <MFRC522.h>
#include <Keyboard.h>
#include <SoftwareSerial.h>

#define RST_PIN         9                                          
#define SS_PIN          10

       MFRC522 mfrc522(SS_PIN, RST_PIN);                                        
       SoftwareSerial btSerial(3, 2);  

int incomingByte = 0;
int beep = 8;

void setup() {
// Serial.begin(9600);                                                            
//  while (!Serial);                                                                 
   btSerial.begin(9600);                                                             
   pinMode( beep, OUTPUT);                                                       
   SPI.begin();                                                                    
   mfrc522.PCD_Init();                                                            
}

void loop() {

   if ( ! mfrc522.PICC_IsNewCardPresent())                                         
   if ( ! mfrc522.PICC_ReadCardSerial())   
                                          
          Push_dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size); 
          Keyboard_print_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);      
             
      mfrc522.PICC_HaltA();                                 
      mfrc522.PCD_StopCrypto1(); 

    digitalWrite(beep, HIGH);
    delay(200);
    digitalWrite(beep, LOW);
}

void Keyboard_print_byte_array(byte *buffer, byte bufferSize) {                      
  for (byte i = 0; i < bufferSize; i++) {
       Keyboard.print(buffer[i] < 0x10 ? "0" : "");
       Keyboard.print(buffer[i], HEX);
   }
}

char Header[4] = {0x55, 0xFF, 0xFF, 0xFF};
char Endpoint[2] = {0xFF, 0xAA};

void Push_dump_byte_array(byte *buffer, byte bufferSize) {                                                                                                          
     for(int i = 0; i < 4; ++i){
        btSerial.print(Header[i]);
     }  
     for(byte i = 0; i < bufferSize; i++) {
        btSerial.print((char)buffer[i]);
     }    
     for(int i = 0; i < 2; ++i){
        btSerial.print(Endpoint[i]);
     }
}

Did you update your IDE version recently?

You have posted code without using code tags. This creates certain problems and obstacles for other forum members. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click.
If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button. Code tags can also be inserted manually in the forum text using the [code] and [/code] metatags.

When you are finished that, please this post:

How to use this forum - please read.

When you say, "old firmware" do you mean your previous code, or actual Arduino firmware?

Also, I'm curious about why you're using SoftwareSerial when the Micro has an extra hardware serial port.

Do you have another board you can try?

Ok, one last check.

I have removed the code for Keyboard HID, only the routine for Serial1 (bluetooth module) left.
The problem is as before. So it is connected with handling Serial interface on atmega 32u4.

I have tested the full code from the first post and the short as below on atmel 328p arduino pro mini, and it works on the same side with the same libraries.
So if on a different microcontroler it works, and the only problematic boards are those with atmega 32u4 than i think that something is broken in IDE connected with handling 32u4.

I have tested the code on 26 boards from different manufacturers.

I was testing on software serial, and on serial1, both setups have the same problem.

As i said before, boards were working fine (those flashed over a week ago) and one of them still works, problem appears when i reflash them now with the same sketch. And the same code flashed to atmega 328p board works without problem using the same developing environment .

#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN         9
#define SS_PIN          10
        MFRC522 mfrc522(SS_PIN, RST_PIN);
        
void setup() {
  Serial1.begin(9600);
  SPI.begin();
    mfrc522.PCD_Init();
}

void loop() {
    if ( ! mfrc522.PICC_IsNewCardPresent())
        return;
    if ( ! mfrc522.PICC_ReadCardSerial())
        return;  
        
           Bluetooth_HID(mfrc522.uid.uidByte, mfrc522.uid.size);           
          
       mfrc522.PICC_HaltA();
       mfrc522.PCD_StopCrypto1(); 
}

void Bluetooth_HID(byte *buffer, byte bufferSize) { 
   for (byte i = 0; i < bufferSize; i++) {
        Serial1.print(buffer[i] < 0x10 ? "0" : "");
        Serial1.print(buffer[i], HEX);
    }
}