Go Down

Topic: setup() and loop(): does the board restart itself? (Read 995 times) previous topic - next topic

Hack3berry

I know that the code in "setup() function" should run ONCE, and the one in "loop() function" should run repeatedly, but I have a question (and maybe a problem) : is it normal that after a while the code in the loop() section stops and the code in the setup() section is executed again?

I made some tests with led lights: at the beginning they're "LOW", the I set them "HIGH" in the loop()...but at a certain point they turn off, and back on again and again...

thanks for the help


ballscrewbob

Please post your sketch but use the code tags to do so ( </> )

It is unusual but it is also possible to force a sketch to run from the start too so without seeing the code then anything is possible.

It may not be the answer you were looking for but its the one I am giving based on either experience, educated guess, google or the fact that you gave nothing to go with in the first place so I used my wonky crystal ball.

Hack3berry

Code: [Select]
/* Turn off Led 13*/
void setup() {
pinMode(13, OUTPUT);//led on pin 13
digitalWrite(13, HIGH);//turn on led
}

void loop() {
delay(3000);//wait 3 seconds
digitalWrite(13, LOW);//turn off led
}

sterretje

Please show us a diagram (hand drawn is fine) how everything is connected.

The code in reply #2 should not reboot the Arduino.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Hack3berry

Code: [Select]
const int switchPin = 8;
unsigned long previousTime = 0;
int switchState = 0;
int prevSwitchState = 0;
int led = 2;
long interval = 10000;

void setup() {
  // put your setup code here, to run once:
  for (int x = 2; x < 8; x++) {
    pinMode(x, OUTPUT); 
  }
  pinMode(switchPin, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  unsigned long currentTime = millis();
  if(currentTime - previousTime > interval) {
    previousTime = currentTime;
    digitalWrite(led, HIGH);
    led++;
    if(led == 7){
     
    }
  }
  switchState = digitalRead(switchPin);
  if(switchState != prevSwitchState) {
    for (int x = 2; x < 8; x++) {
      digitalWrite(x, LOW);
    }
    led = 2;
    previousTime = currentTime;
  }
  prevSwitchState = switchState;
}

Hack3berry

the code up here reboots itself several times: my "hourglass" ,as you can see at line 6, counts the minutes, but only three leds (on six) light up, then the board does a reboot...


p.s : I'll post a photo of the connections asap

Hack3berry


Hack3berry

ok: here's a funny thing:

if in the sketch I include Serial.begin(String) and then in the loop include Serial.print(Other String) my "Hourglass" seems to work fine ONLY IF I open the Serial Monitor on Arduino IDE :


if I launch the program and keep open the window with the Serial Monitor everything's fine, but as I close it, after a while the boards resets itself


Code: [Select]
const int switchPin = 8;
unsigned long previousTime = 0;
int switchState = 0;
int prevSwitchState = 0;
int led = 2;
long interval = 10000;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.print("inizio");
  for (int x = 2; x < 8; x++) {
    pinMode(x, OUTPUT); 
  }
  pinMode(switchPin, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  unsigned long currentTime = millis();
  if(currentTime - previousTime > interval) {
    previousTime = currentTime;
    digitalWrite(led, HIGH);
    Serial.print("Sono passati ");
    int tempoInSec = currentTime / 1000;
    Serial.print(tempoInSec);
    Serial.print(" secondi, ");
    led++;
    if(led == 7){
     
    }
  }
  switchState = digitalRead(switchPin);
  if(switchState != prevSwitchState) {
    for (int x = 2; x < 8; x++) {
      digitalWrite(x, LOW);
    }
    led = 2;
    previousTime = currentTime;
  }
  prevSwitchState = switchState;
}

Hack3berry

I add that the program (I'm talking about the HourGlass) works fine just for about 30s (without Serial.begin(...) and the Serial Monitor opened)

Hack3berry


Hack3berry

I think i  solved the problem: PC Companion seems to be in conflict with Arduino, causing the reboot of the board. :smiley-confuse:
now the program runs smoothly, but who knows?!
if you have other suggestions...they're well accepted!!

ballscrewbob

Is that the SONY thing ?
If it is and you can do without it then kill it.

If not then would you mind posting a direct link to it as there are also some dubious items with that name too.

It may not be the answer you were looking for but its the one I am giving based on either experience, educated guess, google or the fact that you gave nothing to go with in the first place so I used my wonky crystal ball.

westfw

Quote
PC Companion seems to be in conflict with Arduino, causing the reboot of the board.
Various apps that "scan" serial ports can cause the arduino to auto-reset; it's something that the hardware does whenever the port is "opened."

BigBobby

The official way the Arduino Uno expects the auto-reset to be disabled is by cutting a trace and installing a pushbutton to be pressed every time that you want to load new code.  Google "Arduino Uno RESET-EN" for directions how to do this.

It seems that adding a 10uF Capacitor between Reset and Gnd also works, which you would then need to remove every time that you want to load new code -> https://electronics.stackexchange.com/questions/49373/how-to-keep-the-arduino-uno-up-on-serial-connections


Go Up