Converting C++ to pde

Hi,

I’m pretty new to Sparkfun and Arduinos.

I bought some Rebel LED parts (http://www.sparkfun.com/products/9748) and I was planning to use my Arduino UNO as the PWM generator. The Sparkfun link provided some sample code for the Arduino, but it appears to be in raw C++ files that the Arduino program refuses to open; it gives me an error message saying it only opens .pde files.

I’m pretty new to this, so I would appreciate any help. How do I open those files and load them into the Arduino?

Thanks! :smiley:

Nate

As a starting point:

Rename the .c file to .pde.

Rename the main() function to “void setup()”

add an empty “void loop() {}” function.

See if it compiles…

It compiled! Thanks for your help!

Out of curiosity, what does the empty void loop function do? When I forgot to add it in, I got this error message:

core.a(main.cpp.o): In function `main’:

C:\Documents and Settings\Nate\My Documents\Downloads\arduino-0022\arduino-0022\hardware\arduino\cores\arduino/main.cpp:10: undefined reference to `loop’

Again, really appreciate your help! :smiley:

Most C languages use main as the main entry point. From main one normally does the initialization and then for most embedded work start an infinite loop for processing. Arduino breaks that mold and gives you two entry points. One (setup) that gets called once on power up, and then loop that gets called from an infinite loop.

The arduino “core” contains a main() function that looks like:

main()
{
  setup();
  while (1)
    loop();
}

This is/was a philosophical decision, reflecting the way a microcontroller can’t “end” a program, and it means that both setup() and loop() need to be defined elsewhere in the “sketch.” setup() is somewhat more like main() in that it is only called once, so renaming main() from a cpp program being ported to setup() is slightly easier than renaming it to loop() and putting some kind of “stop” at the end.

OK… But why can’t I just use the Arduino environment with plain C or C++ ? I think it’s a really useful option.

Because the built-in main function is part of a library, you can overwrite it with your own. The following code will compile properly:

void main()
{
    init();    //Required for Arduino core functions

    //User code goes here
}

Just don’t forget the init() function call!

As a side note, returning from main is not a problem because the compiler puts an infinite loop at the end. I do it all the time.