Thomas499:
Hello,
I would like to know if it is possible to transfer code written for an arduino mega 2560 to a raspberry pi B+. I was watching a few youtube videos on how to program the raspberry pi in C++ but it is very far from the simple ease of what I am use to when programming arduino.
Is it possible to use the arduino compiler to write the official code that is ready to go in a file somewhere which includes all the libraries needed so that one can copy an paste it into the raspberry pi?
This is a tricky, multifaceted question.
Let's see if I can get this all straight.......
The Arduino uses the Atmel AVR chips which are programmed in C/C++. The Arduino "system" also has libraries and special custom functions to add to the usability of the AVR chip such as "pinMode" to define if a pin is an input or an output, "digitalRead" and "digitalWrite" for pins as well.
These functions are just comfort blankets for new programmers. You can just do either of these:
digitalWrite (13, HIGH);
-- or --
PORTB |= (1ULL << N); (whatever bit pin 13 in on - I forget.
Of course, the first one is simpler to grasp and easier to learn. Once you really "get into" the programming, you begin to realize that the "comfort commands" have a lot of overhead, you begin to feel the need for speed and you end up using the more cryptic looking, but faster version.
Now, Linux is compiled from source, so it can run on virtually any computer, microcontroller, heck probably a bag full of transistors could run Linux.
The Raspberry PI boards use the ARM processors (ARM = Advanced RISC Machines and "RISC" = Reduced Instruction Set Computer).
So, take Linux source code, get a copy of GCC for the ARM target and compile. Bingo, you have Linux running on the Raspberry PI.
Once the Pi is running, you can use the ARM-GCC compiler and compile your own code, but this would be strictly C/C++. The Arduino libraries mostly support the specific hardware of the Arduino boards, so that code would have no place and no use on a Pi (although you COULD probably compile it - but there would be nothing for it to do).
Now, imagine you have an Arduino board and you write a fairly complex program... maybe an LCD or graphic display, several buttons, a menu, ports that you turn on and off and/or read/write with. This code could be re-compiled on a Pi board (you would just need to redefine things like pin numbers - for example "Pin 13" means nothing on a Pi, but you might be able to use GPIO Bit 4 to do the same job).
So realize that when you write programs for an Arduino, a Raspberry PI or Linux (on any platform - including PC and MAC), all you are doing is writing and compiling C/C++ code which is the same across all platforms (except for the DEVICE SPECIFIC hardware).
For example, "printf ("Hello World\n")" will compile on any platform as-is. But to blink an LED on Arduino Pin 13, you may need a completely different named port and pin of other platforms. All of them can DO IT, but the names and numbers are different.
Lastly (in my own opinion), a Raspberry Pi is really not the "same animal" as an Arduino. An Arduino is a microcontroller. It's meant to read sensors, make binary decisions, drive motors, operate displays, etc... whereas a Raspberry PI is more of a VERRRRYYY slow PC with a boat anchor chained to it.
Honestly, I see little use for the Pi board. It's too complicated and powerful to be used as a microcontroller, and as a "computer" (i.e. PC) it's downright pathetic.
So there's my 2 cents. Hope this explains a little.........