Hi Mononofu,
the timing for resetting the Arduino is very critical because the wait time in the bootloader is really short (2s ?). When you don't start avrdude during this time, your sketch will start again.
The right way would be to tell the bootloader via an EEPROM variable, that it shall endlessly wait for a new sketch and not time out and start the existing sketch. This would involve tweaking the bootloader.
Another idea: At the beginning of your sketch, you could check if your sketch has triggered a reset and do reset the Arduino again when no sketch has been uploaded.
This could be implemented using the following variables:
#define RESET_SIGNATURE (0xE79BF1A2UL)
unsigned long loopResetSignature;
int loopResetCounter;
As long as loopResetSignature contains a predefined value and the loop counter is greater than 0, your sketch will immediately reset the ATmega.
The variables shall not be initialized (then they will be placed in the BSS section) and be defined at the very end of your other global variables, so the chance that they are overwrittten by the bootloader is as small as possible. Maybe this doesn't work at all and you have to use the EEPROM instead of RAM.
At the beginning of your sketch in setup():
SoftReset(0); // check if the reset loop is active
Some helper functions:
void StartResetLoop(void)
{
loopResetCounter = 20; // tune this to get an acceptable timing
loopResetSignature = RESET_SIGNATURE;
SoftReset();
}
// keep resetting the ATmega unless we reached the number of reset loops
// should actually exit when a new sketch has been loaded, but
// how can this be detected?
void CheckResetLoop(void)
{
if(loopResetSignature == RESET_SIGNATURE)
{
if(loopResetCounter > 0)
{
loopResetCounter--;
SoftReset();
}
else
{
loopResetCounter = 0;
loopResetSignature = 0;
}
}
}
void SoftReset()
{
// here your normal reset code
...
}
And where you want to enter the reset loop:
[code]StartResetLoop();
According to Atmel documents, resetting the ATmega shall not be done using one of its PIO pins, because the PIO signal will go to tristate immidiately when the reset starts. To reliably resetting the ATmega, the reset signal must be kept below 1V (0.2*Vcc) for at least 2.5us. To be honest, I don't think this is your problem. Normally the WatchDogTimer shall be used for software reset, but this is not supported with the standard Arduino bootloader.
MikeT