Mouse.h does not work with Platformio

In Platformio every code i upload to my Arduino Micro Pro works except this one:

#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
Serial.print(vertValue);

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

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
}
}

This same code works if i upload it through Arduino IDE.
In Platformio i use pio run -t upload to upload the code, the command pio run -t program does not work because i don’t have an external programmer, my Arduino is connected directly via usb port to the PC.
Platformio uploads the code without errors, testing with Serial.print() i can see the values of vertValue and horzValue changing via serial monitor when i move the joystick, but my mouse is not moving.
I compared the library Mouse.h from Platformio library with the Mouse.h from Arduino IDE library and they are identical.
Why this same code works via Arduino IDE and doesn’t work via Platformio???

Could you upload in verbose mode? pio run -t upload -v. Please provide full output. Need to see which Mouse library is used by PlatformIO.

Sorry for take too long, it’s because my board has fried and i had to get a new one, as you can see, the Mouse is from C:\Users\Beto\Documents\PlatformIO\Projects\KeyboardMouseControl.pio\libdeps\micro\Mouse_ID890

And here is the upload command with the --verbose option:

Processing micro (platform: atmelavr; framework: arduino; board: micro; lib_deps: Mouse)

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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\firmware.elf -Os -mmcu=atmega32u4 -Wl,--gc-sections -flto -fuse-linker-plugin .pio\build\micro\src\main.cpp.o -L.pio\build\micro -Wl,--start-group .pio\build\micro\lib488\libHID.a .pio\build\micro\libf25\libMouse_ID890.a .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 5716 bytes from 28672 bytes)
.pio\build\micro\firmware.elf  :
section                     size      addr
.data                         50   8388864
.text                       5666         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                      11854
<lambda>(["upload"], [".pio\build\micro\firmware.hex"])
AVAILABLE: avr109
CURRENT: upload_protocol = avr109
BeforeUpload(["upload"], [".pio\build\micro\firmware.hex"])
Auto-detected: COM17
Forcing reset using 1200bps open/close on port COM17
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 (5716 bytes):

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

avrdude: 5716 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 5716 bytes
avrdude: reading on-chip flash data:

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

avrdude: verifying ...
avrdude: 5716 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.83 seconds =====================================================================================================

Do you use the same Mouse library with Arduino IDE? Try adding lib_archive = false to platformio.ini -> env.

1 Like

No, the library was downloaded automatically from platformio when i upload the project to the board, as you can see platformio put the library in the libdeps folder of the project:

screen

PS: The lib_archive = false in platformio.ini didn’t work.