I'm a developer that now turned to arduino as a way scape the corporate stress life...
Being using the Arduino IDE for a while, running a few small projects, and wanted to try the Command line tool in order to have better productivity when coding. Haven't done much cpp in a while, so this could possibility be just another case of super simple typos that we can't find on our own.
Ultrasonic sensor to measure distance, using HC-SR04 and oled display
Sensor is working fine, measuring proper distances, without a problem, when I'm compiling and uploading to the same arduino using the Arduino IDE
Problem:
Compiling and Uploading using Arduino CLI will lead to HC sensor deliver different measurements on the same code I'm compiling and running with Arduino IDE
Same code, different results
Arch:
SparkFun Pro Micro 5V 16Hmz
Attached:
Print from IDE with configuration that I can run a distance measurement test and it gives me proper results (on a scale, given general inaccuracies of the sensor)
The sensor would present a increasing distance when compared to my control sample (Running the same code with Arduino IDE) - the bigger the distance, the bigger the discrepancy - linear progression.
Control: Sensor: 10cm
Arduino-cli: Sensor: 15cm
Control: Sensor: 20cm
Arduino-cli: Sensor: 30cm
What I have tried:
Connected to the sensor and run vanilla sketch with bare distance calculation (not using the Ultrasonic library) - Same Result
Changed controller (to Arduino Nano ) - same result
Hi @victorhg. I'm going to ask for some additional information that might show us some difference between the compilation process that's happening when you're using the Arduino IDE vs. Arduino CLI. Please do this:
In the Arduino IDE:
File > Preferences
Check the box next to "Show verbose output during: > compilation
Click "OK"
Sketch > Verify/Compile
After the compilation finishes, copy the full contents of the black console pane at the bottom of the Arduino IDE window and post it here. It will almost certainly exceed the forum's 9000 character limit, so save it as a text file and attach it to your forum reply. If you click the "Reply" button you'll see the "Attachments and other options" link that will allow you to make the attachment.
Post also the full output from your "arduino-cli compile" command, using the -v flag for verbose output.
I noticed the binaries differ in size and even tough both installations are pointing to my user arduino files, they are not always using the same libraries...
The reason for the difference is that in the Arduino IDE you have selected Tools > Processor > ATmega32U4 (5V, 16 MHz), which causes the code to be compiled for a 16 MHz clock by adding a -D flag to the compilation commands setting the global F_CPU macro to 16000000L:
-DF_CPU=16000000L
The FQBN you are using with your Arduino CLI configuration doesn't specify a value for the custom cpu board option, so the default value is used (the first option in the menu). So it's the equivalent of compiling in the Arduino IDE with Tools > Processor > ATmega32U4 (3.3V, 8 MHz) selected. This causes the F_CPU macro to be set to 8000000L:
-DF_CPU=8000000L
The solution is to specify the correct value for the cpu custom board option via the FQBN. So in your sketch.json, instead of this:
It looks like you tried to define this via a separate field in sketch.json:
"cpu": "16MHzatmega32U4",
but this is not supported so it was just ignored by Arduino CLI.
The command:
arduino-cli board details SparkFun:avr:promicro
will show you the available board options and their possible values along with the selected value for each option that is selected by the FQBN you specify to the command.
As for that nil pointer exception, I'll try to look into that further later today. I just tried your command on Linux and Windows with Arduino CLI 0.11.0 and Sparkfun:avr 1.1.13, just like you and didn't get that NPE, so I'll need to try it on my macOS machine to see if it's an OS-specific issue.
Hi @victorhg. I apologize for taking so long to reply. I did try it on macOS, but still wasn't able to reproduce that NPE. I wanted to suggest that you try the new release of Arduino CLI to see if the problem you're having was already fixed by one of the many improvements that have been made since the 0.11.0 release, and that release is now out!: