Standalone 328 runs examples code but not mine

So today I come back to the Arduino after not touching it for 2 months because of an annoying issue.

In a nutshell, every sketch that I write runs perfectly on the actual USB Duemilanove, but fails to run on a breadboard and 12V AC adapter. Here is the weird part: the prepackaged examples, like “BlinkWithoutDelay” and “Knob” (from the servo library) work perfectly on the breadboard. However, when I try to run my own code on the breadboard, it doesn’t work. :’(

At first I thought it was my code, so I downgraded to a much simpler sketch: turn a servo every 5 seconds. Again, it works perfectly on the Duemilanove. This is the unmodified sketch (please note all the commented lines):

#include <Servo.h>

Servo myservo;
const int SERVO_PIN = 9;

unsigned long nTimeLastChanged = 0; //(milliseconds) so that only a certain number of changes are made each time
boolean bLeftOrRight = 0;

void setup(){
//    Serial.begin(57600);
    /* Random number seeding */
//    randomSeed(analogRead(0));
//    myservo.write(45 + 135 * (random(0, 1000) / 1000.0));
//    delay(500);
    myservo.write(10 + !bLeftOrRight * 170);


void loop(){
    /* If a certain time has passed then turn the servo over. */
    if(abs(millis() - nTimeLastChanged) > 5000){
        myservo.write(10 + bLeftOrRight * 170);
        bLeftOrRight = !bLeftOrRight;
        nTimeLastChanged = millis();
//    Serial.println((1000 * 60 * 10));

This is what happens when I turn on power with this sketch on a breadboard: instead of going “turn-5sec-turn-5sec-turn”, it will go “5-sec-turn-turn-8sec-turn-1sec-turn” with the 8 and 1 being random. Plus, after about 30 seconds the servo never turns again until the power is cut.

I am pretty darn sure that I have everything hooked up correctly since the “Knob” example worked well on the breadboard. I just can’t get my own code to work.

You posted in Hardware / Troubleshooting; do you suspect the problem is hardware related?

(I do)

I figure it has to be, since all I do is transfer the chip over from the Duemilanove to the breadboard and get issues.

In that case, you’ll need to provide more information. A few questions that will hopefully get you in the right state of mind…

What schematic did you use when constructing the breadboard circuit?

Are the servos powered directly from the 12V adapter?

Have you correctly wired the processor’s reset pin?

Is the processor powered (through a voltage regulator) by the 12V adapter?

Is anything hot to the touch?

Sidebar: Remove the call to “abs”; it isn’t necessary.

Here is my setup. I tried to use Fritzing to create a nice schematic, but it would crash whenever I tried to save for the fourth time, and when I reopen it, it says the save file is corrupt. On OS X AND Windows.

I think this is easier. In the picture you can see I have power to the LED and that the 12V is being fed into the right side of the 7805.

3 things, what is going on with the 10k reset circuit ?. It looks like its connected to pins 1 and 2 on the chip.

Secondly its a good idea to keep the oscillator circuit leads as short as possible. With a bit of pin adjusting on the resonator, its usually possible to jiggle it so its connected to the oscillator pins and the ground connection next to them without leads at all. (put it in at an angle so the 2 outer pins line up with the pins on the chip and bend the centre one to reach the gnd connection next door).

Third, some decoupling capacitors on the power lines is a really good idea.

Scrub the third, it looks like you have some around the regulator…

The 10K resistor is just bent in a funny way. It's only touching pin 1.

Funny, I never thought you could adjust the resonator with such short leads like that, but I was able to get that middle pin stuck in the ground row while I got the other two on the microcontroller pins 9 and 10. Did a continuity test, and confirmed all the leads were touching.

But it's still behaving the same, erratic way.

Could the voltage regulator be oscillating? Those caps around the regulator's input and output, look a little small to me, have you checked their value against normal recommended values?


OK, now I am REALLY confused. I tried putting the ATmega back into the Duemilanove like this: (the blue thing is an hourglass made of ooze.)

Still running the same sketch. Here are the times that the motor turned:

12:15:47 12:16:02 (difference: 15 sec) 12:16:17 (difference: 15 sec) 12:16:27 (difference: 10 sec) 12:16:32 (difference: 5 sec) 12:16:42 (difference: 10 sec) 12:16:47 (difference: 5 sec) 12:16:57 (difference: 10 sec) 12:17:12 (difference: 15 sec) 12:17:22 (difference: 10 sec) 12:17:32 (difference: 10 sec) 12:17:42 (difference: 10 sec)

As you can see it's not constant for some reason. I even tried using another ATmega chip entirely, and it was still not constant. I think this is about as simple as it can get, isn't it? What is wrong here?

In your Sketch, does changing all the "170" to "160" make a difference?

In your Sketch, does changing all the “170” to “160” make a difference?

I changed both occurances to 160 and got new times:

12:51:28 (difference: 16 sec)
12:51:38 (difference: 10 sec)
12:51:49 (difference: 11 sec)
12:51:54 (difference: 5 sec)
12:51:59 (difference: 5 sec)
12:52:02 (difference: 3 sec)
12:52:12 (difference: 10 sec)
12:52:18 (difference: 6 sec)
12:52:28 (difference: 10 sec)
12:52:32 (difference: 4 sec)
12:52:47 (difference: 15 sec)
12:52:53 (difference: 6 sec)
12:53:02 (difference: 9 sec)
12:53:12 (difference: 10 sec)
12:53:18 (difference: 6 sec)
12:53:32 (difference: 14 sec)

They are still pretty close to multiples of 5.

OK, new information:

I changed the "Serial.begin" line to:


and I inserted this right after the "nTimeLastChanged = millis();" line

Serial.println(bLeftOrRight, DEC);

Here are the times I measured:

12:59:11 12:59:16 (difference: 5 sec) 12:59:21 (difference: 5 sec) 12:59:26 (difference: 5 sec) 12:59:31 (difference: 5 sec) 12:59:36 (difference: 5 sec) 12:59:41 (difference: 5 sec) 12:59:46 (difference: 5 sec)

So it seems that my program only works on the Duemilanove with the Serial connection enabled. (but the actual "serial monitor" does not have to be open) This is the exact same behavior that occurred with my more complicated sketch. What is going on?

I only now have skimmed through this thread.
My first idea was that the processor is resetting in some cases when the motor is started after 5,10,15 seconds… Due to unfiltered spikes (you have hardlyany decoupling) or voltage drop (you have hardly any buffer on the breadbord, though you have 47uF on the Duomille, which however is not so much with motors … 470U would be much better)

Can you make sure, that this (I mean the reset) is not happening?

The print is causing a delay of around 3ms… Though I see no reason whythis is relevant… Can you try to exchange the print with an delay((3)?

Just BTW: There is a server.write in setup() which will immediately be overridden be a counter-instruction during the first loop()

Did you remove the abs() call as already suggested?

Because of the way the abs() function is implemented, avoid using other functions inside the brackets, it may lead to incorrect results.

abs(a++);   // avoid this - yields incorrect results

a++;          // use this instead - abs(a);       // keep other math outside the function

Aren't you supposed to change the FUSE settings if you're using a resonator instead of a crystal oscillator?

Not really... What change do you suggest?

OP here.

2 things: 1) Removing the abs() call caused all the errors to disappear. Now it works without a call to "Serial.begin()". But I'm still wondering why the abs() function worked with Serial.begin() with no errors before.

2) The Duemilanove is working perfectly now, but the breadboarded version still stops responding after 30 seconds. I let it just sit there connected to power for 5 minutes and decided to touch the voltage regulator. It was so hot that I had to let go immediately. But I'm only using a switched 12V supply! Is that too high?

This is a picture of the regulator:

I went on a search for the datasheet and found it here:

I guess I either need to lower the voltage or get a different type of voltage regulator. (and maybe more decoupling capacitors while I'm at it.) Any suggestions? (I don't want to use a heatsink)

Update: yeah. It was the 12V adapter. I switched to a 9V adapter and now it functions for a tiny bit longer. Still gets hot.

Update 2: the 9V adapter was not regulated. It was 12V on my circuits "idle" and 8.5 when the motor turned. This made it almost the same as my regulated 12V supply, which explains why it still got hot.