problem with the hc-sr04 with nrf240l01
when tried combine ultrasonic, output serial monitor zero. either problem coding.?for tx code
and here rx code
code: [select]
#include <spi.h>
#include <mirf.h>
#include <nrf24l01.h>
#include <mirfhardwarespidriver.h>
#define trigpin 5
#define echopin 4
int rate, duration;
long previousmillis = 0;
long interval = 1000;
void setup(){
serial.begin(9600);
mirf.spi = &mirfhardwarespi;
mirf.init();
mirf.setraddr((byte *)"clie1");
mirf.payload = sizeof(rate);
mirf.config();
}
void loop(){
unsigned long currentmillis = millis();
if(currentmillis - previousmillis > interval) {
previousmillis = currentmillis;
digitalwrite (trigpin, high);
delaymicroseconds (1000);
digitalwrite (trigpin, low);
duration = pulsein (echopin, low);
rate = (duration/2) / 29.1;
serial.print(rate);
delay (500) ;
mirf.settaddr((byte *)"serv1");
mirf.send((byte *) &rate);
while(mirf.issending()){
}
}
}
and here rx code
code: [select]
#include <mirf.h>
#include <mirfhardwarespidriver.h>
#include <mirfspidriver.h>
#include <nrf24l01.h>
#include <spi.h>
int rate;
void setup(){
serial.begin(9600);
mirf.spi = &mirfhardwarespi;
mirf.init();
mirf.setraddr((byte *)"serv1");
mirf.payload = sizeof(rate);
mirf.config();
}
void loop(){
while(!mirf.dataready()){
}
mirf.getdata((byte *) &rate);
serial.print("range of distance");
serial.print(rate);
serial.println("cm");
delay(100);
}
thanks you
because of can finish nrf2401l + hcsr04.
code connections correct , data sent . code ultrasonic sensors still wrong . distance sent " 0 " . perfected using library newping , worked.
transmitter
code: [select]
#include <spi.h>
#include <mirf.h>
#include <nrf24l01.h>
#include <mirfhardwarespidriver.h>
#include <newping.h>
#define trigger_pin 6
#define echo_pin 5
#define max_distance 200
int rate;
long previousmillis = 0;
long interval = 1000;
newping sonar(trigger_pin, echo_pin, max_distance);
void setup(){
serial.begin(9600);
mirf.cepin = 8;
mirf.csnpin = 7;
mirf.spi = &mirfhardwarespi;
mirf.init();
mirf.channel = 0;
mirf.payload = sizeof(rate);
mirf.config();
mirf.settaddr((byte *)"nrf01"); // le 2eme module va envoyer ses info au 1er module
mirf.setraddr((byte *)"nrf02");
}
void loop(){
unsigned long currentmillis = millis();
if(currentmillis - previousmillis > interval)
previousmillis = currentmillis;
rate = sonar.ping_cm();
serial.print(rate);
delay (100) ;
mirf.send((byte *) &rate);
while(mirf.issending()){
}
}
receiver
code: [select]
#include <mirf.h>
#include <mirfhardwarespidriver.h>
#include <mirfspidriver.h>
#include <nrf24l01.h>
#include <spi.h>
int rate;
void setup(){
serial.begin(9600);
mirf.cepin = 8;
mirf.csnpin = 7;
mirf.spi = &mirfhardwarespi;
mirf.init();
mirf.channel = 0;
mirf.payload = sizeof(rate);
mirf.config();
mirf.settaddr((byte *)"nrf02");
mirf.setraddr((byte *)"nrf01");
}
void loop(){
while(!mirf.dataready()){
}
mirf.getdata((byte *) &rate);
serial.print("range of distance");
serial.print(rate);
serial.println("cm");
delay(100);
}
thank much!
Arduino Forum > Using Arduino > Programming Questions > problem with the hc-sr04 with nrf240l01
arduino
Comments
Post a Comment