transfer arduino code to Linix

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?

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

This might help: Notion – The all-in-one workspace for your notes, tasks, wikis, and databases.

Arduino is based on "wiring" (See: wiring.org.co) so the wiringPi library described in that tutorial will get you a fairly long way toward running basic Arduino sketches on your Raspberry Pi. You will have to port any library you use.

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.

Are you saying you don't have to use pinMode? They really should put that in a comment section on the examples that come pre-installed.

Which of the two examples that you provided is the fastest? I've seen "<<" before but never really understood what it did, and no one ever explained it.

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 to transfer the code, I can use ctrl f and search for 13 and replace it with GPIO Bit 4? I will have to to that for all the pins but the concept would work?

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

I need more processing power though. For example, you can't write basic math problems on one line using arduino, you have to use many lines and split it up so the processor can handle it. The scram and speed has a rather large issue as well. You can use the f macro for serial, but if you are sending something to a web server if you use the f macro the speed is incredibly reduced (takes about half a second per line that uses f macro when using client.print) to the point it won't send everything and is incredibly slow at loading and refreshing. The only option to split up the web code like

client.print("<a href=\"/");
client.print("Cfir");
client.print("\">");
client.print("Yes");
client.print("</a>");
client.print("
");
//in this way, each thing printed is stored in a char[] 
//so if you print the same thing twice it doesn't use up any more scram. 
//For example, you will never print "<a href=\"/Cfir\">Yes</a>
" twice, 
//but you may print "<a href=\"/Cneg\">No</a>
".
// using method above, only extra scram used will be for "No" an "Cneg" 
//which is 6 bits more rather than 29 bits.
//It's faster (processing wise and typing wise) 
//and cleaner to write the whole thing out, 
//but the bozos that designed arduino
//didn't give us much scram to work with.
//As a result, you have to compromise by writing code like a caveman

I was told the raspberry pi B+ model would be more ideal for my needs in regards to processing and scram.

I also need to be able to run a wifi based ping test to find out if a certain device is in the building. I was told that command prompt can do this, but you need to have a computer or pi to use command prompt. Can I do this with arduino?

Thanks John, I will check out and review the links you suggested.

I need more processing power though. For example, you can't write basic math problems on one line using arduino, you have to use many lines and split it up so the processor can handle it.

Nonsense. What do you mean? The compiler is on your computer, not the processor on the board. And it doesn't matter how many lines you take to write your code, the compiler turns it into machine instructions either way.

I think you may have some fundamental misunderstandings. Perhaps if you spoke more about what you actually wanted to do we could help you accomplish your goals without a bunch of needless run-around stemming from misunderstandings of how this stuff works.

Thomas499:
I would like to know if it is possible to transfer code written for an arduino mega 2560 to a raspberry pi B+.

IMHO the short answer to that question is NO.

There are so many differences between the systems that it would be easier to write a completely new program for the RPi. The knowledge and experience of the Arduino and the design of the Arduino program may make that somewhat easier compared to the situation where you have no Arduino program.

As @Krupski said, an Arduino is a different sort of thing to an RPi. It may make sense to use an RPi in conjunction with an Arduino, or to use an Arduino Yun which effectively contains a Linux PC and an Arduino Leonardo on the same board. But the Yun Linux will be less powerful than an RPi.

And just to round things off, the long answer is that it just might be possible to write a program that would run unchanged on both an RPi and an Arduino. But it could not possibly make good use of either board because it would have to sacrifice so much in order to be compatible with both.

...R

Nonsense. What do you mean? The compiler is on your computer, not the processor on the board. And it doesn't matter how many lines you take to write your code, the compiler turns it into machine instructions either way.

I think you may have some fundamental misunderstandings. Perhaps if you spoke more about what you actually wanted to do we could help you accomplish your goals without a bunch of needless run-around stemming from misunderstandings of how this stuff works.

So are you saying that all three methods in theory should take the same amount of time and use the same amount of SRAM to send?

//  Method A: Least amount of SRAM needed
client.print("<a href=\"/");
client.print("Cfir");
client.print("\">");
client.print("Yes");
client.print("</a>");
client.print("
");
client.print("<a href=\"/");
client.print("Cneg");
client.print("\">");
client.print("No");
client.print("</a>");
client.print("
");


//Method B:  in theory it should take the same amount of SRAM as MethodA, 
//because it splits it up the same way, the only difference is that we don't include client.print each time. Reality is it takes similar amount of SRAM as Method C though. 

client.print("<a href=\"/""Cfir""\">""Yes""</a>""
"); 
client.print("<a href=\"/""Cneg""\">""No""</a>""
"); 

//Method C:  fastest to process, and cleanest to write, uses way to much of the little SRAM we have to work with though as it will add up fast. 

client.print("<a href=\"/Cfir\">Yes</a>
");
client.print("<a href=\"/Cneg\">No</a>
");

//Method D:  print this 20 times in the same loop and it will never load, way to slow. Surprisingly it isn't as slow if used as Serial.print(F(

client.print(F("<a href=\"/Cfir\">Yes</a>
"));
client.print(F("<a href=\"/Cneg\">No</a>
"));

and i thought about having the arduino talk to and control the raspberry pi, but I thought it wouldn't make since to power both devices when you could do the same thing using only one device. I really don't want to have to re-write the entire program from scratch though.

Is it possible to use the arduino to do a ping test via wifi to see if a certain device is connected to the wifi? I know how to do this using command prompt, but do not know how to do it without command prompt.

Thomas499:

... Perhaps if you spoke more about what you actually wanted to do we could help you accomplish your goals without a bunch of needless run-around stemming from misunderstandings of how this stuff works.

So are you saying ...

I find it interesting that you quoted the important part of @DeltaG's advice but you did not bother to respond to it.

...R

... Perhaps if you spoke more about what you actually wanted to do we could help you accomplish your goals without a bunch of needless run-around stemming from misunderstandings of how this stuff works.

So are you saying ...

I find it interesting that you quoted the important part of @DeltaG's advice but you did not bother to respond to it.

...R

I did

Is it possible to use the arduino to do a ping test via wifi to see if a certain device is connected to the wifi? I know how to do this using command prompt, but do not know how to do it without command prompt.

Basically I want the arduino/pi whatever to constantly do a ping test, if a certain device is connected to the wifi modem the ping test would come back successful. If that happens, i would like a light to come on. I know how to do a ping test using command prompt, but I couldn't figure out how to do it without command prompt. I also need more SRAM for the wireless data communications. Does the Yun add more SRAM? 2000 isn't a lot.

=======edit===========
Also, can I stream music using the Yun or pi? I have a Wtv020sd16p sound module for the arduino, but there is a 1 second delay between switching the sound files, and you can't stream which is important, or upload new sound files for that matter without taking the sd card out.

Basically I want the arduino/pi whatever to constantly do a ping test

ICMPPing library

ICMPPing library

Requirements

This library works with the Arduino Ethernet shield.

I looked into that before I asked about it. I use the wifi shield. I don't know how to modify libraries so that it will work with the wifi shield.

Thomas499:
I did Basically I want the arduino/pi whatever to constantly do a ping test, if a certain device is connected to the wifi modem the ping test would come back successful. If that happens, i would like a light to come on.

I guess I missed the significance of that because it did not seem to relate to the title of the Thread.

If you want an RPI to do a ping test I can't understand why you would need Arduino code. Isn't that the sort of thing that could be done much more easily with Python. And if I was using an Arduino Yun I would use Python on that also.

...R

If you want an RPI to do a ping test I can't understand why you would need Arduino code. Isn't that the sort of thing that could be done much more easily with Python. And if I was using an Arduino Yun I would use Python on that also.

My current Arduino code already has the webserver,client,send text message, activate lights, and speaker stuff working. This is only function, but I didn't want to have to re-write all that code that I already have working from scratch.

In terms of code, the only two languages that I currently know are C++; and Php -thanks to code academy.

Would Python be a better language to write in for this?

Thomas499:
Are you saying you don't have to use pinMode? They really should put that in a comment section on the examples that come pre-installed.

Which of the two examples that you provided is the fastest? I've seen "<<" before but never really understood what it did, and no one ever explained it.

The "<<" and ">>" operators are bit shift operators. Consider this: You are writing code for an FM radio chip. The volume control is 4 bits out of a 16 bit configuration register. The volume value can be from 0 to 15 (or 0x00 to 0x0F in hex - 4 bits). Now imagine that this is the control register:

[tt][tt][b]Bit        15  14  13  12   11   10   9   8   7   6   5   4   3   2   1   0[/b]
Function:[/tt]
** **  V3  V2  V1  V0  F11  F10  F9  F8  F7  F6  F5  F4  F3  F2  F1  F0 [tt]            [   volume   ] [tt][            frequency (radio station)          ]** **
[/tt][/tt][/tt]

don't panic yet!!!! Just look and see that the volume control bits are the top 4 in the control register. BUT, in the driver you are writing, you want to enter "volume 0" to "volume 15". What to do? You have to somehow slide your 4 volume bits up where they belong. THIS is where bit shifting comes in. Now look at some "pseudo code" called "setVolume":

void setVolume (uint8_t vol) // accepts 0 thru 15, anything else is illegal
{
    uint16_t register = RADIO.read (); // read the control register into var
    register |= (vol << 12); // slide "vol" up 12 bits and OR it into the register
    RADIO.write (register); // update radio with new volume.
}

Notice the direction of the shift operator. I used "<<" which slid it to the left.

Now let's say we want to READ the radio chip and find out what volume it's currently set to. We want to get out a value of 0 thru 15, but the darn data is way at the end. See it coming? We now need to use right shift. Look at the code:

uint8_t getVolume (void) // reads the radio chip, returns the volume 0 thru 15
{
    uint8_t volume; // we are going to put the value in here
    uint16_t register = RADIO.read (); // read all 16 bits
    volume = (register >> 12); // slide the bits down and grab the volume
    return volume; // return value to the anxious user!
    
}

Now, I simplified things a bit in order not to confuse. For the first example, we read the register (which contained "whatever" volume it happened to be set to) and then OR'd in our value. What we actually should have done is read the register, set the top 4 bits to zero, THEN stuck in out new value.

Now picture a wooden board with 16 holes and a pencil in each hole. This represents the 16 bits, and the volume is the top 4 pencils. Say you wanted to set the volume to "8" which is 1000 in binary. In the original example, we asked for the wooden board and simply stuck a pencil into hole 15. But what if the previous volume was... say... 6.

The board came to us like this (we're looking at volume only):

0 1 1 0 (6 in binary)

We stuck a pencil into bit 15, which gave us this:

1 1 1 0 (14 in binary - but we wanted 8!!!)

What we SHOULD have done is this:

(1) Request register = 0 1 1 0
(2) Remove all pencils = 0 0 0 0
(3) Stick in what we want = 1 0 0 0
(4) Send it back to the chip.

There are a few other things we didn't do (in the second example) as well. If we want volume, we should get the wood board, remove ALL the pencils except the volume ones, then slide the volume pattern down 12 places and take it (because bits from other stuff may "contaminate" our volume reading if we don't remove them first).

In general, you always want to set or clear ONLY the bits you are concerned with. Imagine to set the volume you did this:

register = (volume << 12);

Fine, "volume" is set, but you blew away the other 12 bits and now the radio doesn't work!

You modify ONLY the bits you care about and take care to PRESERVE the state of all the other ones.

I wanted to keep the example simple so that it would be easier to grasp. Then once you "get it" the rest falls into place and makes sense.

Hope this helps...

Thomas499:
Would Python be a better language to write in for this?

It's a long time since I abandoned PHP (in favour of Ruby) but it may well be just as useful as Python.

(I abandoned Ruby in favour of Python because Python is more widely used - at least among Arduino users)

...R

Delta_G:
Nonsense. What do you mean? The compiler is on your computer, not the processor on the board. And it doesn't matter how many lines you take to write your code, the compiler turns it into machine instructions either way.

I think you may have some fundamental misunderstandings. Perhaps if you spoke more about what you actually wanted to do we could help you accomplish your goals without a bunch of needless run-around stemming from misunderstandings of how this stuff works.

I THINK what he's getting at is the ridiculous contortions required to print simple data. For example, the Arduino way:

[b]Serial.print ("Temperature: ");
dtostrf (thermo, 5, 2, buffer_I_hope_is_big_enough);
Serial.print (buffer);
Serial.print (", Setpoint: ");
dtostrf (setpt, 3, 1, buffer_I_hope_is_big_enough);
Serial.print (buffer);
Serial.println ();[/b]

--- or --- the right way:

[b]fprintf (stdout, "Temperature: %5.2f, Setpoint: %3.1f\n", thermo, setpt);[/b]

Yeah, yeah I know linking the floating point code adds about 1.5K to the final program. Boo-hoo. But at least give us a simple checkbox to enable or disable it!!!

I added the feature to my IDE... took all of an hour or so... and I don't even know how to program in Java.

How hard can it be?

Krupski:
Yeah, yeah I know linking the floating point code adds about 1.5K to the final program. Boo-hoo.

By "floating point code" do you mean for calculations (as in going from 0.75 + 0.5 to 1.25), or for formatting (as in going from 1.25 to {'1', '.', '2', '5', '\0'})?

odometer:
By "floating point code" do you mean for calculations (as in going from 0.75 + 0.5 to 1.25), or for formatting (as in going from 1.25 to {'1', '.', '2', '5', '\0'})?

Because the Arduino boards don't have an awful lot of memory (flash, eeprom or sram), the GNU people (who make the GCC compiler that the Arduino IDE uses) offer two different libraries related to scanning and printing strings.

One library is "stripped" so that it's as small as possible.

It's stripped so that the smallest possible code can be generated and fit into the Arduino.

Unfortunately, one of the things missing is floating point support for printing or scanning strings (for example, try this):

char buffer [64];
sprintf (buffer, "This should be a floating point number: %f\n", 123.456);
Serial.print (buffer);
-- or --
printf ("This should be a floating point number: %f\n", 123.456);

What you'll get is

[b]"This should be a floating point number: ?"[/b]

The question mark is printed to represent "we're working, but we can't print a floating point number".

The reason they do that is because the library that DOES support printing (and scanning - like "scanf") strings and handling floating point uses quite a bit of memory (1.5K of PROGMEM or more).

All the math functions in Arduino WORK (that is, you can add and subtract floating point, you can do trig, anything... you just can't print is easily).

However, IF when you compile your sketch, you link in the floating point library, everything will work (and your code will be at least 1.5K larger).

I don't think this is any big deal, but some people seem to get spins, pass out or blow fuses when their 11K sketch in a 256K MEGA board ends up being 1.5K larger.

Well, that's fine... whatever floats their boat. But at least a simple checkbox in "Preferences" should be available so that users can CHOOSE to use it or not.

If you have the room, go for it and life is easier. If the sketch is bulging past the seams, then fine take the floating point code out. BUT I WANT THE CHOICE!!!

Fortunately, I know enough programming to muddle my way through and figure it out (which I did - I added the option to my IDE). But others are forced to scratch their heads, go through their code over and over again, find NOTHING wrong with it and finally post here asking the eternal question "why does it print a question mark?".

Floating point (IMHO) should be enabled by default, and the consequences of it (larger code) should be documented, and the user at least giver enough information so that they can decide "yes I want it" or "no, I really need those last few bytes", as well as the option to CHOOSE FOR THEMSELVES if they want it or not.

Have yourself some fun... search for posts asking about the dang question mark. And if that's not entertaining enough, also search for "three consecutive exclamation points". LOL!!!

I added the feature to my IDE... took all of an hour or so... and I don't even know how to program in Java.

The code or walkthrough of how you did that would be amazing. I don't see why they don't add that to the IDE automatically.

Questions I still have:

As @Krupski said, an Arduino is a different sort of thing to an RPi. It may make sense to use an RPi in conjunction with an Arduino, or to use an Arduino Yun which effectively contains a Linux PC and an Arduino Leonardo on the same board. But the Yun Linux will be less powerful than an RPi.

If you want an RPI to do a ping test I can't understand why you would need Arduino code. Isn't that the sort of thing that could be done much more easily with Python. And if I was using an Arduino Yun I would use Python on that also.

So if I buy a Yun Shield for my Mega 2560 can I run mutliple threads at once?

Also, can I stream music using the Yun or pi? I have a Wtv020sd16p sound module for the arduino, but there is a 1 second delay between switching the sound files, and you can't stream which is important, or upload new sound files for that matter without taking the sd card out. Would I be able to do this with the Yun or Pi?

Last question, can you run multiple types of code on a single webserver? For example, If I have code written in Php, and my buddy has code written in Python, could we put the code together on one web server program or would it be easier to just re-write the particular sections?

Thomas499:
So if I buy a Yun Shield for my Mega 2560 can I run mutliple threads at once?

Your link to "multiple threads" seems to have something to do with Microsoft and I don't think their products work on a Yun because it uses Linux. However it is normal Linux and runs multiple Threads as a matter of course. You can also run multiple threads within a Python program - which may be a more practical answer.

...R