Hello everyone,
i have a serious problem with my Arduino Mini Pro. I wrote a program that resets the Arduino in the setup() method with the watchdog timer when a button was not pressed.
The analog input pin 0 is connected to that button with a 220 ohm resistor in front to +5V, and with a 330ohm resistor to ground. So if the button is not pressed, analogRead(0) equals 0 and the Arduino resets.
My problem is that now the Arduino somewhy is permanently resetting himself, even if i press the button. Now because of this perma-reset i am unable to re-program the Arduino to remove that software error.
Does anyone has an idea how to solve this problem?
Here's the code i am using:
#include <avr/wdt.h>
void setup() {
wdt_disable();
Serial.begin(9600);
// do some initial stuff here
int val = analogRead(0);
if (val == 0) wdt_enable(WDTO_15MS);
// do some other stuff here
}
PS: I cannot simply replace the Arduino by a new one, it is already soldered into my project.
The analog input pin 0 is connected to that button with a 220 ohm resistor in front to +5V, and with a 330ohm resistor to ground. So if the button is not pressed, analogRead(0) equals 0 and the Arduino resets.
This doesn't make sense. Please post a pencil drawing (not Fritzing) showing the wiring.
Here's a little drawing of it. Forgot to mention the blue LED that is connected between the button/switch and the analog pin/220ohm resistor. The wiring is based on a pull-up resistor wiring.
You might be able to upload it by plugging it while holding RESET button, and then releasing reset at just the right moment for the bootloader to work (the goal being to make sure the user code that enables the WDT never runs).
I thought the bootloader was smart enough to turn off the WDT if it was started from a WDT reset, and not end up in a state where the WDT reset prevents the bootloader from running, though...
Burning bootloader with an ISP programmer will definitely sort it out, in any case.
That gets me out of that deadlock, thanks. But it still does not solve my problem: how can i use the watchdog for reading a value on an analog pin at the beginning of the program if a button is pressed and keep the program from running if it isn't pressed?
What exactly is the behavior you're trying to achieve? You're doing something that doesn't seem normal to me... Is this an xy problem? ( xyproblem.info )
Do you just want the program to not run until a non-0 value is read? while (analogRead(A0)==0){;} will sit there spinning until a non-0 value is measured (you might want to check if it's below a certain value instead, so that a 1 or 2 or similar very low values are treated as 0)
My first problem was that the Arduino Mini Pro has no AREF pin. So i wanted the Arduino to calibrate at the beginning of the scetch to a certain value (in this case of the analog pin 0).
If that button is not pressed, the Arduino can't calibrate, can't start the program and therefore needs to restart or wait for that button press to calibrate. I also can't use an other pin because i don't have any pins left ...
I need this value because i am operating with different voltages. If someone has another solution than the watchdog reset at the beginning and the AREF pin for a reference i am going to try that as well.
Just read the internal bandgap voltage with the ADC and use that to calculate the operating voltage.
Or internal reference as your analog reference voltage.
Or solder a flying lead onto the AREF pin on the chip and use that (the pin on the chip is there, just not broken out to a pin on the board). Be sure to tack the wire down close to where it's soldered with some hot glue or something, otherwise that is a strain point that will fatigue and break from the flexing of the wire during handling.
The_Programmer:
That gets me out of that deadlock, thanks. But it still does not solve my problem: how can i use the watchdog for reading a value on an analog pin at the beginning of the program if a button is pressed and keep the program from running if it isn't pressed?
Yeah - until reply #6 where he explained what he was trying to deal with, I was thinking just do the while thing (it does what he asked for in #4) - but I think the solutions I suggested in #7 are likely to be more robust for his application, and will likely be easier to implement than whatever calibration routine OP has in mind.
DrAzzy:
I thought the bootloader was smart enough to turn off the WDT if it was started from a WDT reset, and not end up in a state where the WDT reset prevents the bootloader from running, though...