Go Down

Topic: [SOLVED] Code Uploads fine with IDE 1.8.5 but NOT with 1.8.7 (Read 339 times) previous topic - next topic

BitKraken

Hi guys!

I have a code that uploads just fine with 1.8.5 IDE to an Arduino Nano, but it doesn't with 1.8.7.

At first I wasn't able to upload at all but after reviewing Board.txt I realized that under Board: Arduino Nano, there is a processor "ATmega328P (Old Bootloader)" which IDE 1.8.5 didn't have. So I used that one and it uploaded my code (it basically uses upload speed of 57600 instead of 115200).

The intriguing part is that my code doesn't work when uploading with 1.8.7 but when I switch to 1.8.5 it works perfectly!

Any ideas why?

Thanks in advance.

JohnRob

I'm far from an expert in the Arduino IDE but when you mentioned "switch back" to 1.8.5 are they both on the same machine?  If so could the libraries be mixed up on the 1.8.7 installation?


I just switched from 1.8.5 to 1.8.6 then the next day 1.8.7 with no issues.  I'm currently working with a Pro Mini.

Please do not PM me with thread based messages.  If your thoughts are worth responding,  the group should benefit from your insight.

BitKraken

Thanks JohnRob for your prompt reply.

Yes they are on the same machine, but they have been manually installed so there is no mix up in the libraries. You can manually install different versions in one machine with no mixing issues, I've installed many different IDEs for various reasons and never had any problems.

Thanks John

BitKraken

OK, before you slap me in the face, in my defense I've read that you could declare a variable and it will be automatically initialized to zero.

Here is a snippet of the code of how the function worked without initializing. This code worked for versión IDE 1.8.5 and below.

Code: [Select]


int readAlcohol()
 {
   
  // Number measurements
  int nb_measurements = 5;
  int measurements;

  // Measure
  for (int i = 0; i < nb_measurements; i++) {
    //measurements = measurements + analogRead(analogPin);
    measurements = measurements + analogRead(0); 
  }
  measurements = measurements/nb_measurements;
 
  return measurements;
 }



For a reason that I still don't know, now you need to initialize int measurements = 0;

Code: [Select]

int readAlcohol()
 {
   
  // Number measurements
  int nb_measurements = 5;
  int measurements = 0;

  // Measure
  for (int i = 0; i < nb_measurements; i++) {
    //measurements = measurements + analogRead(analogPin);
    measurements = measurements + analogRead(0); 
  }
  measurements = measurements/nb_measurements;
 
  return measurements;
 }




And now, in IDE 1.8.7 this code Works! Can someone please enlight me as of why? Is this the way to do it? is it a bug?

Budvar10

#4
Sep 25, 2018, 02:42 pm Last Edit: Sep 25, 2018, 02:57 pm by Budvar10
Local variable have to be initialized otherwise the value is unknown.
Only global and static variables are initialized to zero by default.

The reason why the local need to be initialized is that the compiler can decide to keep local variable just in the registers. It has no place in memory (can be in the memory but only temporarily, just  to free up the registers, it is same story). The lifetime of local variable is only during its function execution. While the execution point is outside of the function, for the local variable it is discontinuity.
Arduino clone with ATmega1284P   http://forum.arduino.cc/index.php?topic=277260.0

BitKraken

Thanks Budvar10 for clearing that up.


I've uploaded the same code using 1.6.13; 1.8.5 and 1.8.7 without initializing the variable (int measurements) and readings from my analog A0 were fine with IDEs 1.6.13 and 1.8.5 but NOT with 1.8.7 as explained above. Once I initialize the variable (int measurements =0) it works (no surprise) with all IDEs (1.6.13; 1.8.5 and 1.8.7).

It seems that IDE 1.8.7 compiler is, in a sense, stricter than other IDEs…..

Anyway....I hope this helps others.

JohnRob

I've always gone by the philosophy that if it was importation that a variable be initialized the one should do so.

Working with different systems I always initialize valuables, even if the documentation indicates you don't have to.  It stops me from ever having to consider the initialization when troubleshooting.



Please do not PM me with thread based messages.  If your thoughts are worth responding,  the group should benefit from your insight.

Go Up