[SOLVED] Arduino gets hang/stuck/frezz? Code or a hardware issue?

Hello, I’ll explain what the code below does or drives.
I have an Arduino Pro Mini, connected to a 3.7v 950mAh battery (protected) and a 5v 400mAh solar panel.
I have an SD card module connected directly to the Arduino.

There is an RTC, Humidity and Temp sensor, Barometer and a simple light sensor thing connected to a MOSFET.
The MOSFET activates and deactivates every 2 minutes or whenever the loop runs if the Arduino is not sleeping.
Basically, records data to the SD card. Everything seems to be working fine but, I don’t know what happens that the software or hardware causes the Arduino to Freeze or hang and it will remind like that until I press reset button.

How the outside looks when the micro controller is hang/frozen: The Arduino gets ‘stuck’ with the MOSFET on HIGH, allowing all the modules to be on, thus drawing power. Is it my sleep code? I don’t think it is a MOSFET problem because otherwise the serial data port also stops sending to my pc.

I took a lot of the code from one of Nick’s projects which I understand to some extent. MOSFET used is a TIP31 and the reason for it is that not only do not know which to use, but it the only one I have, literally.
Any recommendations for a MOSFET that fits 0 to <5v? Voltage ranges from 3.3v to 4.2v.

I had to include the code as a file because I exceed the maximum allowed length…

Anyone? Ideas?

A TIP31 is a NPN transistor, not a MOSFET. How do you have it connected to the Arduino?

SurferTim: A TIP31 is a NPN transistor, not a MOSFET. How do you have it connected to the Arduino?

Collector to +3.7 to 5v Base to a pinout form the atmega328. Goes HIGH to 'activate' the tip31 thus putting power to the sensors. Emitter the sensors + pins. As I mention, it is literally the only transistor that I have right now. Which MOSFET can you recommend me to buy for the following setup. Everything runs of a 3.7v LiPo battery @ 1000mAh.

That sounds very wrong. You need a base resistor, you need to connect the emitter to ground and the collector to the load. The other end of the load to +ve.

Found the problem. It was not the code or the MOSFET. I did not had pull-ups/pull-down resistors in the I2C channels. That was all. I think the atmega was getting confused on which module to communicate with since none of them were getting a signal down from the pull resistors to read the next module. Thanks to all. Also I got a proper MOSFET now.

I did not had pull-ups/pull-down resistors in the I2C channels.

This is surprising as you never mentioned you were using I2C anywhere. If you had then certainly you would have been reminded about this.