Hello. I am programming an app on an Arduino with a distance sensor. I am sending the distance via 433 mhz HC-SR04 transmitter. I need to convert unsigned int to const char* so I can send it. I am using the following code.
unsigned int distance;
String str0= String(int(distance));
String a = str0;
char b[10];
a.toCharArray(b,10);
const char *msg1 = b;
driver.send((uint8_t *)msg1, strlen(msg1));
driver.waitPacketSent();
It does sort of work but I don't think that I converts properly because the numbers that I am reading off of the distance sensor make no sense. Does anyone have experience with this? Thanks alot.
Please post ALL the code you are actually using -- the posted fragment is not an Arduino program, and won't compile. Avoid Strings.
You do not need to convert data to text in order to send it by radio. You can send an integer in binary, like this:
int result = 12345; //an example
driver.send((uint8_t *)&result, 2); //two byte integer on Uno. Note the "&", required
driver.waitPacketSent();
You can receive the binary data in a similar fashion.
the numbers that I am reading off of the distance sensor make no sense
You need to fix that separate problem first.
Hello. I tried what you told me and it didn't work so now I am going to upload all of the code that I am using. The output message that I receive is blank.
receiver end code
#include <LiquidCrystal.h> // includes the LiquidCrystal Library
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);// Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7)
#include <RH_ASK.h>
#include <SPI.h> // Not actualy used but needed to compile
RH_ASK driver;
#define trigger_pin 15 // HC-SR04 trigger pin is connected to Arduino A1
#define echo_pin 14
int ledPin = 13; // choose the pin for the LED
int inputPin = 9; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
void setup()
{
pinMode(ledPin, OUTPUT); // declare LED as output
digitalWrite(ledPin, LOW);
pinMode(trigger_pin, OUTPUT);
pinMode(echo_pin, INPUT);
digitalWrite(trigger_pin, LOW);
Serial.begin(9600); // Debugging only
if (!driver.init())
Serial.println("init failed");
lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display }
lcd.print("Start"); // Prints "Arduino" on the LCD
}
unsigned long duration;
unsigned int distance;
void loop()
{
uint8_t buf[12];
uint8_t buflen = sizeof(buf);
if (driver.recv(buf, &buflen)) // Non-blocking
{
int i;
// Message with a good checksum received, dump it.
digitalWrite(ledPin, HIGH);
lcd.setCursor(2, 1);
lcd.print("Message:");
lcd.setCursor(5, 1);
lcd.print((char*)buf);
}
}
transmitter end
#include <RH_ASK.h>
#include <SPI.h> // Not actually used but needed to compile
#include <string.h>
RH_ASK driver;
#define trigger_pin 15 // HC-SR04 trigger pin is connected to Arduino A1
#define echo_pin 14 // HC-SR04 echo pin is connected to Arduino A0
void setup()
{
pinMode(trigger_pin, OUTPUT);
pinMode(echo_pin, INPUT);
digitalWrite(trigger_pin, LOW);
// Serial.begin(9600); // Debugging only
if (!driver.init())
Serial.println("init failed");
}
unsigned long duration;
unsigned int distance;
void loop()
{
// Send 10us pulse to HC-SR04 Trigger pin
digitalWrite(trigger_pin, HIGH);
delayMicroseconds(10);
digitalWrite(trigger_pin, LOW);
// Read pulse comes from HC-SR04 Echo pin
duration = pulseIn(echo_pin, HIGH); // Read echo pulse width
if(duration == 0) { // Previous function returns 0 if timeout
}
else {
distance = duration / 58; // Calculate the distance in cm
if(distance > 400) { // HC-SR04 module can measure up to 400cm
}
else {
int d = (int)distance;
driver.send((uint8_t *)d, 2);
driver.waitPacketSent();
delay(1000);
}
}
delay(100);
}
The message I receive kinda flickers a bit and shows some Chinese letters. Thanks for the help.
Multiple things wrong here:
- You're not properly providing a pointer to the data you want to send. Use:
driver.send((uint8_t *)&d, sizeof(d));
lcd.print((char*)buf);
There are probably other problems. But, as a start, fix those.
It really does help to pay attention to details. For example, check out the comment at the end of this line:
driver.send((uint8_t *)&result, 2); //two byte integer on Uno. Note the "&", required
The receive code could look like this:
int received_value = 0;
uint8_t buflen = 2; //maximum allowed message length
if (driver.recv((uint8_t *)&received_value, &buflen)) // DON"T FORGET THE "&"
{
if (buflen == 2) Serial.print("Got: ");
Serial.println(received_value);
}
& is used as the "memory address of variable" operator, and is required.
Hello. Thanks for the help guys I just tried out this code
receive
int received_value = 0;
uint8_t buflen = 2; //maximum allowed message length
if (driver.recv((uint8_t *)&received_value, &buflen)) // DON"T FORGET THE "&"
{
if (buflen == 2) Serial.print("Got: ");
// Message with a good checksum received, dump it.
digitalWrite(ledPin, HIGH);
lcd.setCursor(2, 1);
lcd.print("Message:");
lcd.setCursor(5, 1);
lcd.print((char*)received_value);
}
transmitter
// Send 10us pulse to HC-SR04 Trigger pin
digitalWrite(trigger_pin, HIGH);
delayMicroseconds(10);
digitalWrite(trigger_pin, LOW);
// Read pulse comes from HC-SR04 Echo pin
duration = pulseIn(echo_pin, HIGH); // Read echo pulse width
if(duration == 0) { // Previous function returns 0 if timeout
}
else {
distance = duration / 58; // Calculate the distance in cm
if(distance > 400) { // HC-SR04 module can measure up to 400cm
}
else {
driver.send((uint8_t *)&distance, 2);
driver.waitPacketSent();
delay(1000);
}
}
delay(100);
On the receive end lcd screen I see this Message: then it shows Chinese letters when I put my hand over the sensor. Thanks for the help.
You seem to have great difficulty paying attention.
This WILL NOT WORK, because received_value is a binary integer value. It is NOT a pointer to a character string.
lcd.print((char*)received_value);
Oh I get it thanks so remove the (char*) and replaced it with (String) received_value = distance. Thanks . It was working but something wierd occurred after. All my lcd characters are straight white now. I checked all connections played with the variable resistor checked my code. I cannot see what would do that. Does anyone know what makes an LCDs characters all white? thanks.
replaced it with (String)
Wrong again.
Good luck with your project.