sending data via RF 433MHz

Hello everyone, i hope you’re good and well!
I’m using:
-RF 433MHz emitter and receiver
-two arduino uno
-two PIR motion sensors
-2*16 LCD screen
sensors are used to count the number of people coming in and leaving a place, by substraction. that number is being sent via the RF module to the second arduino card, which is connected to a LCD screen that will show the result. this is the code:
Transmission part:

/*  
    Arduino with PIR motion sensor
    For complete project details, visit: http://RandomNerdTutorials.com/pirsensor
    Modified by Rui Santos based on PIR sensor by Limor Fried
*/
#include <RH_ASK.h>
#include <SPI.h> // Not actually used but needed to compile
RH_ASK driver;
int pfe=18;
char msg[10];


 
int led = 13;                // the pin that the LED is atteched to
int sensor = 3;// the pin that the sensor is atteched to
int sensor2 = 5;
int state = LOW;             // by default, no motion detected
int state2 = LOW; 
int val = 0;                 // variable to store the sensor status (value)
int val2 = 0;
int countpos=0;// counter for sensor1
int countneg=0; //counter for sensor two 
int countres=0; // result : what we are really after here. later on, this result will be sent by the RF transmitter

void setup() {
  pinMode(led, OUTPUT);      // initalize LED as an output
  pinMode(sensor, INPUT);    // initialize sensor as an input
  pinMode(sensor2, INPUT);
  Serial.begin(9600);        // initialize serial
 if (!driver.init())
         Serial.println("init failed");

}

void loop(){
  val = digitalRead(sensor);   // read sensor value
  if (val == HIGH) {           // check if the sensor is HIGH
    digitalWrite(led, HIGH);   // turn LED ON
    delay(100);                // delay 100 milliseconds 
    
    if (state == LOW) {
      Serial.println("Motion detected!"); 
      state = HIGH;       // update variable state to HIGH
    countpos=countpos+1;
    Serial.print("countpos="); 
    Serial.println(countpos);
    }
  } 
  else {
      digitalWrite(led, LOW); // turn LED OFF
      delay(200);             // delay 200 milliseconds 
      
      if (state == HIGH){
        Serial.println("Motion stopped!");
        state = LOW;       // update variable state to LOW
    }
    }

    // SECOD SENSOR 
    val2 = digitalRead(sensor2);   // read sensor value
  if (val2 == HIGH) {           // check if the sensor is HIGH
    digitalWrite(led, HIGH);   // turn LED ON
    delay(100);                // delay 100 milliseconds 
    
    if (state2 == LOW) {
      Serial.println("Motion detected!"); 
      state2 = HIGH;       // update variable state to HIGH
    countneg=countneg+1;
        Serial.print("countneg"); 
Serial.println(countneg);
    }
  } 
  else {
      digitalWrite(led, LOW); // turn LED OFF
      delay(200);             // delay 200 milliseconds 
      
      if (state2 == HIGH){
        Serial.println("Motion stopped!");
        state2 = LOW;       // update variable state to LOW
    }
    }
    countres=countpos-countneg;
          delay(2000);             // delay 200 milliseconds 

        Serial.println("countres=");

    Serial.println(countres);
   

   // CODE EMETTEUR
const char *msg = "Hello World!";
    itoa(countres, msg, 10);  
    driver.send((uint8_t *)msg, strlen(msg));
    driver.waitPacketSent();
    delay(1000);
   
}

Receiver:

#include <LiquidCrystal.h>
#include <RH_ASK.h>
#include <SPI.h> // Not actualy used but needed to compile

RH_ASK driver;


char buf[12];
char ch;
int Contrast=15;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 10, 5, 4, 3, 2);

void setup() 
{
  Serial.begin(9600);
  Serial.println("LCD test with PWM contrast adjustment");
  pinMode(13,OUTPUT);
  analogWrite(6,Contrast);
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("LCD test!!");
//two lines receiver
   if (!driver.init())
         Serial.println("init failed");

}

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.
      Serial.print("Message: ");
      Serial.println((char*)buf);         
    }



   digitalWrite(13,LOW);
   delay(1000);
   digitalWrite(13,HIGH);
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print((char*)buf);









}

