Hi all,
Im working on a project with two nrf24l01 one arduino pro mini and one arduino une all clones from ebay…
I have made a code that I transmit 5 values from 5 different sensors… the code works fine but I have added a delay(500) … the problem is that if I add delay <500 signal lost…I connected nrf modules with extended suplies… please could anyone help me?
thats my codes
[code]
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <HMC5883L.h>
//-----------
#define CE_PIN 9
#define CSN_PIN 10
#include "Wire.h"
#define JOYSTICKFINGER A0 //reads from finger
#define JOYSTICKARM A1 //reads from arm
#define dataRate RF24_2MBPS
#define paLevel RF24_PA_MAX
HMC5883L compass;
int valy, valx;//mag vals
const int MPU=0x68; // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
int VAL[6];
char output[512];
byte values[6];
#define accel_module (0x53)
const uint64_t pipe = 0xE8E8F0F0E1LL;
RF24 radio(CE_PIN, CSN_PIN);
void setup()
{
Wire.begin();
Serial.begin(9600);
//----nrf
radio.begin();
radio.setDataRate(dataRate);
radio.setPALevel(paLevel);
radio.setChannel(70);
radio.openWritingPipe(pipe);
Wire.setClock(250000UL);
//-----adxl
Wire.beginTransmission(accel_module);
Wire.write(0x2D);
Wire.write(0);
Wire.endTransmission();
Wire.beginTransmission(accel_module);
Wire.write(0x2D);
Wire.write(16);
Wire.endTransmission();
Wire.beginTransmission(accel_module);
Wire.write(0x2D);
Wire.write(8);
Wire.endTransmission();
Wire.beginTransmission(MPU);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
Serial.begin(9600);
//hmc5883l
Serial.println("Serial started.");
compass = HMC5883L();
Serial.println("Setting scale to +/- 1.3 Ga");
int error = compass.SetScale(1.3); // Set the scale of the compass.
if(error != 0) // If there is an error, print it out.
Serial.println(compass.GetErrorText(error));
Serial.println("Setting measurement mode to continuous.");
error = compass.SetMeasurementMode(Measurement_Continuous); // Set the measurement mode to Continuous
if(error != 0) // If there is an error, print it out.
Serial.println(compass.GetErrorText(error));
}
//--------------------(END VOID SETUP)-----------
void Output(MagnetometerRaw raw, MagnetometerScaled scaled, float heading, float headingDegrees)
{
Serial.print(headingDegrees);
VAL[5] = map(headingDegrees, 0, 360, 0, 360);
Serial.print("degrees");Serial.print(VAL[5]);
}
//--------------------( voide loop )-----------------
void loop()
{
VAL[0] = analogRead(JOYSTICKFINGER);
VAL[1] = analogRead(JOYSTICKARM);
int y, x, z;
int xyzregister = 0x32;
Wire.beginTransmission(accel_module);
Wire.write(xyzregister);
Wire.endTransmission();
Wire.beginTransmission(accel_module);
Wire.requestFrom(accel_module, 6);
int i = 0;
while(Wire.available()){
values[i] = Wire.read();
i++;
}
Wire.endTransmission();
x = (((int)values[1]) << 8) | values[0];
y = (((int)values[3])<< 8) | values[2];
z = (((int)values[5]) << 8) | values[4];
if (x < -255) x = -255; else if (x > 255) x = 255;
if (y < -255) y = -255; else if (y > 255) y = 255;
VAL[2] = map(x, -255, 255, 0, 180);
Serial.print(VAL[2]);
Serial.print("\t");
sprintf(output, "%d %d %d", x, y, z);
Serial.print(output);
Serial.print("\n");
Wire.beginTransmission(MPU);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU,14,true); // request a total of 14 registers
AcX=Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
VAL[3] = AcX;
Serial.print("mpu=");
Serial.print(VAL[3]);
VAL[4] = AcY;
//hmc5883l
// Retrive the raw values from the compass (not scaled).
MagnetometerRaw raw = compass.ReadRawAxis();
// Retrived the scaled values from the compass (scaled to the configured scale).
MagnetometerScaled scaled = compass.ReadScaledAxis();
// Calculate heading when the magnetometer is level, then correct for signs of axis.
float heading = atan2(raw.YAxis, raw.XAxis);
// Correct for when signs are reversed.
if(heading < 0)
heading += 2*PI;
// Convert radians to degrees for readability.
float headingDegrees = heading * 180/M_PI;
// Output the data via the serial port.
Output(raw, scaled, heading, headingDegrees);
radio.write( VAL, sizeof(VAL) );
}
[/code]
receiver:
[code]
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
#define dataRate RF24_2MBPS
#define paLevel RF24_PA_MAX
#define CE_PIN 9
#define CSN_PIN 10
int valfinger, valarm, valy, valx, headingDegrees;
const uint64_t pipe = 0xE8E8F0F0E1LL;
RF24 radio(CE_PIN, CSN_PIN);
int VAL[6];
Servo bodyturn;
Servo fingerservo;
Servo armservo;
Servo turnservo;
Servo mpuxservo;
Servo mpuyservo;
void setup()
{
Serial.begin(9600);
Serial.println("Nrf24L01 Receiver Starting");
radio.begin();
radio.setDataRate(dataRate);
radio.setPALevel(paLevel);
radio.setChannel(70);
radio.openReadingPipe(1,pipe);
radio.startListening();;
fingerservo.attach(2);
armservo.attach(3);
turnservo.attach(4);
mpuxservo.attach(5);
mpuyservo.attach(6);
bodyturn.attach(7);
}
void loop()
{
if ( radio.available() )
{
bool done = false;
while (!done)
{
done = radio.read( VAL, sizeof(VAL) );
delay(500);
valfinger = map(VAL[0], 0, 1023, 0, 179); //finger joy
fingerservo.write(valfinger);
Serial.print("finger =");
Serial.print(VAL[0]);
Serial.print("\t");
valarm = VAL[1]; //finger joy
Serial.print("arm =");
Serial.print(VAL[1]);
Serial.print("\t");
Serial.print("turn=");
Serial.print(VAL[2]);
Serial.print("\t");
//ADXL
Serial.print(" X=");
Serial.print(VAL[3]);
Serial.print("\t");
Serial.print(" Y=");
Serial.print(VAL[4]);
Serial.print("\n");
headingDegrees = VAL[5];
Serial.print("degrees=");Serial.print(VAL[5]);
Serial.print(" Degrees=");Serial.print(VAL[5]);
valy = map(headingDegrees, 180, 0, 0, 25);
Serial.print("valy= ");
Serial.print(valy);
valx = map(headingDegrees, 360, 181, 20, 60);
Serial.print("valx=");Serial.print(valx);
if ( VAL[5] > 1300)
{
digitalWrite(4,HIGH);
}
else if ( VAL[5] < -5000)
{
digitalWrite(1,HIGH);
}
else
{
digitalWrite(1,LOW);
if (headingDegrees >180)
{
bodyturn.write(valx);
}
else
{
bodyturn.write(valy);
}
}
}
}
else
{
Serial.println("NO radio available");
}
}
[/code]