Arduino Uno.Simple qquestion.

Hi there!I just wanted to ask you:Why Arduino Uno resets the code automatically when i use loops?I mean that the loop repeats over and over again ,but after some time (for example 40 seconds), the code resets by itself.Could you ,please explain to me why this happens?

It shouldn’t do that.

“The code resets itself”. Why do you think this?

“when i use loops”: Do you mean the function loop(void) or loops as is for or while loops?

If you don’t use"loops" does the problem not happen?

Have you tried to reduce the code to the smallest number of lines that demonstrate this problem? Could you post them?

Is it always 40 seconds exactly or is it a random number of seconds?

What is this program generally trying to do? Blink a light or guide a rocket to the moon?

What resources on the Arduino are you using? Timers, digital inputs or outputs, analog inputs, PWM outputs, …?

Sorry for all the questions, but since last Tuesday my crystal ball has been broken.

I can think of 2 reasons for that to happen. First is that the voltage regulator is dropping out, perhaps because you’re trying to draw too much current from the Arduino. What, if anything, do you have running off the Arduino or it’s regulator(s) ? A schematic or wiring diagram would be helpful.

Second reason is you’ve overwritten or run out of SRAM. I’ve had this happen to me when using the PString function. It’s just not coded properly. With the stack corrupted, all manner of weirdness can (and did) happen. Potentially you could be jumping to the same place the bootloader jumps to when it’s finished … that is the start of your code. We’d need to see your code to see if this is a possibility.

I just made a simple circuit with a photoresistor…

http://www.google.bg/imgres?imgurl=http … BQ&dur=753

This is the scheme i used.

And the code :

int lightPin = 0; //define a pin for Photo resistor

int threshold = 200;

void setup(){

Serial.begin(9600); //Begin serial communcation

pinMode(12, OUTPUT);

}

void loop(){

Serial.println(analogRead(lightPin));

if(analogRead(lightPin) > threshold ){

digitalWrite(12, LOW);

}else{

digitalWrite(12, HIGH);

}

delay(100);

}

I don’t see anything wrong with the wiring nor anything in the code that would result in a reset. However there is a problem, note that the code uses pin12 as the output but the wiring indicates pin11 is connected to the LED. Give this revision a try.

int lightPin = 0; //define a pin for Photo resistor
int LEDpin = 11;  // define an output pin for the LED
int threshold = 200;

void setup()
{
   Serial.begin(9600); //Begin serial communcation
   pinMode(LEDpin, OUTPUT);
}

void loop()
{
   Serial.println(analogRead(lightPin));
   if(analogRead(lightPin) > threshold )
   {
      digitalWrite(LEDpin, LOW);  //LED is off
   }
   else
   {
      digitalWrite(LEDpin, HIGH);  //LED is on
   }
   delay(100);
}

I don’t think the above will fix any problem that actually resembles a reset. Why do you think a reset is happening ?

Mee_n_Mac:
Why do you think a reset is happening ?

Hey! That was one of my first questions!

Because the led goes off ,while it’s dark ,it blinks and the loop begins again.The TX led on the board blinks too .

TX line should blink each time you send a serial message.

LED could go off is the threshold condition is meet.

Analog to digital converters can have a significant amount of noise in them is not properly designed for low noise. Having a glitch would not be unheard of. Caps, short leads, and other filtering may be needed on the analog input.

Does the whole light/dark turn on the LED ‘thing’ work most of the time?

Proof of a reboot would be a println in setup that says “rebooted” and see if that prints.

Sounds like brownout. What are you powering your arduino with? A picture is worth a thousand questions and usually leads to a correct answer very quickly.

WethaGuy:
Sounds like brownout. What are you powering your arduino with? A picture is worth a thousand questions and usually leads to a correct answer very quickly.

See the post by the OP above.

http://www.google.bg/imgres?imgurl=http … BQ&dur=753

From this and the code, I believe it’s running off the USB power. The OP needs to do what fll-freak suggested, put debug statements in the code and get some insight as to what’s happening.

Here’s the code with some debug prints in it and cleaned up a bit.

    int lightPin = 0;         // define a pin for Photo resistor
    int LEDpin = 11;          // define an output pin for the LED
    int threshold = 200;      // threshold to turn LED on or off
    unsigned int loopCtr = 0; // variable to hold loop counter
    int lightVal = 0;         // variable to hold A2D reading

    void setup()
    {
       Serial.begin(9600); //Begin serial communcation
       Serial.println("Starting up !");
       pinMode(LEDpin, OUTPUT);
    }

    void loop()
    {
       loopCtr ++;
       lightVal = analogRead(lightPin);
       Serial.print("Loop count is : ");
       Serial.println(loopCtr);
       Serial.print("Light value from A2D =  ");
       Serial.println(lightVal);
       Serial.println(" ");
       if(lightVal > threshold )
       {
          digitalWrite(LEDpin, LOW);  //LED is off
       }
       else
       {
          digitalWrite(LEDpin, HIGH);  //LED is on
       }
       delay(1000);
    }

Do you think that it might have to do with the possibility that you set the LED too close to the photoresistor? The photoresistor may catch some of the light and read it as enough light to turn off the LED just long enough for you to think it has reset itself.