void serialEvent()
{
     if (Serial.available())
  {
    ch= Serial.read();
    if(ch=='A' && Contrast<255)
    {
      Contrast=Contrast+1;
    }
    if(ch=='B' && Contrast>0)
    {
      Contrast=Contrast-1;
    }
        if(ch=='N')
    {
      analogWrite(9,28836);
    }
       if(ch=='F')
    {
      analogWrite(9,0);
    }
    analogWrite(6,Contrast);
    Serial.println("Current contrast");
    Serial.println(Contrast);
  }
}

It it working perfectly, the numbers are being adjusted in real time while sensors detect movement. But i want to send another variable. For starters, i want to send a constant named to test it, can anyone help me try this?
PS: the previous code works just fine so i didn’t add the schemes, if you need them, please say so!
thanks

Start with the simple examples that come with the RadioHead library, and get them working to your satisfaction before adding anything to your PIR sensor code.

Hint:
Instead of this standard example:

   const char *msg = "Hello World!";
   driver.send((uint8_t *)msg, strlen(msg));

Try this:

    char buf[10];
    itoa(countres,buf, 10); 
    driver.send((uint8_t *)buf, strlen(buf));

Is yours like this one here?

jremington:
Start with the simple examples that come with the RadioHead library, and get them working to your satisfaction before adding anything to your PIR sensor code.

Hint:
Instead of this standard example:

   const char *msg = "Hello World!";

driver.send((uint8_t *)msg, strlen(msg));




Try this:


char buf[10];
    itoa(countres,buf, 10);
    driver.send((uint8_t *)buf, strlen(buf));

you are right! at first i started with a test value and it worked so i should try two and then add the rest of the code!
could you please explain what does change with that?

tjones9163:
Is yours like this one here?

Not exactly! mine look like the ones in this website:

I actually used that tutorial to get the information i need

There are lots of ways to send several numbers by radio, and you need to decide on a plan for exactly what you want to send.

The itoa() example I posted formats 1 integer value to a character array. To send more than one value as a character array, you can use the sprintf() function, like this example:

char buf[20];
sprintf(buf,"%d, %d",value1, value2); //int is assumed
driver.send((uint8_t *)buf, strlen(buf));

You can send any variable as binary, but you must receive it as a binary value.

int countres=1234;
driver.send((uint8_t *) &countres, 2);  //2 bytes for an int, note the "&"

Send an array of integers like this:

int values[2]={1234,5678};
driver.send((uint8_t *) values, 4);

The possibilities are limited only by your imagination.

jremington:
There are lots of ways to send several numbers by radio, and you need to decide on a plan for exactly what you want to send.

The itoa() example I posted formats 1 integer value to a character array. To send more than one value as a character array, you can use the sprintf() function, like this example:

char buf[20];

sprintf(buf,"%d, %d",value1, value2); //int is assumed
driver.send((uint8_t *)buf, strlen(buf));




You can send any variable as **binary**, but you must receive it as a binary value.


int countres=1234;
driver.send((uint8_t *) &countres, 2);  //2 bytes for an int, note the "&"




Send an array of integers like this:


int values[2]={1234,5678};
driver.send((uint8_t *) values, 4);




The possibilities are limited only by your imagination.

It is impressive what you could do with those! I actually thought about the first one but i couldn't succeed to retrieve both values (separately) at the reception

i couldn’t succeed to retrieve both values (separately) at the reception

One method would be to use the functions strtok() to split the string at “,” and atoi() on the substrings. Or use sscanf() on the entire message.

jremington:
One method would be to use the functions strtok() to split the string at "," and atoi() on the substrings. Or use sscanf() on the entire message.

That makes it easier! So when using sprintf, values "," will be automatically used as a delimiter, right?

Nothing is "automatic". Please study the documentation for sprintf() carefully.

That advice holds for ALL the string functions you intend to use.

jremington:
Nothing is "automatic". Please study the documentation for sprintf() carefully.

That advice holds for ALL the string functions you intend to use.

They seem new to me, yet they are useful. I will try to learn more about these functions.
Thank you for all your help

Hello everyone, i hope you’re doing well!
I’m working on a project and this is the final phase so i need advice.
Material:
-2 arduino boards
-2 PIR motion sensors
-RF 433 transmitter and receiver

  • 2*16 LCD screen

