Data received from serial communication

Hi guys, I am using my arduino as an ADC to calculate flex sensors angles, in total I have 6 flex sensors. The angles calculated from the arduino are then transferred to the pc via usb and inputted in a c++ code to rotate 6 smart servo motors. I managed to control 1 motor with 1 flex sensor, the problem is that the data from the arduino is being inputted as a group so I can't access each sensors data individually and assign it to its respective motor, is there any way that I can do it?

See example #5 of the serial input basics thread. It is written for receiving, but the use of methods, start and end markers and delimiting is applicable to your project for transmission.

Thanks a lot @groundfungus , but the data received is all integer but the 6 sensors data is all grouped in 1 variable can I split it in multiple variables?

Show me what the data looks like, please. The 6 integers must be separated somehow or it would look like one huge number. You have control over how the data is formatted, don't you?

Please post the Arduino code.

The data is separated one after the other each sensor in a new line, and yes I have control on the type of data being received.

So you are sending all of the data that is in the screen shot to the PC application? I think that you would be better off sending a structured packet of comma separated values to the PC. Much easier to parse. I would send just the raw ADC output values. Then do the calculations for resistance and bend angle in the PC app.

Why don't you run the servos directly from the Arduino, that has no busy OS?

I can't use the arduino on its own since there is an other part of the project which requires the use of a 6 axis robot this is just part of it.

I managed to improve a bit the system and the output is giving only integers as attached, what is a structured packet of comma separated values, in this way can I access each value individually?

output.JPG

The concept of comma-separated-values (CSV) is very common in computer programming. Google it.

It is used in the 3rd example in Serial Input Basics (which was linked to earlier)

Sending data is that format is as simple as

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

The use of start and end markers is optional but can greatly improve reliability by ensuring that the receiving device knows when it has the complete message

,,,R

Fine I used the comma seperation and I got for example <0,1,0,-1,0,0>. Can I know get the value of 1 for example and use it in a function not the whole group of data?

Again referring to Robin2's serial input basics example #5. Use the receive function to get the data. Modify the parseData function to strip off the 6 integer values into each their own variables with the atoi() function.

JoshuaBarbara:
Can I know get the value of 1 for example and use it in a function not the whole group of data?

What program is receiving the data?

If it is a program you are writing on a PC there may well be special function for extracting the elements from a CSV string.

...R

The program is receiving data from arduino, but each sensor is assigned to a specific motor. So I need to assign the 1st digit in the csv to the 1st motor. using serial input basic example 5 is there any other example with all integer variables and keep in mind that these values are constantly changing?

Thanks a lot guys this is the 1st time programming with arduino :slight_smile:

Look here for strtok() reference.

Here is example #5 modified to parse integers (unfinished).

void parseData() {      // split the data into its parts
    strcpy(tempChars, receivedChars);
    char * strtokIndx; // this is used by strtok() as an index

    strtokIndx = strtok(tempChars,",");      // get the first part - the string
    FSR1 = atoi(strtokIndx); // convert this part to an integer

    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    FSR2 = atoi(strtokIndx);     // convert this part to an integer

     strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    FSR3 = atoi(strtokIndx);

     strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    FSR4 = atoi(strtokIndx);

May I suggest that you set aside your project for a bit and play with the serial input basics tutorial for a bit so that you understand how each function works. Start at the beginning and work through it step by step.

JoshuaBarbara:
The program is receiving data from arduino,

What program?

Is it a program that you are writing?
If so, what programming language are you using?

...R

JoshuaBarbara:
I can't use the arduino on its own since there is an other part of the project which requires the use of a 6 axis robot this is just part of it.

Like a 6-axis machining center?

I mentioned using MCU for motor and sense because a PC with a multi-user/tasking OS like Linux or Windoze can't spare the attention to run 1 motor well. But perhaps you're not running a tasking OS or maybe have the motors driven by dedicated hardware.

FWIW, if you put grouped sensors and motors on the same board, you can achieve a level of automatic reflex that doesn't require "brain" time to react even as the board communicates with the "brain" and carries out tasks --- this is borrowed from biology.

If you build sub-systems to deal with the gnarly bits then they can be controlled with nice clean abstract commands.
A finger might use a $1 ATtiny, a hand controlling fingers might go a $2 ATmega328P and the arm use something in-between, you can program many different AVR chips with an Arduino. If you want a ganglia, the $5-$6 ATmega1284P has 16K RAM, 4K EEPROM, 128K flash and 36 IO pins.

AVR chips can run on an SPI bus as slaves. SPI at default speed is 512KB/s and bi-directional, the port reads as it writes.
SPI bus can be chained from chip to chip and allow sending commands while receiving input, we do have libraries for this.

Could one of these do for a brain?
https://www.pjrc.com/store/teensy36.html

Version 3.6 features a 32 bit 180 MHz ARM Cortex-M4 processor with floating point unit.
....
Actual size is 2.4 by 0.7 inch

A 32-bit CPU with FPU and it can OC to 240MHz. 256K RAM and a meg of flash.... USB and SD on-board....
https://www.pjrc.com/teensy/techspecs.html

It's not a modern PC but without an OS in the way even a 16MHz 8-bit with 2K RAM can do, this monster board lets you do proper floating-point at speed. It has 13-bit ADC and 1 DAC.

I managed to improve a bit the system and the output is giving only integers as attached, what is a structured packet of comma separated values, in this way can I access each value individually?

What Serial.begin() speed do you use? Is it the fastest you can?

You are new to this so you don't know how to use the power of even the AVR's. Most people don't use 1%, they use delays.
AVR gives you direct 1 cycle access to the IO pins. With a 200x faster PC the OS can still take far longer working through higher priority tasks that don't consider 1ms to be a long wait, or even 100ms at times.

You'll get to know as you go. Pursue the BlinkWithoutDelay, aka DoManyThingsAtOnce lesson to get a better view of what you can Arduino.