Serial Communication float variable values are not coming on to 2nd controller

Hi all -

Thanks in advance for taking out your time to read and give out suggestions.

I, need to transfer a variable value(float) onto second controller via serial port, communication is happening between controllers but values are not printed on 2nd controller, please suggest where things are missing.

Enclosing the code.

Best,

Ramesha

FordCupViscosityDataReceiver.ino (475 Bytes)

FordCupViscosityMeasurement_SerialCommunication.ino (1.76 KB)

You are transferring the value of 'duration' to destination. Have you thought that you are sending only the integer part of this: duration = currentMillis/1000ul?. The fractional part is not sent. In order to send the whole floating point number, place the following codes at the appropriate place of your transmit sketch.

float duration;
duration = (float)(currentMillis/1000.0);

Also bring necessary change in your receiver sketch.

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example 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

GolamMostafa:
You are transferring the value of 'duration' to destination. Have you thought that you are sending only the integer part of this: duration = currentMillis/1000ul?. The fractional part is not sent. In order to send the whole floating point number, place the following codes at the appropriate place of your transmit sketch.

float duration;

duration = (float)(currentMillis/1000.0);




Also bring necessary change in your receiver sketch.
#include <Wire.h>
#include <VL6180X.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
#define LED 13
VL6180X sensor;
float dist, sens;
int count;
unsigned long currentMillis;
unsigned long previousMillis;
unsigned long duration;
void setup()
{
  Wire.begin();
  sensor.init();
  sensor.configureDefault();
  sensor.writeReg(VL6180X::SYSRANGE__MAX_CONVERGENCE_TIME, 30);
  sensor.writeReg16Bit(VL6180X::SYSALS__INTEGRATION_PERIOD, 50);
  sensor.setTimeout(500);
  sensor.stopContinuous();
  delay(300);
  sensor.writeReg(0x10A, 0x08);
  sensor.startRangeContinuous(100);
  Serial.begin(57600);
  mySerial.begin(4800);

  //ideas
  // Look at docs to see how to improve accuracy
  // only use samples that converged
  // look into whether samples are used twice or other issues with the uncoltrolled loop
  // allow more variability in the output of the sensor and average in the mcu
  // measure drift by connecting to the CNC machine to get precise moves

  count = 1;
}
void(* resetFunc) (void) = 0;
void loop()
{
  sens = sensor.readRangeContinuous();
  if (sens < 255)
  {
    if ( abs(sens - dist) > 10.0) {
      dist = sens;
    }
    dist = .995 * dist + .005 * sens;
  }
  count = count - 1;
  if (count == 0)
  {
    count = 1;
    //  Serial.print("\t Range: ");
  }

  {
    if (dist >= 40.00 && dist <= 80.00)
    {
      digitalWrite (LED, HIGH);
      currentMillis = millis();
      Serial.println(dist);
    }

    if (dist < 40) {
      Serial.println("distance <40");
    }
    else if (dist >= 80) {
      digitalWrite (LED, LOW);
      Serial.print("\t Duration secs   ");
      Serial.print("\t");
      duration = (currentMillis / 1000ul);
      Serial.print(duration);
      float duration;
      duration = (float)(currentMillis / 1000.0);
      mySerial.print(duration);
      Serial.println("\t Measurement Complete");
      delay(5000);
      resetFunc();
    }

  }
}
//

GolamMostafa:
You are transferring the value of 'duration' to destination. Have you thought that you are sending only the integer part of this: duration = currentMillis/1000ul?. The fractional part is not sent. In order to send the whole floating point number, place the following codes at the appropriate place of your transmit sketch.

float duration;

duration = (float)(currentMillis/1000.0);




Also bring necessary change in your receiver sketch.

.jpg file of 2 IDE's of 2 controllers included as modification in post1

Robin2:
Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example 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

I'm using soft Serial, tried using example 3, seem to be missing a lot, as i do not see any data coming onto 2nd controller

//Nano reciever
// read two ints from a Uno
// see Uno_transmitter.ino

#include <SoftwareSerial.h>
//unsigned long currentMillis;
//unsigned long duration;

SoftwareSerial mySerial(10, 11); // RX, TX

//void setup()
//{
//
//  Serial.begin(4800);
//  Serial.flush();
//  mySerial.begin(4800);
//  mySerial.flush();
//
//}
//
//void loop()
//{
//  {
//
//    //    if (mySerial.available() > 0) {
//    //}
//    //    if (mySerial.available()) {
//
//    while (Serial.available() > 0) {
//      Serial.read();
//    }
const byte numChars = 32;
char receivedChars[numChars];

boolean newData = false;

void setup() {
  mySerial.begin(9600);
  mySerial.println("<Receiver is ready>");
}

void loop() {
  recvWithStartEndMarkers();
  showNewData();
}

void recvWithStartEndMarkers() {
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '<';
  char endMarker = '>';
  char rc;

  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();

    if (recvInProgress == true) {
      if (rc != endMarker) {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) {
          ndx = numChars - 1;
        }
      }
      else {
        receivedChars[ndx] = '\0'; // terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;
      }
    }

    else if (rc == startMarker) {
      recvInProgress = true;
    }
  }
}

void showNewData() {
  if (newData == true) {
    Serial.print("This just in ... ");
    Serial.println(receivedChars);
    newData = false;
  }
}
//    Serial.println("Viscosity = ");
//    float duration;
//    duration = (float)(currentMillis / 1000.0);
//    mySerial.println(duration);

//delay(100);

Not all are born "GENIUS", hence I'm looking for support in this FORUM

No one is born genius, even the most gifted persons have to spend a lot of time to learn and train.

Genius is 1% inspiration and 99% transpiration.

At the receiver side, try the following codes and check that you see something in the Serial Monitor.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX

void setup()
{
 Serial.begin(9600);
 mySerial.begin(4800);
}

void loop()
{
  byte n = mySerial.available():
  if(n !=0)
  {
       char x= mySerial.read();
       Serial.print(x);
  }
}

Looking at the program in Reply #5 it occurs to me that these lines

  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();

should be using mySerial in place of Serial

and ALSO in setup() you should have Serial.begin(9600); so that you can view the output from showNewData()

...R