Robin2:
I have tried to look at your working and non-working Receiver programs side by side but I can't figure out the rationale for any of it. Meaningless names like EU and CR don't help.Can you provide a "walk-through" description of how the working code works - i.e. what each part does. And then do the same for the modified version.
As you have not posted a second version I am assuming there is no change to the TX code.
What version of the RF24 library are you using?
You are right i am going to post a walk-through description.
I can´t determinate the version of the library but i have download here, fist for the nrf24l01, second for the analog comparator:
The code should work in the next way:
First, the beacon sends a RF signal and an ultrasound signal.
Then, the receiver gets the RF signal and start a conunter.
After that, when the ultrasound is heared gets the time of the counter.
Finally, the maths to get the distance are solved, and i get that distance,
#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
char msg[16]= "Baliza 1 0 0 1";
int CR=0;
int CU=0;
int i;
RF24 radio(9,10);
const uint64_t pipes[2] = {0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL};
void startRadio() // here i start the radio module and send the message
{
radio.stopListening();
radio.write(msg, 16);
//Serial.println("r");
CR=1;
}
void Beacon() //here the ultrasound is sended
{
noInterrupts();
for (i=0; i<=8; i++)
{
PORTD = PORTD & B11011111 | B00010000;
delayMicroseconds(12.5);
PORTD = PORTD & B11101111 | B00100000;
//PORTD |= B00001000;
delayMicroseconds(12.5);
}
interrupts();
//Serial.println("u");
CU=1;
}
void setup()
{
Serial.begin(9600);
DDRD= DDRD | B00110000;
radio.begin();
radio.openWritingPipe(pipes[1]);
radio.openReadingPipe(1,pipes[0]);
radio.setRetries(15,15);
}
void loop()
{
startRadio();
Beacon();
// delay(500);
}
for the reciver codes, first the one that works well:
#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
#include <TimerOne.h>
#include <analogComp.h>
char msgint[16]="Baliza 1";
char msg[16];
int CR=0, CU=0;
int t1=0;
boolean s;
boolean r=0;
RF24 radio(9,10);
const uint64_t pipes[2] = {0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL};
int ER() // function for the receving rf message
{
if(r == 0)
{
radio.startListening(); // the radio start listening, i don´t know why but if this function is out that if()
r=1; // it doesn´t works correctly
}
if (radio.available())
{
int done = radio.read(msg, 16);
if (compmsgs(msgint, msg)==0)
{
Timer1.start(); // if the message is correct start a counter
CR=1;
}
}
if(compmsgs(msgint, msg)==0 && CR == 1 && r == 1)
{
radio.stopListening();
r=0;
}
}
int EU () // the internal analog comparator of the arduino is enabled
{
analogComparator.setOn(AIN0, AIN1); //we instruct the lib to use voltages on the pins
s= false;
analogComparator.enableInterrupt(changeStatus, RISING); //we set the interrupt and when it has to be raised
if (s == true)
{
t1=Timer1.read(); // when the pin in the arduino gets a read a high, i read the time of the timer.
//Timer1.stop();
CU=CU+1;
}
if(s == true && CU == 1)
{
analogComparator.disableInterrupt();
analogComparator.setOff();
}
}
void changeStatus()
{
s= true;
}
unsigned char compmsgs(char *code, char *codeintro)
{
while(*code!=0 && *codeintro!=0)
{
if(*code!=*codeintro)
{
return 1;
}
code++;
codeintro++;
}
return 0;
}
void setup(){
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1,pipes[1]);
Timer1.initialize();
}
void loop()
{
if (CR == 0) // here i have the other variables "CR" and "CU" just to ensure the program is running
{ // correctly
ER ();
}
if (CU == 0 && CR == 1)
{
EU ();
}
if (CR == 1 && CU == 1)
{
float distancia = t1*0.034; // in this program i get the measure in milimeters
Serial.print(t1);
Serial.print(" ");
Serial.println(distancia);
CR=0;
CU=0;
}
}
and in the other code:
#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
#include <TimerOne.h>
#include <analogComp.h>
boolean s;
boolean r=0;
char msgint[16]="Baliza 1 0 0 1";
char msgtostring[16];
String msg;
int CR=0, CU=0;
int t1=0;
int done;
float T=0;
float tiempofallo;
RF24 radio(9,10);
const uint64_t pipes[2] = {0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL};
int ER()
{
if(r == 0)
{
radio.startListening();
r=1;
}
if (radio.available())
{
done = radio.read(msgtostring, 16); //it has the same function in this program than in the other
if (compmsgs(msgint, msg)==0)
{
Timer1.restart();
CR=1;
Serial.println("r");
}
}
if(compmsgs(msgint, msg)==0 && CR == 1 && r == 1)
{
radio.stopListening();
r=0;
}
}
int EU () // and in this function should do the same
{
analogComparator.setOn(AIN0, AIN1); //we instruct the lib to use voltages on the pins
//s= false;
analogComparator.enableInterrupt(changeStatus, RISING); //we set the interrupt and when it has to be raised
if (s == true)
{
t1=Timer1.read();
//Timer1.stop();
CU=1;
Serial.println("u");
}
if(s == true && CU == 1)
{
analogComparator.disableInterrupt();
analogComparator.setOff();
Serial.println(s);
s= false;
}
}
void changeStatus()
{
s= true;
}
unsigned char compmsgs(char *code, char *codeintro)
{
while(*code!=0 && *codeintro!=0)
{
if(*code!=*codeintro)
{
return 1;
}
code++;
codeintro++;
}
return 0;
}
unsigned long ftemperatura() // here i just read the temperature with a LM35
{
int T10=analogRead(A0);
float T11=analogRead(A0)*(5000.0 / 1024.0);
float T12=T11/10;
return T12;
}
void setup(){
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1,pipes[1]);
Timer1.initialize(1000000);
T=ftemperatura();
tiempofallo=(2000/(331.4+0.6*T)); // the maximun time for waiting an ultrasound
}
void loop()
{
if(t1 < tiempofallo)
{
if(CR==0 && CU==0)
ER ();
if(CR==1 && CU==0)
EU ();
if (CR == 1 && CU == 1)
{
float distancia = t1*((331.4+0.61*T)/1000); // here i get the measure in centimeters
Serial.println(distancia);
Serial.print(CR);
Serial.print(CU);
CR=0;
CU=0;
done=0;
Serial.print(CR);
Serial.println(CU);
}
}
else
{
Serial.println("fuera de rango"); //if it is out of range sets the counter to 0
Timer1.restart();
CR=0;
CU=0;
}
}