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