Hi all,
Currently I'm building a wireless distance measuring unit, using nRF24L01.
It is working quite ok, but... At this moment the data is transferred in centimeter to the receiver.
I would like to have this in meter, so devide by 100 in my receiver code. My value is now rounded of, no decimals. f.e. if I measure 331cm, should be 3.31m, but I get 3.00m.
How do I need to fix this?
The receiver code:
#include "Arduino.h"
#include <SPI.h>
#include <RF24.h>
#include <Wire.h>
#include <LIDARLite.h>
#include <SPI.h>
#include "RF24.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH 16
static const unsigned char PROGMEM logo16_glcd_bmp[] =
{ B00000000, B11000000,
B00000001, B11000000,
B00000001, B11000000,
B00000011, B11100000,
B11110011, B11100000,
B11111110, B11111000,
B01111110, B11111111,
B00110011, B10011111,
B00011111, B11111100,
B00001101, B01110000,
B00011011, B10100000,
B00111111, B11100000,
B00111111, B11110000,
B01111100, B11110000,
B01110000, B01110000,
B00000000, B00110000 };
#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
// This is just the way the RF24 library works:
// Hardware configuration: Set up nRF24L01 radio on SPI bus (pins 10, 11, 12, 13) plus pins 7 & 8
RF24 radio(7, 8);
byte addresses[][6] = {"1Node","2Node"};
float dataInMeter;
// -----------------------------------------------------------------------------
// SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP
// -----------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
Serial.println("THIS IS THE RECEIVER MODULE - YOU NEED THE OTHER ARDUINO TO TRANSMIT");
// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32)
// init done
// Show image buffer on the display hardware.
// Since the buffer is intialized with an Adafruit splashscreen
// internally, this will display the splashscreen.
// Clear the buffer.
display.clearDisplay();
// text display tests
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(20,0);
display.println("Allskies");
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(20,21);
display.println("RPAS Engineering");
display.display();
delay(2000);
display.clearDisplay();
// Initiate the radio object
radio.begin();
// Set the transmit power to lowest available to prevent power supply related issues
radio.setPALevel(RF24_PA_MAX);
// Set the speed of the transmission to the quickest available
radio.setDataRate(RF24_2MBPS);
// Use a channel unlikely to be used by Wifi, Microwave ovens etc
radio.setChannel(124);
// Open a writing and reading pipe on each radio, with opposite addresses
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1, addresses[1]);
// Start the radio listening for data
radio.startListening();
}
// -----------------------------------------------------------------------------
// We are LISTENING on this device only (although we do transmit a response)
// -----------------------------------------------------------------------------
void loop() {
// This is what we receive from the other device (the transmitter)
unsigned char data;
// Is there any data for us to get?
if ( radio.available()) {
// Go and read the data and put it into that variable
while (radio.available()) {
radio.read( &data, sizeof(char));
}
// No more data to get so send it back but add 1 first just for kicks
// First, stop listening so we can talk
radio.stopListening();
data;
radio.write( &data, sizeof(char) );
// Now, resume listening so we catch the next packets.
radio.startListening();
// Tell the user what we sent back (the random numer + 1)
Serial.print("Sent response ");
Serial.println(data);
dataInMeter = (data/100);
Serial.println(dataInMeter);
}
display.setTextSize(3.5);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.print(dataInMeter);
display.println(" m");
display.display();
delay(10);
display.clearDisplay();
}