Trying to understand how to import arduino code

hello, i am new to vscode and platformio. I am trying to understand how to import a project. I’ve been reading and already solved some problems.

here is what i did:

  • change the name to .cpp, i also it changed back to compile the code
  • declare the functions
  • moved the .h to the include branch
  • made some changes to platformio.ini , but end leaving it as it was

What i am missing?

this is my platfrom.ini

[env:uno]

platform = atmelavr

board = uno

framework = arduino

i still get this errors

  • src\main.cpp:440:12: warning: unused variable ‘dummyReading’ [-Wunused-variable]
    byte dummyReading = mozziAnalogRead(analogPins[i]); // need to read pin once because first reading is not accurate
    ^
  • src\main.cpp: In function ‘void flashLeds()’:
    src\main.cpp:869:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    while(millis()<(i+1)*25) {

which correspond to

  • byte dummyReading = mozziAnalogRead(analogPins[i]);
  • while(millis()<(i+1)*25)

and finally
C:\Users.…\AppData\Local\Temp\ccSym2at.ltrans0.ltrans.o: In function main': <artificial>:(.text.startup+0x1d4): undefined reference to randSeed(long)’
:(.text.startup+0x1dc): undefined reference to startMozzi(int)' <artificial>:(.text.startup+0x1fc): undefined reference to Bounce::interval(unsigned int)’
:(.text.startup+0x210): undefined reference to Bounce::attach(int, int)' <artificial>:(.text.startup+0x310): undefined reference to rand(int, int)’
:(.text.startup+0x46a): undefined reference to rand(int, int)' <artificial>:(.text.startup+0x48c): undefined reference to rand(int, int)’
:(.text.startup+0x734): undefined reference to rand(int, int)' <artificial>:(.text.startup+0x774): undefined reference to rand(int, int)’
:(.text.startup+0xa84): undefined reference to audioHook()' C:\Users\deadman\AppData\Local\Temp\ccSym2at.ltrans1.ltrans.o: In function global constructors keyed to 65535_0_main.cpp.o.2134’:
:(.text.startup+0x180): undefined reference to `Bounce::Bounce()’

I am also wondering about the declaration of two functions that mozzi library use to control the sound. updatesound, updateaudio

I was reading in this nice post about the diferent types from arduino a c++, but it seems not to give me problems some byte tipes…

Thanks in advantage just for reading the post, and ofcourse if you could give me some directions or links, any help.
i guess this is a common problem

Hello @pedante.

The first thing I would do is re-import the project – to a different folder when you save it – and then, compile it unchanged. It will still be an Arduino *.ino source file and it should tell you if all is well with the sketch, or otherwise.

Once that works fine, we can then progress with the conversion to *.cpp and so on.

The two “errro” messages you list above are actually warnings. They have not prevented your code from compiling.

That one is simply saying that a variable, dummyReading was created and possibly initialised, but was then never used. The [-Wunused-variable] is just the compiler option which has caused the warning. You can safely ignore these warnings, or if it is possible, delete the variables in question. In this case, it’s not possible, so you can live with it, or edit the code to flag the variable as unused:

unsigned int  dummyReading __attribute__((unused));

Those are a pair of underscores before and after the “attribute” word – 4 underscores in all.

This warning is obvious (?) as the code is comparing a signed value with an unsigned one. Looking at the expression it highlights, I see where the problem lies. millis() returns an unsigned long but the expression “(i+1)*25)” is signed – probably because i is signed (I’m guessing), so that line can be fixed by casting the expression to unsigned long as per this:

while(millis() < (unsigned long)(i+1)*25)

So that’s the warnings done and dusted, I hope!

The other errors are coming from the linker (named ld) and indicate that something being called is not found. So, question, did you add #include "Arduino.h" to the top of your main code file? (The one with main() in) – if not, you will need to do this.

randSeed() is not an Arduino supplied function. There is on that is supplied with the Arduino, randomSeed() which is declared in Arduino.h and implemented in Wmath.cpp. I would think that the inclusion of Arduino.h at the top of your main source file would fix that error. Equally rand() is again, not supplied with Arduino, random() on the other hand, is.

The Bounce stuff is probably coming from the “Bounce” library for debouncing key presses? If so, this is not a built in Arduino library, so you need to tell PlatformIO that it has to find and use the Bounce library:

  • Go to PlatformIO Registry
  • Type in “Bounce platform:arduino” as the search criteria
  • Search.

You will see that “Bounce2” is the top hit. That’s the one you want so click on the name to open it.

You have two choices, one to use the command line to install the library into your project. The second, and best (other opinions are available!) is to add it to your project’s platformio.ini file as that way, when you zip up the project for safe keeping, you’ll have the source to be able to recreate it at some future point – without having to think of any installed libraries.

Click on the “installation” tab and you will see a number of was to install the library, I would advise the recommended variant, so add the following to platformio.ini:

lib_deps =
     thomasfredericks/Bounce2 @ ^2.55

That’s 4 spaces at the start of the line there by the way, or one single TAB character. Next time you compile the code, PlatformIO will go off and fetch the library source. build it into the .pio\build\???? folder in your project ready for further use.

You will also need to add #include "Bounce2.h" to your source file(s). How do I know this? Click on the Headers tab on the library’s page and it tells you the name(s) of all the required headers.

Sorry, I can find no references on Google to those function names for an Arduino. :frowning_face:

Yes indeed, that is an excellent post – even if I say so myself! (Modesty is my greatest virtue! :rofl:)

I’m assuming that you mean byte data types? If you have not used them in your code, then they will not be a problem. I used them in the example I wrote about just to cause errors as they are not “standard” C++ data types, they are something invented by the Arduino project. They are just an 8 bit value and as such can be replaced by uint8_t to replace unsigned byte or int8_t to replace byte. In either case, under PlatformIO, you will need to #include <stdint.h> which holds the definitions of these data types.

HTH

Cheers,
Norm.

Thanks @normandunbar,

Yes i had included Arduino.h, i guess those errors might be referring to other functions inside mozzi libraries . I fix the problem by adding

lib_extra_dirs =

    C:\Users\...\Documents\PlatformIO\Projects\drumkid1.2\include\src\Bounce2

    C:\Users\...\Documents\PlatformIO\Projects\drumkid1.2\include\src\MozziDK

to the platformio.ini file. weirdly it didn’t work by adding the original location

lib_extra_dirs =

    C:\Users\...\Documents\arduino\1.2\drumkid\src\Bounce2

    C:\Users\....\Documents\arduino\1.2\drumkid\src\MozziDK

The program i am working with uses a custom version of mozzi.h that’s why it need to use the local version. For bounce2.h it does no matter.

#include "Arduino.h"
 #include "src/Bounce2/src/Bounce2.h"
 #include "src/MozziDK/src/MozziGuts.h"
 #include "src/MozziDK/src/Sample.h"
#include "src/MozziDK/src/mozzi_rand.h"
#include "src/MozziDK/src/Oscil.h"
#include "src/MozziDK/src/tables/saw256_int8.h"

// include drum beat pattern definitions from separate file
#include "beats.h"

// include sample data (generated via Python script)
#include "sample0.h"

What i don’t understand is why the compiler found the location of some libraries, as for example beats.h and sample0.h and not the bounce2 and mozzi. I located both inside include in the project folder. I also didn’t have to give the location in lib_extra_dirs inside plaformio.ini

by the way the code has many byte data types. maybe it’s taking them from the arduino.h.

Hope to find more excellent posts while learning.

Thanks again, and please understand i am starting again with programming and probably i am not using the correct terms.

Hi @pedante,

if you have the source code for your libraries, mozzie and bounce2, then you can either:

  • Put the *.cpp and *.h files into your src folder - not advised though;
  • Put the *.cpp files into src and the *.h files into include - better, but not the best solution;
  • Use the lib folder. This is sort of frowned upon these days, but works and saves having to mess with lib_deps and such like. :wink:

If you take the latter option, then within lib create mozzie and bounce2 then put the *.cpp and *.h files for mozzie into lib/mozzie. Likewise put the *.cpp and *.h files for bounce2 into lib/bounce2.

The format for the lib folder, or any library folder is:

lib
   mozzie
      mozzie.h
      mozzie.cpp
   bounce2
      bounce2.h
      bounce2.cpp

Each different library has it’s own separate folder within lib, containing all the files for the library. This is what I did in the “olden days” (about 6 months ago!)

With PlatformIO though, the recommended method is to install libraries into each project which uses them. This can be done on the command line using pio lib install "distributer/library_name" but most people I’ve dealt with here install from the platformio.ini file using the method I posed above, where you search for the library and follow the “installation” tab on the results page.

Have fun learning, and don’t be afraid to ask questions.

Cheers,
Norm.