Pages: [1]   Go Down
Author Topic: bug find? redclaration of int causes infinite loop...  (Read 1118 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 49
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:


#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)


Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 179
Posts: 8070
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Edison Member
*
Karma: 17
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
while (1 ==1)
it makes more sense to say while(true)
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 179
Posts: 8070
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 49
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 49
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 179
Posts: 8070
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

#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());
}
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 179
Posts: 8070
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.)
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

0
Offline Offline
God Member
*****
Karma: 24
Posts: 587
Always making something...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Pages: [1]   Go Up
Jump to: