The code compile in Arduino IDE and PlatformIO, but only works if using Arduino IDE

I entered the commands to update:

And set the platformio.ini:

And this is the upload with --verbose option:

C:\Users\Beto\Documents\PlatformIO\Projects\KeyboardMouseControl>pio run -t upload -v
Processing micro (platform: atmelavr; framework: arduino; board: micro; lib_deps: Mouse; lib_archive: False)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CONFIGURATION: https://docs.platformio.org/page/boards/atmelavr/micro.html
PLATFORM: Atmel AVR 1.15.0 > Arduino Micro
HARDWARE: ATMEGA32U4 16MHz, 2.50KB RAM, 28KB Flash
PACKAGES: toolchain-atmelavr 1.50400.190710 (5.4.0), framework-arduinoavr 4.1.2, tool-avrdude 1.60300.190424 (6.3.0)
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 7 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <Mouse> 1.0.1 (C:\Users\Beto\Documents\PlatformIO\Projects\KeyboardMouseControl\.pio\libdeps\micro\Mouse_ID890)
|   |-- <HID> 1.0 (C:\Users\Beto\.platformio\packages\framework-arduinoavr\libraries\__cores__\arduino\HID)
avr-g++ -o .pio\build\micro\libf25\Mouse_ID890\Mouse.cpp.o -c -fno-exceptions -fno-threadsafe-statics -fpermissive -std=gnu++11 -Os -Wall -ffunction-sections -fdata-sections -flto -mmcu=atmega32u4 -DPLATFORMIO=40100 -DARDUINO_AVR_MICRO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10805 -DUSB_VID=0x2341 -DUSB_PID=0x0037 "-DUSB_PRODUCT=\"Arduino Micro\"" -DUSB_MANUFACTURER=\"Arduino\" -I.pio\libdeps\micro\Mouse_ID890\src -IC:\Users\Beto\.platformio\packages\framework-arduinoavr\libraries\__cores__\arduino\HID\src -IC:\Users\Beto\.platformio\packages\framework-arduinoavr\cores\arduino -IC:\Users\Beto\.platformio\packages\framework-arduinoavr\variants\micro .pio\libdeps\micro\Mouse_ID890\src\Mouse.cpp
avr-g++ -o .pio\build\micro\firmware.elf -Os -mmcu=atmega32u4 -Wl,--gc-sections -flto -fuse-linker-plugin .pio\build\micro\lib488\HID\HID.cpp.o .pio\build\micro\libf25\Mouse_ID890\Mouse.cpp.o .pio\build\micro\src\main.cpp.o -L.pio\build\micro -Wl,--start-group .pio\build\micro\libFrameworkArduinoVariant.a .pio\build\micro\libFrameworkArduino.a -lm -Wl,--end-group
MethodWrapper(["checkprogsize"], [".pio\build\micro\firmware.elf"])
Memory Usage -> http://bit.ly/pio-memory-usage
DATA:    [=         ]   8.2% (used 211 bytes from 2560 bytes)
PROGRAM: [==        ]  19.9% (used 5720 bytes from 28672 bytes)
.pio\build\micro\firmware.elf  :
section                     size      addr
.data                         50   8388864
.text                       5670         0
.bss                         161   8388914
.comment                      48         0
.note.gnu.avr.deviceinfo      64         0
.debug_info                 2548         0
.debug_abbrev               2410         0
.debug_line                   26         0
.debug_str                   881         0
Total                      11858
<lambda>(["upload"], [".pio\build\micro\firmware.hex"])
AVAILABLE: avr109
CURRENT: upload_protocol = avr109
BeforeUpload(["upload"], [".pio\build\micro\firmware.hex"])
Auto-detected: COM20
Forcing reset using 1200bps open/close on port COM20
Waiting for the new upload port...
avrdude -v -p atmega32u4 -C C:\Users\Beto\.platformio\packages\tool-avrdude\avrdude.conf -c avr109 -b 57600 -D -P "COM13" -U flash:w:.pio\build\micro\firmware.hex:i

avrdude: Version 6.3, compiled on Sep 12 2016 at 17:24:16
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "C:\Users\Beto\.platformio\packages\tool-avrdude\avrdude.conf"

         Using Port                    : COM13
         Using Programmer              : avr109
         Overriding Baud Rate          : 57600
         AVR Part                      : ATmega32U4
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PA0
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    20     4    0 no       1024    4      0  9000  9000 0x00 0x00
           flash         65     6   128    0 yes     32768  128    256  4500  4500 0x00 0x00
           lfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           lock           0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : butterfly
         Description     : Atmel AppNote AVR109 Boot Loader

Connecting to programmer: .
Found programmer: Id = "CATERIN"; type = S
    Software Version = 1.0; No Hardware Version given.
Programmer supports auto addr increment.
Programmer supports buffered memory access with buffersize=128 bytes.

Programmer supports the following devices:
    Device code: 0x44

avrdude: devcode selected: 0x44
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% -0.00s

avrdude: Device signature = 0x1e9587 (probably m32u4)
avrdude: safemode: hfuse reads as D8
avrdude: safemode: efuse reads as CB
avrdude: reading input file ".pio\build\micro\firmware.hex"
avrdude: writing flash (5720 bytes):

Writing | ################################################## | 100% 0.50s

avrdude: 5720 bytes of flash written
avrdude: verifying flash memory against .pio\build\micro\firmware.hex:
avrdude: load data flash data from input file .pio\build\micro\firmware.hex:
avrdude: input file .pio\build\micro\firmware.hex contains 5720 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.09s

avrdude: verifying ...
avrdude: 5720 bytes of flash verified

avrdude: safemode: hfuse reads as D8
avrdude: safemode: efuse reads as CB
avrdude: safemode: Fuses OK (E:CB, H:D8, L:FF)

avrdude done.  Thank you.

==================================================================================================== [SUCCESS] Took 5.70 seconds ====================================================================================================

Still not working!

Thanks for hanging in there! :slightly_smiling_face:

I dunno what more to try… I started de-constructing the PlatformIO and Arduino IDE compile and linker commands, and whilst there were some minor variations, that didn’t seem to be altering anything… but there some parts I don’t know how to compare/pull apart.

If you don’t mind humouring me on one final possible point of difference, can you compare the file generated by this command (gets placed in the sketch folder)

with this one (fc /b for a binary comparison or some other tool)

This was the code I compiled - the same as above, with the RXLED1/TXLED1 macros commented out.



/* HID Joystick Mouse Example
   by: Jim Lindblom
   date: 1/12/2012
   license: MIT License - Feel free to use this code for any purpose.
   No restrictions. Just keep this license if you go on to use this
   code in your future endeavors! Reuse and share.

   This is very simplistic code that allows you to turn the
   SparkFun Thumb Joystick (http://www.sparkfun.com/products/9032)
   into an HID Mouse. The select button on the joystick is set up
   as the mouse left click.
 */
#include <Arduino.h>
#include "Mouse.h"

int horzPin = A0;  // Analog output of horizontal joystick pin
int vertPin = A1;  // Analog output of vertical joystick pin
int selPin = 9;  // select button pin of joystick

int vertZero, horzZero;  // Stores the initial value of each axis, usually around 512
int vertValue, horzValue;  // Stores current analog output of each axis
const int sensitivity = 200;  // Higher sensitivity value = slower mouse, should be <= about 500
int mouseClickFlag = 0;

void setup()
{
  //Serial.begin(9600);
  pinMode(horzPin, INPUT);  // Set both analog pins as inputs
  pinMode(vertPin, INPUT);
  pinMode(selPin, INPUT);  // set button select pin as input
  digitalWrite(selPin, HIGH);  // Pull button select pin high
  delay(1000);  // short delay to let outputs settle
  vertZero = analogRead(vertPin);  // get the initial values
  horzZero = analogRead(horzPin);  // Joystick should be in neutral position when reading these
}

