Program freezes after a while

giorgisp:
if I use a 5V 2A AC adapter or a 5V usb charger I've got, the microcontroller eventually freezes after some commands. So it's something related to power supply

Maybe if your power supply is not producing very clean, well regulated DC then the Arduino could suffering from spikes or out-of-spec voltages which are provoking logic faults. Presumably you're bypassing the onboard regulator, so whatever your wallwart produces goes direct to the processor, noise and all. The current ratings you mention make me suspect these are intended as battery chargers rather than to directly power digital devices, so they may not be well regulated and smoothed.