I need your support on a project. I developed a project with Arduino nano. When the RFID card is read, the current date and time information in the RTC module is saved on the SD card. The system is powered by 2pcs 18650 batteries. Then I added a button to the arduino and when the button was pressed, I showed the log records in the SD card on the serial monitor. So far everything works very well. What I want to do is, I want the button that I use to show the information in the SD card on the serial monitor only work when the arduino is connected to the PC via usb. So if the arduino is not connected to the pc via usb, the button will be useless. This is the reason why I want it, I will automatically delete the log records on the SD card after showing them on the serial monitor. Therefore, if the arduino is not connected to the pc and the button is accidentally pressed, the log records will not be deleted.
I thought I could find a solution by reading the VIN pin analog. Because when the battery is connected to the VIN pin and the arduino is connected to the PC with the USB cable at the same time, the VIN pin goes low. However, I think this solution might have some issues. For example, when the battery voltage decreases, can the VIN pin read low and cause the button to activate?
I think we can find a solution to this with a little brainstorming. Thank you in advance for your support.
The nano I use has an ATMEGA328p processor and also it has a mini usb connector. I tried to deactivate the button when the usb is not connected using the Serial.available() function, but it didn't work. Are you talking about creating a virtual port with the SoftwareSerial library?
The arduino I use communicates via the COM3 port when connected to the PC. Can I detect COM3 port with Arduino? For example, if COM3 is active, the button should also be active.
Thinking about it the overall approach is flawed. If you connect a serial port and the monitor is set on the wrong baud rate you will not get your log and it will be deleted.
A much better way is to ask in the serial monitor window, do you want to delete the log. And only delete if you receive a YES or Y back.
No.
Some Arduinos create a virtual serial port for USB communications, like the Micro and Leonardo as well as all the modern 3V3 Arduinos. When the USB is removed then the Serial call will return false.
Just like this bit of code is waiting for one to be established before continuing :-
After even more thinking I would recommend not deleting the log at all but simply changing the file names. The logs would then only be accessible to someone with access to the physical system.
Interesting note in some Scandinavian countries it is illegal to log data about someone doing what they are authorised to do. So you can only log data about someone trying to access a door they are not authorised to use or someone using a card that is not on the current authorised list. Like some ex employee.