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.attach(SERVO_PIN);
// 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.
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.
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.
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?
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?
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()
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.
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?
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.