Go Down

Topic: bug find? redclaration of int causes infinite loop... (Read 1 time) previous topic - next topic

Code: [Select]



#include <avr/wdt.h>
char in[101];
int x = 0;

void setup() {
 
   wdt_disable() ;
  // put your setup code here, to run once:

Serial.begin(9600);
Serial3.begin(9600);
delay(10);
Serial.println(" Begin ");
Serial.flush();

}

void loop() {
 
  while (1 == 1){
 
  Serial.println(" Loop ");
  Serial.println(millis());
  Serial.flush();
  int x = 0;      // change this line to x = 0; and it works fine
  // put your main code here, to run repeatedly:
  while (Serial3.peek() != '$'){
   in[x] = Serial3.read();
   x = x + 1;
  }
 
 
   for (int i=0; i <= 100; i++){
      Serial.print(in[i]);
   }
   Serial.flush();
   
    for (int i=0; i <= 100; i++){
      in[i] = 0;
   }
 
 
 
  }
 
  delay(1000);
  Serial.print(millis());
 
 
}




The second line  int x = 0; was wrongfully redeclared inside the loop. This causes an infinite reboot loop ...

using latest ide (1.05)



johnwasser

Not a bug in the programing language.  Just a bug in your programming. 

Local variables can have the same name as global variables.  Your code will use the most local version of the variable.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

WizenedEE


Not a bug in the programing language.  Just a bug in your programming. 

Local variables can have the same name as global variables.  Your code will use the most local version of the variable.
No program error should cause a "reboot loop." It sounds like a problem with the circuit.

also, instead of
Code: [Select]
while (1 ==1) it makes more sense to say while(true)

johnwasser


No program error should cause a "reboot loop."

A reboot is just the Arduino's reaction to most software crashes.  It's easy to cause repeated crashes with bad code.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

I would have expected the ide to pop up with an error message instead of crashing the atmega. :-)

Quote


Local variables can have the same name as global variables.  Your code will use the most local version of the variable.



Does that mean it should have worked then?

johnwasser


Quote

Local variables can have the same name as global variables.  Your code will use the most local version of the variable.

Does that mean it should have worked then?


Looks good to me.  The main problem I see is that you don't have checks to prevent a long input rom overflowing the buffer.  Try this:
Code: [Select]


#include <avr/wdt.h>
char in[101];
int x = 0;

void setup() {
  wdt_disable() ;
  // put your setup code here, to run once:

  Serial.begin(9600);
  Serial3.begin(9600);
  delay(10);
  Serial.println(" Begin ");
  Serial.flush();
}

void loop() {

  while (1 == 1) {
    Serial.println(" Loop ");
    Serial.println(millis());
    Serial.flush();

    int x = 0;      // change this line to x = 0; and it works fine
    // put your main code here, to run repeatedly:
    while (Serial3.peek() != '$') {
      if (x < 100)   // Added to protect against buffer overflow.
        in[x] = Serial3.read();
      x = x + 1;
    }

    for (int i=0; i <= 100; i++) {
      Serial.print(in[i]);
    }
   
    Serial.flush();

    for (int i=0; i <= 100; i++) {
      in[i] = 0;
    }
  }

  // This code never executes:
  delay(1000);
  Serial.print(millis());
}
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

johnwasser


I would have expected the ide to pop up with an error message instead of crashing the atmega. :-)


The Arduino is a stand-alone computer.  The IDE doesn't have any way of knowing that your sketch crashed. You have to figure that out from the behavior of your sketch: typically you see a debug message you put into setup().  That means your sketch has re-started and that usually means your sketch has crashed. (And that usually means you used up al the SRAM.)
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Paul Stoffregen


I would have expected the ide to pop up with an error message instead of crashing the atmega. :-)


How would it be able to do that?

Or more precisely, how is the IDE supposed to tell the difference between your program which crashes (and therefore doesn't communication in any way) and a program that is running normally, but isn't supposed to communicate in any way?

Go Up