Serial Communicaton by C++

Hi i got stuck in one of my projects. The problem is i have to send to arduino a lot of pre processed instructions like (
function(2);
delay(1);
function(4);
delay(2);
function(6);
delay(1);
) (it is too big for arduino's memory), i thought that i can send them by serial communication it is like 3000 commands per second it is possible to do? Can you instruct me how to use serial com in c++ (i prefer code:blocks) . Thanks for help.

What do you mean, "pre processed instructions"? Your example wasn't very clear.

Why would you need 3000 commands per second?

I suspect a major re-design is needed.

Please describe the project so we can see if there is a simpler solution.

You may find some useful stuff in Serial Input Basics

Rather than send “function(2)” or “delay(1)” just send “” or “”

…R

Hi, i am making a musical floppy drives on my own. I am using only my own code no stealing codes from internet tutorials i already made a c++ program that converts music to arduino instructions and can pre process the code to 3000 instructions like :
function(2);
delay(1);
function(4);
delay(2);
function(6);
delay(1);
but the problem is that 1 second of music like this take all memory on arduino . So i thought i can send arduino instructions up to date. The problem is i am a "noob" in arduino and i don't know how to use this serial comunication properly. If i understand right there is some kind of bufor that limits the input for arduino so i think i need a command which will check how much memomry left in the bufor.

The problem is i am a "noob" in arduino

This is not a beginner project. So, either you ditch the "noob" attribute or you give up.

If i understand right there is some kind of bufor that limits the input for arduino

There is a buffer. It does not limit how much data you can send. What it does is require that you not send data faster than the Arduino can read it and deal with it.

so i think i need a command which will check how much memomry left in the bufor.

Only the Arduino could know that. The buffer size is typically 64 bytes, and Serial.available() tells you how much is used. A bit of simple math would tell you how much room was left.

In general, that is not the proper approach. The proper approach is that the Arduino send something to say that it is ready to receive (more) data. The sending application does not send data until the Arduino says that it is ready. That's called handshaking, and is something you need to learn about.

Thanks that gave me a better look on my project now. I hope i can complete it now thanks!

You are making an interpreter then.

You should shorten those instruction names, single letter would be best and the number directly after. As soon as the letter arrives your code will know what the number is for, why bother with f-u-n-c-t-i-o-n when F might do?

115200 baud is bits/second. It takes a start bit, 8 data bits and a stop bit to serial transmit 1 byte.

115200 baud is 11520 chars/second.

1,000,000 / 11520 = 86.8~ microseconds per character, 1388 cpu cycles between available chars, enough for some processing per character right then.

Best way to handle such an interpreter is with a state machine, it uses some of those cycles right then instead of later.

State Machine serial reader is in this tutorial

The intro:

Another way of processing incoming data, without blocking, is to set up a "state machine". Effectively this means looking at each byte in the input stream, and handling it depending on the current state.

As an example, say you had this coming into the serial port:

R4500S80G3

Where Rnnn is RPM, Snnnn is speed, and Gnnnn is the gear setting.

The state machine below switches state when it gets a letter "R", "S" or "G". Otherwise it processes incoming digits by multiplying the previous result by 10, and adding in the new one.

When switching states, if first handles the previous state. So for example, after getting R4500 when the "S" arrives, we call the ProcessRPM function, passing it 4500.

This has the advantage of handling long messages without even needing any buffer, thus saving RAM. You can also process message as soon as the state changes, rather than waiting for end-of-line.

Code is included in the lesson. It can easily be adapted.

but the problem is that 1 second of music like this take all memory on arduino .

Do you know that you can store and read data that does not change in the flash memory that the program does not use? Or that you can add an SD adapter for less than lunch money?

BTW: hardest part of using C++ in Winblows to get to Arduino is getting to COM in C++!

If it is to be stored to be played back later, you can use the EEPROM.