convert c code to arduino

Hello,

i'm new in arduino use. i have arduino uno and i want to convert c code to arduino code.
Some body can help me.

thanks
turw

Arduino code is C code.

i want to convert c code to arduino code.

But, you're not sharing it.

Some body can help me.

Seems unlikely.

turw:
I'm new in arduino use. I have Arduino UNO and I want to convert C code to Arduino code.
Some body can help me.

Change "int main(...)" to "void setup()".

Add "void loop() {}".

Fix any syntax errors that causes (like trying to return a value from a 'void' function.

You're done!

You're done!

That would depend on the C code, wouldn't it? It don't think that things like fork() are going to port well.

It's possible you are sharing the same confusion I had. Read this thread:
http://forum.arduino.cc/index.php?topic=176184.0

AWOL:
Arduino code is C code.

More or less. The differences between the Arduino language and 'C'/C++ can be ignored for most purposes, but there is a distinction between them. I think it is correct to refer to it as a language closely based on 'C'/C++ but not absolutely identical.

The differences between the Arduino language and 'C'/C++ can be ignored for most purposes, but there is a distinction between them.

I, for one, would be interested to see the difference enumerated.
The lack of "main" isn't one of them, IMO; I write device drivers in C, and "main" is something I only ever encounter in my test programs.

AWOL:
I, for one, would be interested to see the difference enumerated.

I don't have a list of all the changes that I can refer you to, but these are the ones that come to mind:

1/ Because the Arduino scrapes together all the files it thinks you need into a temporary directory, the semantics of #include "" versus #include <> are changed.

2/ Because the Arduino IDE implicitly includes a standard set of #include files, various types and functions provided by the Arduino runtime are available automagically without you needing to provide or reference any declarations for them.

3/ Because the IDE generates function prototypes (badly) for you within a .ino sketch file, it is not required to declare a function defined in the .ino file before you use it.

4/ Because the Arduino IDE is not smart about where to put its autogenerated prototypes, some code that looks like legitimate C++ will be broken because the autogenerated prototypes are put in the wrong place e.g. Arduino will happily put its code insertions inside a #if conditional section of source, or before the type declarations referred to by the declaration.

I'm sure this isn't a comprehensive list.

It could be argued that some of the issues under 4/ are cause by the fact that the Arduino code munging is implemented poorly rather than an inherent part of the Arduino language, on the other hand the Arduino language has never been specified to my knowledge and so it is defined implicitly by what the current implementation does.

Altogether the differences are pretty minor and could be ignored most of the time, but the fact remains that there are differences between the Arduino code we write, and the C++ that gets compiled; valid Arduino code is not valid C++.

I'm seeing macro preprocessor differences; dialect possibly.
Nothing I'd call language differences though.

Automatic function prototype generation is the only thing that violates the c++ core language standard, AFAIK. Pretty poor decision from Arduino devs IMO and I'm pretty sure they didn't realize the consequences, i.e. perfectly valid c++ code like this doesn't work in ino:

struct foo {};
void bar(foo&) {}

HW guys should have kept their hands off from language dev :wink: I would just get rid of all the ino stupidity and write it all in cpp to make it c++ standard compliant.

AWOL:
Nothing I'd call language differences though.

I don't know what you would consider a language difference, but valid Arduino code is not valid C++ and to me that indicates that Arduino code and C++ are not the same, even though they share the same fundamental syntax and it is only a small amount of work to convert between them. If you learn to write Arduino code and think that you are writing C++ then you are in for a disappointment when you discover that in real C++ you are expected to deal with function prototypes and managing which files you #include and where you #include them from yourself.

JarkkoL:
I would just get rid of all the ino stupidity and write it all in cpp to make it c++ standard compliant.

Me too. Adding insult to injury, the Arduino/C++ conversion is pretty buggy. I'd file it under 'misguided and poorly implemented'. But it's not the end of the world and for little projects of the sort suitable to be run on an Arduino it's really not worth worrying about.

If you want 'pure' C++ code, move most of your code to an .h and a .cpp as a library, and call it from a skeleton .ino file (or .pde file in older Arduino IDEs).

Note, I'll give you an example, but since I'm just typing it on the fly, I may not have all of the details correct. I also use an external editor with the Arduino IDE. There is a way to create these files under the IDE, but I prefer to create them outside of the Arduino IDE using gnu emacs.

Under Linux, I would create the ~/sketchbook/libraries/MyFoo directory (it may be different under Windows or Mac).

Create ~/sketchbook/librarys/MyFoo/MyFoo.h:

class MyFoo {
public:
  MyFoo (int);
  void Initialize (void);
  void Action (void);
private:
  int state;
};

Create ~/sketchbook/libraries/MyFoo/MyFoo.cpp:

#include "MyFoo.h"

MyFoo::MyFoo (int arg)
{
  state = arg;
}

void MyFoo::Initialize (void)
{
}

void MyFoo::Action (void)
{
}

Then create your main program, that exists just to get into the 'pure' C++ layer:

#include "MyFoo.h"

MyFoo foo (1);

void setup (void)
{
  foo.Initialize ();
}

void loop (void)
{
  foo.Action ();
}

Note, if you are used to more modern C++ versions, the version of GCC that is shipped with the Arduino project is several years old, and does not have C++11 support.

MichaelMeissner:
Then create your main program, that exists just to get into the 'pure' C++ layer:

Actually, you don't need to put anything in particular in the sketch file - you can put any part of it that you want in plain old C++ files; if you want, you can even leave the .ino file completely empty and put the whole sketch in plain old C++ files.

i have arduino uno,how to convert c coding to arduino uno..in advanced skill..help me please,this for final year project.ty

You did not bother to read the thread did you?

Without seeing the structure of the C code we can't help. First try to separate things into initialization and your loop that does all the work. The initialization goes into setup(), and the rest gets put into loop().

Then start attacking syntax errors one by one and rewriting system functions that are coded differently on the arduino.

1 Like

how to convert c coding to arduino uno

What C code? Most C code will work just fine on the Arduino, as long as it does not deal with peripherals that the Arduino doesn't have, like stdin and stdout.

this for final year project

I'd expect that even if this was for a final year of elementary school, you'd know how to read the stickies at the top of the forum, and post the code in question.