I'm experimenting with it because it does not work in my project.
I'm not going to post the more than 600 lines for obvious reasons (i'm tired of discussing that topic).
Instead i'm going to post the very relevant and complete experimenting code in the three steps i'v tried (to find out why it doesn't work).
It is a attempt to convert an example sketch for the waterproof A02YYUW (UART) distance sensor to a function in my sketch. I'm using this library instead of writing/copying a piece of code because this lib is the only one that generates almost no errors.
The goal is to get the unsigned int 'dis' (distance) as a global variable in cm (the library 'DistanceSensor_A02YYUW' gives distance in mm; hence division by 10).
And yes, i know many of you would 'return' the value from the function but i want it globally defined so that it's more accessible for the other functions in my project code.
Why won't the distilled version of the sketch work like it should?
Step one: sketch that works (note: you see i'v changed the 'do/while' in a 'while' loop)
/*
*
* Gets the distance from the sensor using a Software Serial every 1 second
*
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DistanceSensor_A02YYUW.h>
#include <SoftwareSerial.h>
#define SOFTWARE_SERIAL_PIN_RX 11 //this is where the tx (white wire) of the sensor goes!!
#define SOFTWARE_SERIAL_PIN_TX 10
SoftwareSerial mySerial(SOFTWARE_SERIAL_PIN_RX, SOFTWARE_SERIAL_PIN_TX);
DistanceSensor_A02YYUW distanceSensor(&mySerial);
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
mySerial.begin(9600);
Serial.begin(9600);
// initialize the LCD
lcd.begin();
}
void loop() {
DistanceSensor_A02YYUW_MEASSUREMENT_STATUS meassurementStatus;
unsigned int dis = 0;
// Gets the distance from the sensor and if the measurement is wrong, it retries to get the distance
while (meassurementStatus != DistanceSensor_A02YYUW_MEASSUREMENT_STATUS_OK) {
lcd.backlight();
meassurementStatus = distanceSensor.meassure();
if (meassurementStatus == DistanceSensor_A02YYUW_MEASSUREMENT_STATUS_OK) {
lcd.clear();
lcd.setCursor(0, 0);
dis = distanceSensor.getDistance()/10;
lcd.print(dis);
lcd.print(" cm");
//Serial.print("Distance : ");
//Serial.print(distanceSensor.getDistance()/10);
//Serial.println(" cm");
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Error:");
lcd.print(meassurementStatus);
//Serial.print("Error:" );
//Serial.println(meassurementStatus);
}
}
/*// Gets the distance from the sensor and if the measurement is wrong, it retries to get the distance
do {
lcd.backlight();
meassurementStatus = distanceSensor.meassure();
if (meassurementStatus == DistanceSensor_A02YYUW_MEASSUREMENT_STATUS_OK) {
lcd.clear();
lcd.setCursor(0, 0);
dis = distanceSensor.getDistance()/10;
lcd.print(dis);
lcd.print(" cm");
//Serial.print("Distance : ");
//Serial.print(distanceSensor.getDistance()/10);
//Serial.println(" cm");
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Error:");
lcd.print(meassurementStatus);
//Serial.print("Error:" );
//Serial.println(meassurementStatus);
}
} while (meassurementStatus != DistanceSensor_A02YYUW_MEASSUREMENT_STATUS_OK);
*/
delay(1000);
}
Step two: The 'split-off' to a void function and unsigned int dis = 0; as global variable works
/*
*
* Gets the distance from the sensor using a Software Serial every 1 second
*
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DistanceSensor_A02YYUW.h>
#include <SoftwareSerial.h>
#define SOFTWARE_SERIAL_PIN_RX 11 //this is where the tx (white wire) of the sensor goes!!
#define SOFTWARE_SERIAL_PIN_TX 10
unsigned int dis = 0;
SoftwareSerial mySerial(SOFTWARE_SERIAL_PIN_RX, SOFTWARE_SERIAL_PIN_TX);
DistanceSensor_A02YYUW distanceSensor(&mySerial);
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
mySerial.begin(9600);
Serial.begin(9600);
// initialize the LCD
lcd.begin();
}
void loop() {
distance ();
}
void distance() {
DistanceSensor_A02YYUW_MEASSUREMENT_STATUS meassurementStatus;
while (meassurementStatus != DistanceSensor_A02YYUW_MEASSUREMENT_STATUS_OK) {
lcd.backlight();
meassurementStatus = distanceSensor.meassure();
if (meassurementStatus == DistanceSensor_A02YYUW_MEASSUREMENT_STATUS_OK) {
lcd.clear();
lcd.setCursor(0, 0);
dis = distanceSensor.getDistance()/10;
lcd.print(dis);
lcd.print(" cm");
//Serial.print("Distance : ");
//Serial.print(distanceSensor.getDistance()/10);
//Serial.println(" cm");
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Error:");
lcd.print(meassurementStatus);
//Serial.print("Error:" );
//Serial.println(meassurementStatus);
}
}
delay(1000);
}
Step three: Cutting away the printing balast in the void distance(); function and adding some printing balast to the loop() function doens't work (displays the variable 'dis' is 0 cm)
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DistanceSensor_A02YYUW.h>
#include <SoftwareSerial.h>
#define SOFTWARE_SERIAL_PIN_RX 11 //this is where the tx (white wire) of the sensor goes!!
#define SOFTWARE_SERIAL_PIN_TX 10
unsigned int dis = 0;
SoftwareSerial mySerial(SOFTWARE_SERIAL_PIN_RX, SOFTWARE_SERIAL_PIN_TX);
DistanceSensor_A02YYUW distanceSensor(&mySerial);
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
mySerial.begin(9600);
Serial.begin(9600);
// initialize the LCD
lcd.begin();
}
void loop() {
distance ();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(dis);
lcd.print(" cm");
}
void distance() {
DistanceSensor_A02YYUW_MEASSUREMENT_STATUS meassurementStatus;
while (meassurementStatus != DistanceSensor_A02YYUW_MEASSUREMENT_STATUS_OK) {
lcd.backlight();
meassurementStatus = distanceSensor.meassure();
if (meassurementStatus == DistanceSensor_A02YYUW_MEASSUREMENT_STATUS_OK) {
dis = distanceSensor.getDistance()/10; //dis=global variable
}
}
delay(1000);
}
To be honest i, i don't exactly know what DistanceSensor_A02YYUW_MEASSUREMENT_STATUS meassurementStatus; does but i assume this is a variable created in the lib that gets renamed. Although i can't point my finger to, the trouble is mostly caused by something i don't really understand.
My apologies but i'm still having trouble naming everything with the right terms.
thx!