Objective:
the two sensors will detect people coming in and out of a place and then do subtraction and the result is “countres”. “countres” will be sent via the RF module (alongside another constant "const=15) to the other board where it will be shown on the screen.
I started step by step, finally it was working just fine, until i added some modifications using sprintf for the strings before sending them and changing the arrays size!
problem:
On the serial monitor on the transmitter it shows me that sensors are working and detecting, but the values on the screen(of the second board) change waay too long after the ones in the serial monitor of the first board change!
It is very confusing, and i couldn’t modify a thing, first, because it works but it is slow, second, i was hoping that someone would help me figure it out! maybe the delay time or a function is slowing the whole process.

screen should show:
val1 val2
15 #(countres:which is result of PIR sensors values)

i appreciate the help! Thank you.

transmitter side

/*  
    Arduino with PIR motion sensor
    For complete project details, visit: http://RandomNerdTutorials.com/pirsensor
    Modified by Rui Santos based on PIR sensor by Limor Fried
*/
#include <RH_ASK.h>
#include <SPI.h> // Not actually used but needed to compile
RH_ASK driver;

int const=17;
char msg[10];
char buff[8];



 
int led = 13;                // the pin that the LED is atteched to
int sensor = 3;// the pin that the sensor is atteched to
int sensor2 = 5;
int state = LOW;             // by default, no motion detected
int state2 = LOW; 
int val = 0;                 // variable to store the sensor status (value)
int val2 = 0;
int countpos=0;// counter for sensor1
int countneg=0; //counter for sensor two 
int countres=0; // result : what we are really after here. later on, this result will be sent by the RF transmitter

void setup() {
  pinMode(led, OUTPUT);      // initalize LED as an output
  pinMode(sensor, INPUT);    // initialize sensor as an input
  pinMode(sensor2, INPUT);
  Serial.begin(9600);        // initialize serial
 if (!driver.init())
         Serial.println("init failed");

}

void loop(){
  val = digitalRead(sensor);   // read sensor value
  if (val == HIGH) {           // check if the sensor is HIGH
    digitalWrite(led, HIGH);   // turn LED ON
    delay(100);                // delay 100 milliseconds 
    
    if (state == LOW) {
      Serial.println("Motion detected!"); 
      state = HIGH;       // update variable state to HIGH
    countpos=countpos+1;
    Serial.print("countpos="); 
    Serial.println(countpos);
    }
  } 
  else {
      digitalWrite(led, LOW); // turn LED OFF
      delay(200);             // delay 200 milliseconds 
      
      if (state == HIGH){
        Serial.println("Motion stopped!");
        state = LOW;       // update variable state to LOW
    }
    }

    // SECOD SENSOR 
    val2 = digitalRead(sensor2);   // read sensor value
  if (val2 == HIGH) {           // check if the sensor is HIGH
    digitalWrite(led, HIGH);   // turn LED ON
    delay(100);                // delay 100 milliseconds 
    
    if (state2 == LOW) {
      Serial.println("Motion detected!"); 
      state2 = HIGH;       // update variable state to HIGH
    countneg=countneg+1;
        Serial.print("countneg"); 
Serial.println(countneg);
    }
  } 
  else {
      digitalWrite(led, LOW); // turn LED OFF
      delay(200);             // delay 200 milliseconds 
      
      if (state2 == HIGH){
        Serial.println("Motion stopped!");
        state2 = LOW;       // update variable state to LOW
    }
    }
    countres=countpos-countneg;
          delay(2000);             // delay 200 milliseconds 

        Serial.println("countres=");

    Serial.println(countres);
   

   // CODE EMETTEUR
        char buff[8];

const char *msg = "Hello World!";

sprintf(buff,"%d, %d",const, countres); //int is assumed   
driver.send((uint8_t *)buff, strlen(buff));
    driver.waitPacketSent();
    

  for(int i = 0; i < 8; i++)
{
 
Serial.print(buff[i]);

}
    delay(100);
   
}

receiver side

#include <LiquidCrystal.h>
#include <RH_ASK.h>
#include <SPI.h> // Not actualy used but needed to compile

RH_ASK driver;


char buf[8];
char ch;
int Contrast=15;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 10, 5, 4, 3, 2);

void setup() 
{
  Serial.begin(9600);
  Serial.println("LCD test with PWM contrast adjustment");
  pinMode(13,OUTPUT);
  analogWrite(6,Contrast);
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
    lcd.setCursor(0, 0);
  lcd.print("val1");
     lcd.setCursor(10, 0);
  lcd.print("val2");
//two lines receiver
   if (!driver.init())
         Serial.println("init failed");

}

