Hello,
I am tying to communicate some arduinos through a Time Difference of Arrival DtoA.
The proyect consist in an indoor positioning system with three beacons and one receiver.
I use a NRF24l01 module for de radiofrecuency with the library RF24master and an ultrasonic device wich signal is detected through de analog comparator of the arduino with the library analogComp.
I have a code that works pretty well, just to measure the distance between one beacon and teh receiver, but when i try to introduce more staff, like the sound speed as a function of temperature the distance measure is awfull.
I have allready checked all the conexions of the arduinos.
The first code i used for the beacon is:
#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()
{
radio.stopListening();
radio.write(msg, 16);
//Serial.println("r");
CR=1;
}
void Beacon()
{
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);
}
And the code for the receiver is:
#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()
{
if(r == 0)
{
radio.startListening();
r=1;
}
if (radio.available())
{
int done = radio.read(msg, 16);
if (compmsgs(msgint, msg)==0)
{
Timer1.start();
CR=1;
}
}
if(compmsgs(msgint, msg)==0 && CR == 1 && r == 1)
{
radio.stopListening();
r=0;
}
}
int EU ()
{
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=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)
{
ER ();
}
if (CU == 0 && CR == 1)
{
EU ();
}
if (CR == 1 && CU == 1)
{
float distancia = t1*0.034;
Serial.print(t1);
Serial.print(" ");
Serial.println(distancia);
CR=0;
CU=0;
}
}
and the code that works awfull is:
#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);
if (/*compmsgs(msgint, msg)==0*/ done == 1)
{
Timer1.restart();
CR=1;
Serial.println("r");
}
}
if(/*compmsgs(msgint, msg)==0*/ done == 1 && CR == 1 && r == 1)
{
radio.stopListening();
r=0;
}
}
int EU ()
{
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()
{
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));
}
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);
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");
Timer1.restart();
CR=0;
CU=0;
}*/
}
I am desperate because im really stacked with this code, if someone have an idea please help me.