ATMega328p resetting itself

I have a breadboard ATMeg328p chip, configured with the 16Mhz external crystal and the UNO bootloader, controlling a MOSFET switch to spin a disco ball. I’m using PWM on pin ~3 via the digitalWrite function.

I really like the set-up, the light bouncing around the room, and the project in general but the set-up just stops working after approximately 10 minutes. If I switch it off and then back on, it continues working again until another 10 minutes have elapsed.

I’ve checked the voltage to the chip and it is steady at 5.04 V so I don’t see it as a brown-out situation. Also, I looked at the forums and saw some mention of serial.print causing issues with buffers. I went back through the sketch and removed all the serial.print statements I had in there. They were definately affecting the timing of the PWM digitalWrites but removing them, and changing the duration of the digitalWrite functions, hasn’t stopped the problem.

The code is very simple.

#define LED 7
int fr = 8000;  // frequency - du
int du = 2000;  
int j = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(3, OUTPUT);
  pinMode(LED, OUTPUT);
}

void loop()
{
    if (j<2)
    {
    digitalWrite(LED, HIGH);
    Serial.print("Wrote HIGH ");
    Serial.print(j);
    Serial.println("th time");
    delay(500);
    digitalWrite(LED, LOW);
    delay(500);
    }
    j++;
    
    digitalWrite(3, HIGH);
    delayMicroseconds(du);
    digitalWrite(3, LOW);
    delayMicroseconds(fr);

}

It seems that a buffer is overflowing somewhere. The chip just resets itself continuously if I don’t switch it off and back on again, but I’m not aware of filling any buffers using the code I’m using.

Any suggestions? :astonished:

thanks
Sarge

Use only this in the loop:

    digitalWrite(3, HIGH);
    delayMicroseconds(du);
    digitalWrite(3, LOW);
    delayMicroseconds(fr);

The value of ‘j’ is incremented, so it will turn into a negative number. When you test the negative number for “if (j<2)”, that will be true of course for a negative number.

Integer is -32,768 to 32,767. So things get hairy after 32767. You have about 10ms for the loop plus extra for the function calls. That results into something between 5 to 7 minutes. Are you sure it is 10 minutes ?
http://arduino.cc/en/Reference/int

An breadboard Arduino going to get stuck could also mean bad contacts of the breadboard, or the crystal with long wires, or forgetting a (few) decoupling capacitors at 5V and GND, or a 7805 without capacitors (it will oscillate), and so on.

thanks Peter_n

I do believe you have found the problem. I'll check it this evening to confirm.

Yes, it is possible that the reset time is more like 5.5 minutes than the ~ 10 minutes I originally mentioned.

My PWM timing was totally thrown off by some serial.print statements in the loop during initial implementations. As I mentioned, I took those out and the reset period has indeed gone down to around 6 minutes.

learning experience for me, I guess :-[

sarge: learning experience for me, I guess

That is what this forum is for :)