void loop()
{
  vertValue = analogRead(vertPin) - vertZero;  // read vertical offset
  horzValue = analogRead(horzPin) - horzZero;  // read horizontal offset

  if (vertValue != 0)
    Mouse.move(0, vertValue/sensitivity, 0);  // move mouse on y axis
  if (horzValue != 0)
    Mouse.move((horzValue/sensitivity) *-1, 0, 0);  // move mouse on x axis

  if ((digitalRead(selPin) == 0) && (!mouseClickFlag))  // if the joystick button is pressed
  {
    mouseClickFlag = 1;
    Mouse.press(MOUSE_LEFT);  // click the left button down
  }
  else if ((digitalRead(selPin))&&(mouseClickFlag)) // if the joystick button is not pressed
  {
    mouseClickFlag = 0;
    Mouse.release(MOUSE_LEFT);  // release the left button
  }

  // RXLED1;
  // TXLED1;
}

@ivankravets Is there anything more we can do to try and get to the bottom of this? Judging from the binary file compare in my post above, there is only a very difference between the PlatformIO compile output and the Arduino IDE… it’s just trying to pin down what is causing it.

  1. Does PlatformIO use the same Mouse library? It is very easy to check via pio run -v
  2. Did you try lib_archive = false in platformio.ini?
  1. #890, so yes, same library in use, and same version - 1.0.1
  2. This is with lib_archive = no - prior to that, file sizes were the same, but massive variation binaries. Now it’s down to just to two lines.

If this is dev/platform issue, I would ask to report it to Issues · platformio/platform-atmelavr · GitHub

There is an issue open for it, and it’s cross linked to this thread for completeness. Hopefully we can get to the bottom of it when you have some of that ‘spare time’ I hear people talking about! :wink:

ivankravets, yeah, i already reported the issue to the developers in github on May/27.
pfeerick i would like to give you thanks for you dedication and effort to help me solving this issue, i really appreciate that, i wish you the best! :grin:

PS: I compared you hex with mine and yours is larger than mine (15.7 KB vs 15.6 KB), using the same code you posted here.

Hey, no problem… Just wish we’d been able to get to the bottom of it… I hope it gets resolved, as it would be nice for you to at least have the option of using PlatformIO for that particular project :wink:

Yeah i really hope for that!
I use VSCode for everything and i really wish i could use it to program microcontrollers like Arduino and ESP8266/32 =]

I ran into this similar issue using a Teensy LC emulating a keyboard and mouse. It compiles and works using Arduino IDE but not VS Code and Platform IO. The first issue was during build the compiler complains #Mouse and #Keyboard were not defined. I discovered the Arduino IDE runs a macro during build time that sets the USB port on the Teensy for a specific mode. In this case it was set to Serial+Keyboard+Mouse+Joystick. In VS Code, Platform IO sets the USB port on the Teensy to Serial only. So to remedy this, we have to set specific build time options in the PlatformIO.ini file. Each board has it’s own build flag macros found at:
https://docs.platformio.org/en/latest/boards/index.html#boards

After selecting the Teensy LC board from the list I found that I could configure the serial port for many different modes such as serial, HID, mouse, joystick, keyboard only, etc. It also had the option to configure the port for all modes, which worked for my case. I set the platformio.ini file to:

[env:teensylc]
platform = teensy
board = teensylc
framework = arduino
build_flags = -D USB_EVERYTHING

With this build time option, the sketch properly compiled and worked.

1 Like

Thanks for the reply snargaloo, i have set my platformio.ini to this:

[env:micro]
platform = atmelavr
framework = arduino
board = micro
build_flags = -D USB_EVERYTHING

But sadly still not working for me, it appears that the -D USB_EVERYTHING instruction only works for Teensy (Teensy — PlatformIO latest documentation).

1 Like