Need help with a wireless motion detector code

Hi everyone,

I’m making a wireless motion detector. I’m using a SR04 ultrasound sensor and a nRF24L01 module.

I’m talking here only about the transmitter code. I already got one version of the code working, but the sensor values were very unaccurate. Then I read about the Newping library. I found some example codes of the library online, but I haven’t got them working with the wireless module.

I think I know where my problem is here. I can’t put the piece of code that sends the signal to the other nRF24L01 module in the right place. This part:

if (digitalRead(SW1) == HIGH){
msg[0] = 111;
radio.write(msg, 1);}

Now I have two “IF”-statements back to back, is that the problem?

Here is the full code:

#include <NewPing.h>
#define TRIGGER_PIN 7 
#define ECHO_PIN 6    

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
int msg[1];
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int SW1 = 4;
int led = 5;

NewPing sonar(TRIGGER_PIN, ECHO_PIN);

void setup() {
  Serial.begin(115200);
  radio.begin();
  radio.openWritingPipe(pipe);}

void loop() {
  delay(50);
  unsigned int ultrSonic = sonar.ping();

  if ((ultrSonic / US_ROUNDTRIP_CM) < 50) {
    digitalWrite(led,HIGH);
    }else {
    digitalWrite(led,LOW);}
    
 if (digitalRead(SW1) == HIGH){
msg[0] = 111;
radio.write(msg, 1);}

unsigned int uS = sonar.ping();
Serial.print("Ping: ");h
  Serial.print(uS / US_ROUNDTRIP_CM);
  Serial.println("cm");}

Any suggestions would be appreciated.

I would like to recommend that you take just a little time and read up a bit on coding styles. Find one you like and stick with it. Some thing like this:

#include <NewPing.h>
#define TRIGGER_PIN 7
#define ECHO_PIN 6    

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

int msg[1];
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int SW1 = 4;
int led = 5;

NewPing sonar(TRIGGER_PIN, ECHO_PIN);

void setup() 
{
	Serial.begin(115200);
	radio.begin();
	radio.openWritingPipe(pipe);

}

void loop() 
{
	delay(50); //50 millisecond delay
	unsigned int ultrSonic = sonar.ping();

	if ((ultrSonic / US_ROUNDTRIP_CM) < 50) 
	{
		digitalWrite(led,HIGH);
	}
	else 
	{
		digitalWrite(led,LOW);
	}
	
	if (digitalRead(SW1) == HIGH)
	{
		msg[0] = 111;
		radio.write(msg, 1);
	}

	unsigned int uS = sonar.ping();
	Serial.print("Ping: ");h
	Serial.print(uS / US_ROUNDTRIP_CM);
	Serial.println("cm");
}

It makes your code easier to read/visualize and often syntax errors leap out at you. Such is not the case however, with your snippet. Posting your whole code is useful if not necessary. It’s very difficult to tell what’s wrong with a car’s engine by looking at the paint job.

int msg[1];

msg is an array containing 1 int. How many bytes will that use ?
Answer, 2

    msg[0] = 111;
    radio.write(msg, 1);

How many bytes are you sending ?

In any case, do you need to use an array, especially as it is only holding one entry ?

DKWatson:
Such is nit the case however, with your snippet. Posting your whole code is useful if not necessary. It's very difficult to tell what's wrong with a car's engine by looking at the paint job.

I don't know what you mean by snippet, that's the full code of the transmitter.

If you take more trouble with the layout of your code (use the AutoFormat tool) it will be much easier to see the different elements. Like this

#include <NewPing.h>
#define TRIGGER_PIN 7 
#define ECHO_PIN 6    

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
int msg[1];
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int SW1 = 4;
int led = 5;

NewPing sonar(TRIGGER_PIN, ECHO_PIN);

void setup() {
    Serial.begin(115200);
    radio.begin();
    radio.openWritingPipe(pipe);
}

void loop() {
    delay(50);
    unsigned int ultrSonic = sonar.ping();

    if ((ultrSonic / US_ROUNDTRIP_CM) < 50) {
        digitalWrite(led,HIGH);
    }
    else {
        digitalWrite(led,LOW);
    }
        
    if (digitalRead(SW1) == HIGH){
        msg[0] = 111;
        radio.write(msg, 1);
    }

    unsigned int uS = sonar.ping();
    Serial.print("Ping: ");h
    Serial.print(uS / US_ROUNDTRIP_CM);
    Serial.println("cm");
}

You seem to be reading the sonar sensor in line 24 but you are not using that information when sending the wireless message. The wireless message seems to depend on some switch.

Then you make another reading of the sonar sensor on line 38 and print that value. It may be quite different from the value obtained in line 24.

A wireless program cannot exist in isolation. It only makes sense in conjunction with the receiving program which you have not posted.

Have a look at this Simple nRF24L01+ Tutorial.

Wireless problems can be very difficult to debug so get the wireless part working on its own before you start adding any other features.

…R

Where does US_ROUNDTRIP_CM come from?