mysterious delay on code!!!

I have made a code for nrf24l01 but when I connect transmitter with serioal monitor the TX led light with a delay about 1sec and this maybe cayse my problem on receiver..why that happens?
code

//----------------( #INCLUDE )--------------------------------
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 10

#include "Wire.h"
#include "I2Cdev.h"
#include "MPU6050.h"

#define dataRate RF24_2MBPS
#define paLevel RF24_PA_MAX

#define JOYSTICKFINGER A0 //reads from finger
#define JOYSTICKARM    A1 //reads from arm
#define accel_module (0x53)
//--------------( INT)-----------------------------------

MPU6050 mpu;
int16_t ax, ay, az;
int16_t gx, gy, gz;

int valy, valx, prevValy, prevValx, xservo, yservo;
char output[512];
byte values[6] ;
int VAL[4];

const uint64_t pipe = 0xE8E8F0F0E1LL;
RF24 radio(CE_PIN, CSN_PIN);

//-------------------( void setup )------------------

void setup()
{
  Wire.begin();
  TWBR = 24;
Serial.begin(9600);
//----nrf
radio.begin();

radio.setDataRate(dataRate);
radio.setPALevel(paLevel);

radio.openWritingPipe(pipe);
//----mpu
Serial.println("Initialize MPU");
mpu.initialize();
Serial.println(mpu.testConnection() ? "Connected" : "Connection failed");
//-----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();
}

//--------------------( voide loop )-----------------

void loop()
{
  
  mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
 VAL[0] = analogRead(JOYSTICKFINGER);
 VAL[1] = analogRead(JOYSTICKARM);
 VAL[2] = map(ay, -18000, 18000, 0, 180); //EXAMPLE
 Serial.print(VAL[2]);
 Serial.print("\t");

 VAL[3] = map(ax,-18000,18000,0,180);//EXAMPLE
 Serial.print(VAL[3]);
Serial.print("\t");

//------------------( ADXL )---------------------
 
int x, y, 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[4] = map(y, -255, 255, 0, 180);

sprintf(output, "%d %d %d", x, y, z);
Serial.print(output); 

Serial.write(10);
radio.write( VAL, sizeof(VAL) );
}
char output[512];

sprintf(output, "%d %d %d", x, y, z);

Do you really expect each int to take, on average, 170 characters to print?

That array is WAY too big.

int VAL[4];
 VAL[0] = analogRead(JOYSTICKFINGER);
 VAL[1] = analogRead(JOYSTICKARM);
 VAL[2] = map(ay, -18000, 18000, 0, 180); //EXAMPLE
 VAL[3] = map(ax,-18000,18000,0,180);//EXAMPLE
VAL[4] = map(y, -255, 255, 0, 180);

Writing 5 values in an array that can hold 4 is NOT a good idea.

You are only sending 4 of the 5 values that you write.

I int VAL [5] but against the same problem

Wire.beginTransmission(accel_module);
Wire.requestFrom(accel_module, 6);

You do not issue a requestFrom() in the middle of a transmission.

I should add these lines?

And how I could transmit data from mpu?

Wire.beginTransmission(accel_module);
Wire.requestFrom(accel_module, 6);

int i = 0;
while(Wire.available()){
values[i] = Wire.read();
i++;
}
Wire.endTransmission();

Lose the beginTransmission and endTransmission there.

int VAL[4];
...

radio.write( VAL, sizeof(VAL) );

I think not. That isn't how you write 4 integers.

Also reserve all-caps for constants, not variables.

The int val [4] WAS MY FAULT on my code is int VAL[5]...So you told me to delete wire.begintransmission and wire.endtransmission line...

Aye, that I did.

The ones in the portion of code I posted. Not all of them.

ok I fixed it also on serial monitor receiver print values from receiver then has some error period then print values then error period and so on.. What can I do for better results? I could add ceramic capacitor parallel vcc and gnd on nrf24l01?
Thanks for your help

Please post your amended code.

I found an other code for mpu..I gonna post it when I go home because im posting from mobile

There is a problem on code transmitter send values on receiver but on receiver serial monitor the adxl takes a value and dont change. For example it shows turn=90 and when I move the sensor this value dont change but all the others sensors work correctly

code:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.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

const int MPU=0x68;  // I2C address of the MPU-6050
    int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;

int VAL[4];
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);
//-----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);
}

//--------------------( 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(y, -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)    
VAL[3] = AcX;
Serial.print("mpu=");
Serial.print(VAL[3]);
radio.write( VAL, sizeof(VAL) );
}

Instead of setting TWBR (Two Wire Bit Rate) to 24 you should probably call Wire.setClock(250000UL). It will be far more readable.

If I delete TWBR then it will run correctly?

Wire.beginTransmission(accel_module);
Wire.requestFrom(accel_module, 6);

int i = 0;
while(Wire.available()){
values[i] = Wire.read();
i++;
}
Wire.endTransmission();

See reply #7.

DimitrisTzam:
I have made a code for nrf24l01

Oh, man - someone has got to explain this to the non-native english speakers. Can everyone please stop talking about code in the singluar.

English has slightly different treatment of things that are counted in individual units, and stuff that isn't made up of individual units, like water (yes, obviously water is made of molecules, but in usual day-to-day usage we don't talk about it that way).

You can say "I have an orange", but it makes no sense to say "I have a water". You can have a cup or a litre of water. You can have some water. You can even simply have water (in situations where not having it is a possibility, eg you are in the desert).

Likewise, you can say "a sketch" or "a program", or "an application", but it doesn't make any sense to say "a code" (in that sense).

Code is an amount of … stuff. You can say you have a certain amount of kilobytes of code, or pages of code, or you can be indefinite and say you have "some" code, or you can sometimes leave off the quantifier altogether, but the only way to have "a" code is if you mean a single line of assembler or a single machine language instruction.

PaulMurrayCbr:
Oh, man - someone has got to explain this to the non-native english speakers. Can everyone please stop talking about code in the singluar.

English has slightly different treatment of things that are counted in individual units, and stuff that isn't made up of individual units, like water (yes, obviously water is made of molecules, but in usual day-to-day usage we don't talk about it that way).

You can say "I have an orange", but it makes no sense to say "I have a water". You can have a cup or a litre of water. You can have some water. You can even simply have water (in situations where not having it is a possibility, eg you are in the desert).

Likewise, you can say "a sketch" or "a program", or "an application", but it doesn't make any sense to say "a code" (in that sense).

Code is an amount of … stuff. You can say you have a certain amount of kilobytes of code, or pages of code, or you can be indefinite and say you have "some" code, or you can sometimes leave off the quantifier altogether, but the only way to have "a" code is if you mean a single line of assembler or a single machine language instruction.

OT - with apology to Dimitris.

As one of those non native English speakers I have found the usage of "articles"
most difficult to grasp. It seems to be grammatical crutch used by many languages ( der / das, la etc.) who lack the ability to modify the "object" the article is attached too to make "sense" , as you nicely put it , from the whole sentence..
What I found interesting is that even native English speakers cannot explain when to use "the" and when not.
So - I speak and write without articles which apparently grammatical purists find offensive.
Anyway - thanks for the post.
Very refreshing format in this sea of arrogance.

English has lots of traps for the non-native speaker.

Why did the chicken cross the road?

Why don't you say:

Why did a chicken cross a road?

You just don't! And that's the end of the discussion. :stuck_out_tongue: