Serial communication

Hello, I want to ask, how can I send 3 or more variables over Serial port from Arduino to another simultaneously? I tries to send them as an array, but I could not because serial.write/print does not work with arrays.

Just a beginner to Arduino.

Put the values in an array and use a for loop round the Serial.write() for each element

What data type are the variables ? You could put them in a buffer using sprintf() and send the buffer

The data type I want to send is Float values I got them from sensors. I tried to change the float into char and combined them in one char array then send them. I do not know if that's right or no?

If you give us a specific example of the data that you wish to communicate, we can give specific advice.

See the serial input basics tutorial for information on reliable serial comms.

You need to be a bit more specific what you want as a result. Lets assume you just want values to be displayed in the Serial Monitor window.

Loop trough you array and send one value at a time.

for( index ... ) Serial.print( array[index] );

This will print all values without any separators. You can add separators with another print

for( index ... ) Serial.print( array[index] ); Serial.print( "," )

or use the println function to get them one value per line.

for( index ... ) Serial.println( array[index] );

Dont worry about multiple calls of print in the loop. The serial connections is so slow that your processor can do lots of other things in between.

Once you have that you can create your own function to print arrays with the formating you want.

Where are you sending the data.

I tried to change the float into char and combined them in one char array then send them.

What happened? Did that work? If not, can you post the code, tell us what the code did and how that differs from what you want. Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask an effective question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

You can use the dtostrf() function to change the floats to strings (note small s*) and then use sprintf() to put them into a (comma delimited) string to send.

*Small s string is a null terminated character array. Big S String is an object of the String class. Big S Strings can cause memory problems with Arduino and are best avoided unless you know how to use them.

Another way is to use multiple print statements to send the data. Here is an example with comma delimited data.

Serial.print(floatA);
Serial.print(",");
Serial.print(floatB);
Serial.print(",");
Serial.println(floatC);

It does not really matter if you use (as an example)

Serial.print("ABC");

Or

Serial.print("A");
Serial.print("B");
Serial.print("C");

Serial.print/println/write places the data in a software buffer that will be transmitted (and emptied) byte-by-byte under interrupt control.

Hello,
Sorry for not answering soon,
This is the code:

Sender Code:

const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
int sensorValue = 0;        // value read from the pot
float vpc = 0.004887585532;
float V[1];
char Info[100];
char result1[8];
char result2[8];
int i = 0;
void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}

void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);
  V[0] = sensorValue * vpc + 2;
  V[1] = sensorValue * vpc + 1;
  dtostrf(V[0], 6, 2, result1);
  dtostrf(V[1], 6, 2, result2);
  for (i = 0; i <= (sizeof(Info) - 1); i++)
  {
    if (i <= sizeof(result1))
      Info[i] = result1[i];
    else
      Info[i] = result2[i - sizeof(result1)];

  }
  Serial.write(Info, 18);
  delay(200);

}

Receiver Code:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
//const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
//LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
char Text[20];
void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // initialize the serial communications:
  Serial.begin(9600);

}

void loop() {
  // when characters arrive over the serial port...
  if (Serial.available()) {
    // wait a bit for the entire message to arrive
    delay(100);
    // clear the screen
    lcd.clear();
    // read all theh available characters
    while (Serial.available() > 0) {
      // Still stuck here do not know what to do
      Serial.readBytes(Text,18);
      Serial.println(Text);
      
      //lcd.write(Serial.read());

    }
  }
}

Using the techniques in serial input basics you can avoid the blocking readBytes() function and the stupid delay() for waiting for the data to arrive.

OK, I will give it a shoot.

The technique in the 3rd example in Serial Input Basics will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

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

…R

float V[1] is an array with one element and only has a member V[0]. Assigning a value to V[1] can result in undefined behaviour.

There are probably more errors like this in your code.