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…
/* 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
}
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;
}
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...
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
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;
}
I think i solved the problem: PC Companion seems to be in conflict with Arduino, causing the reboot of the board.
now the program runs smoothly, but who knows?!
if you have other suggestions...they're well accepted!!
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.