system
April 9, 2014, 2:49pm
1
when i tried to combine ultrasonic, the output from serial monitor is zero. either problem with my coding.?for tx code
#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 is my rx code
#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 the distance");
Serial.print(rate);
Serial.println("cm");
delay(100);
}
thanks for you
because of you I can finish also NRF2401L + HCSR04.
code for your connections are correct and the data sent . but the code for your ultrasonic sensors are still wrong . so the distance is sent is " 0 " . for that I perfected using the library NewPing and it worked.
TRANSMITTER
#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
#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 the distance");
Serial.print(rate);
Serial.println("cm");
delay(100);
}
thank you very much!