void loop() 
{


  
uint8_t buf[8];
    uint8_t buflen = sizeof(buf);
    if (driver.recv(buf, &buflen)) // Non-blocking
    {
      int i;
      // Message with a good checksum received, dump it.
      Serial.print("Message: ");
      Serial.println((char*)buf);         
    }



   digitalWrite(13,LOW);
   delay(100);
   digitalWrite(13,HIGH);
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print((char*)buf);


}

void serialEvent()
{
     if (Serial.available())
  {
    ch= Serial.read();
    if(ch=='A' && Contrast<255)
    {
      Contrast=Contrast+1;
    }
    if(ch=='B' && Contrast>0)
    {
      Contrast=Contrast-1;
    }
        if(ch=='N')
    {
      analogWrite(9,28836);
    }
       if(ch=='F')
    {
      analogWrite(9,0);
    }
    analogWrite(6,Contrast);
    Serial.println("Current contrast");
    Serial.println(Contrast);
  }
}

Do not double post, as it wastes everyone's time. Instead, continue your previous thread on this topic.

jremington:
Do not double post, as it wastes everyone's time. Instead, continue your previous thread on this topic.

if that's the way things work, i will
thank you!

I managed to solve that and it is working, thanks to all of your help! However, it became slow, meaning: results on the LCD are being printed and updated way to long after being printed on the serial monitor of the transmitter board. I thought it was a delay time problem maybe but i couldn’t solve it!

transmitter side

/* 
    Arduino with PIR motion sensor
    For complete project details, visit: http://RandomNerdTutorials.com/pirsensor
    Modified by Rui Santos based on PIR sensor by Limor Fried
*/
#include <RH_ASK.h>
#include <SPI.h> // Not actually used but needed to compile
RH_ASK driver;

int const=17;
char msg[10];
char buff[8];



 
int led = 13;                // the pin that the LED is atteched to
int sensor = 3;// the pin that the sensor is atteched to
int sensor2 = 5;
int state = LOW;             // by default, no motion detected
int state2 = LOW;
int val = 0;                 // variable to store the sensor status (value)
int val2 = 0;
int countpos=0;// counter for sensor1
int countneg=0; //counter for sensor two
int countres=0; // result : what we are really after here. later on, this result will be sent by the RF transmitter

void setup() {
  pinMode(led, OUTPUT);      // initalize LED as an output
  pinMode(sensor, INPUT);    // initialize sensor as an input
  pinMode(sensor2, INPUT);
  Serial.begin(9600);        // initialize serial
 if (!driver.init())
         Serial.println("init failed");

}

void loop(){
  val = digitalRead(sensor);   // read sensor value
  if (val == HIGH) {           // check if the sensor is HIGH
    digitalWrite(led, HIGH);   // turn LED ON
    delay(100);                // delay 100 milliseconds
   
    if (state == LOW) {
      Serial.println("Motion detected!");
      state = HIGH;       // update variable state to HIGH
    countpos=countpos+1;
    Serial.print("countpos=");
    Serial.println(countpos);
    }
  }
  else {
      digitalWrite(led, LOW); // turn LED OFF
      delay(200);             // delay 200 milliseconds
     
      if (state == HIGH){
        Serial.println("Motion stopped!");
        state = LOW;       // update variable state to LOW
    }
    }

    // SECOD SENSOR
    val2 = digitalRead(sensor2);   // read sensor value
  if (val2 == HIGH) {           // check if the sensor is HIGH
    digitalWrite(led, HIGH);   // turn LED ON
    delay(100);                // delay 100 milliseconds
   
    if (state2 == LOW) {
      Serial.println("Motion detected!");
      state2 = HIGH;       // update variable state to HIGH
    countneg=countneg+1;
        Serial.print("countneg");
Serial.println(countneg);
    }
  }
  else {
      digitalWrite(led, LOW); // turn LED OFF
      delay(200);             // delay 200 milliseconds
     
      if (state2 == HIGH){
        Serial.println("Motion stopped!");
        state2 = LOW;       // update variable state to LOW
    }
    }
    countres=countpos-countneg;
          delay(2000);             // delay 200 milliseconds

        Serial.println("countres=");

    Serial.println(countres);
   

   // CODE EMETTEUR
        char buff[8];

const char *msg = "Hello World!";

sprintf(buff,"%d, %d",const, countres); //int is assumed   
driver.send((uint8_t *)buff, strlen(buff));
    driver.waitPacketSent();
   

  for(int i = 0; i < 8; i++)
{
 
Serial.print(buff[i]);

}
    delay(100);
   
}

