I'm having trouble getting a DFRobot Lidar distance sensor (connected to Arduino UNO) to transmit its readings through an RF transmitter to an LCD Display (connected to an Arduino Nano).
When I code the Arduino Uno to read the distance sensor, it shows the readings fine on the serial monitor. And when I code the Arduino Uno only to send a set number to the LCD display on the Nano through the RF transmitter-receiver, that also works fine. But when I combine both pieces of codes nothing happens. No more serial monitor readings and no sending of any info to the LCD display.
Can anyone help me out? The code I used on the Uno is below. The code on the Nano should be ok.
#include"DFRobot_LIDAR07.h"
#include <Wire.h>
// Include RadioHead Amplitude Shift Keying Library
#include <RH_ASK.h>
// Include dependant SPI Library
#include <SPI.h>
// Create Amplitude Shift Keying Object
RH_ASK rf_driver;
//If using IIC mode, please enable macro USE_IIC
#define USE_IIC
#ifdef USE_IIC
DFRobot_LIDAR07_IIC LIDAR07;
#endif
float afstand;
char msg[6];
void setup() {
// Initialize ASK Object
rf_driver.init();
uint32_t version;
Serial.begin(9600);
while(!LIDAR07.begin()){
Serial.println("The sensor returned data validation error");
delay(1000);
}
version = LIDAR07.getVersion();
Serial.print("VERSION: ");
Serial.print((version>>24)&0xFF,HEX);
Serial.print(".");Serial.print((version>>16)&0xFF,HEX);
Serial.print(".");Serial.print((version>>8)&0xFF,HEX);
Serial.print(".");Serial.println((version)&0xFF,HEX);
//After enabling the filter, it can be stopped by calling LIDAR07.stopFilter()
while(!LIDAR07.startFilter());
/**
* @brief Configure the sensor to single acquisition mode
* @param mode The way data are collected
* @n eLidar07Single A single collection
* @n eLidar07Continuous Continuous acquisition
* @return true (Successful) , false (Failed)
*/
while(!LIDAR07.setMeasureMode(LIDAR07.eLidar07Single)){
Serial.println("set measure mode err");
//Open measurement (in single measurement mode, it will automatically close after sampling).To stop collection, use stopMeasure()
LIDAR07.startMeasure();
}
}
void loop() {
//Open measurement (in single measurement mode, it will automatically close after sampling),To stop collection, use stopMeasure()
LIDAR07.startMeasure();
//Get the collected data
if(LIDAR07.getValue()){
Serial.print(LIDAR07.getDistanceMM());
afstand = LIDAR07.getDistanceMM();
dtostrf (afstand, 6, 2, msg); //converts the float into a char
rf_driver.send((uint8_t *)msg, strlen(msg));
rf_driver.waitPacketSent();
delay(1000);
}
}
I think you have a problem here. You are telling the dtostrf() function you want 6 digits with 2 of them to the right of the decimal. That is 6 digits plus a decimal point plus a nul character which is a total of 8 characters. You only have msg declared with a length of 6. This means you are writing past the end of the msg buffer and corrupting other memory. The effects of memory corruption are often very strange behavior.
void loop() {
//Get the collected data
if (LIDAR07.getValue()) {
Serial.print(LIDAR07.getDistanceMM());
dtostrf (LIDAR07.getDistanceMM(), 4, 0, msg);
} //converts the readout into a char
rf_driver.send((uint8_t *)msg, strlen(msg));
rf_driver.waitPacketSent();
delay(1000);
}
I tried to identify what is causing the readout sequence of the sensor to stop, by eliminating the RF related code one by one. To my amazement, the readout (Serialprint) doesn't work if any code related to the RF transmission is included. Even if I only include the loading of the library on top
// Include RadioHead Amplitude Shift Keying Library
#include <RH_ASK.h>
However, additional piece of information that might be useful: when running only the code related to the sensor (which works) I get a warning that 75% of the memory is being used. When loading the RH_ASK.h library, the warning jumps to 95% of the memory. This same warning (95%) also shows when all the RF related code is present. Could this be the reason?
Thanks. Now, I've figured out that one can use PROGMEM to shift variables to the flash memory. But I'm a bit lost what the variables are that I want to shift?
Or have I lost the trail and should I use something else?
Well there is no large tables of values that will save you much that I can see in your code, so there is little scope for moving stuff into program memory. Note that it is only constants and not variables that you can shift into program memory.
Likewise using the F() macro on your strings doesn't save you a significant amount either because you have very little in the way of strings in your print statement.