Receiver side

/* 
    Arduino with PIR motion sensor
    For complete project details, visit: http://RandomNerdTutorials.com/pirsensor
    Modified by Rui Santos based on PIR sensor by Limor Fried
*/
#include <RH_ASK.h>
#include <SPI.h> // Not actually used but needed to compile
RH_ASK driver;

int const=17;
char msg[10];
char buff[8];



 
int led = 13;                // the pin that the LED is atteched to
int sensor = 3;// the pin that the sensor is atteched to
int sensor2 = 5;
int state = LOW;             // by default, no motion detected
int state2 = LOW;
int val = 0;                 // variable to store the sensor status (value)
int val2 = 0;
int countpos=0;// counter for sensor1
int countneg=0; //counter for sensor two
int countres=0; // result : what we are really after here. later on, this result will be sent by the RF transmitter

void setup() {
  pinMode(led, OUTPUT);      // initalize LED as an output
  pinMode(sensor, INPUT);    // initialize sensor as an input
  pinMode(sensor2, INPUT);
  Serial.begin(9600);        // initialize serial
 if (!driver.init())
         Serial.println("init failed");

}

void loop(){
  val = digitalRead(sensor);   // read sensor value
  if (val == HIGH) {           // check if the sensor is HIGH
    digitalWrite(led, HIGH);   // turn LED ON
    delay(100);                // delay 100 milliseconds
   
    if (state == LOW) {
      Serial.println("Motion detected!");
      state = HIGH;       // update variable state to HIGH
    countpos=countpos+1;
    Serial.print("countpos=");
    Serial.println(countpos);
    }
  }
  else {
      digitalWrite(led, LOW); // turn LED OFF
      delay(200);             // delay 200 milliseconds
     
      if (state == HIGH){
        Serial.println("Motion stopped!");
        state = LOW;       // update variable state to LOW
    }
    }

    // SECOD SENSOR
    val2 = digitalRead(sensor2);   // read sensor value
  if (val2 == HIGH) {           // check if the sensor is HIGH
    digitalWrite(led, HIGH);   // turn LED ON
    delay(100);                // delay 100 milliseconds
   
    if (state2 == LOW) {
      Serial.println("Motion detected!");
      state2 = HIGH;       // update variable state to HIGH
    countneg=countneg+1;
        Serial.print("countneg");
Serial.println(countneg);
    }
  }
  else {
      digitalWrite(led, LOW); // turn LED OFF
      delay(200);             // delay 200 milliseconds
     
      if (state2 == HIGH){
        Serial.println("Motion stopped!");
        state2 = LOW;       // update variable state to LOW
    }
    }
    countres=countpos-countneg;
          delay(2000);             // delay 200 milliseconds

        Serial.println("countres=");

    Serial.println(countres);
   

   // CODE EMETTEUR
        char buff[8];

const char *msg = "Hello World!";

sprintf(buff,"%d, %d",const, countres); //int is assumed   
driver.send((uint8_t *)buff, strlen(buff));
    driver.waitPacketSent();
   

  for(int i = 0; i < 8; i++)
{
 
Serial.print(buff[i]);

}
    delay(100);
   
}

I appreciate the help!
Also what is the best a website/software that you use to create the circuits, please tell me, i want to make one so i could share my project with others!
Thank you!

Don't cross post!!
Threads merged

Get rid of the delays. The following does nothing for TWO SECONDS.

         delay(2000);             // delay 200 milliseconds

Replace the "8" below by 13 or larger (2x5 digits, comma, blank and zero terminator). You were illegally overwriting memory, which leads to crashes and program malfunction.

        char buff[8];

update: reduced the time of some of the delay and it is faster, a code optimization might make it go even more smoothly.
Additional question: the incrementation from the pir are being updated in a weird manner like the following:
0
1
11
21
31
so is the decrementation. it's like each number pushes the previous one. Still working on it