nRF24L01 + Nunchuck + servo x2

Hello,
I’m new to the arduino world and I’ve been trying for an arduino to operate remotely with 2 nRF24L01 modules.
One would be the sender and would be connected to an arduino ONE.
Commands would be given by the Nunchuck control.
On the other side is an Arduino Mega with 2 servo motors.
My problem:
I have 2 codes that work well apart and I leave at the end of this post;
The 1st code does the Nunchuck control and works perfectly but unfortunately is for use with only one arduino.
The 2nd code makes perfect communication between nRF24L01 modules. With it I can control an analog PS2 button by moving the Servos on the other end.

I asked someone to give me a little help in getting this nunchuck code working.

// ==========================================================
// --- Auxiliary Libraries ---
#include <Wire.h>                //I2C
#include <Servo.h>               //Servo
#include "nunchuck_funcs.h"      //Para controlar o comando Wii NunChuck
// ============================================================================
// --- Mapeamento de Hardware ---
//--------------------#define   sv_base      8     //conexão servo da base
#define   sv_dir       3     //conexão servo da direita
#define   sv_esq       7     //conexão servo da esquerda
#define   sv_gar       8     //conexão servo da garra
#define   led          4     //led de indicação
#define   led2         2     //led de indicação
#define   led3         4
// ================================================
// --- Declaração de Objetos ---
Servo dir;      //servo da direita
Servo esq;      //servo da esquerda
Servo gar;      //terceiro servo se houver
//     ---------------------------
// ===================================
// --- Prototipo das Funçoes ---
void arm_control();             //Funçãoo de controle do braço robótico
void read_cbut();              //Função de leitura e tratamento do botão C
void ler_led2();
// =======
// --- Global Variables ---
int loop_cnt = 0;
byte accx, accy, zbut, cbut, joyx, joyy;
boolean cbut_flag = 0x00,
       hold_flag = 0x01;
// ==========================================
// --- Main Function---
void setup()
{
 // -- Output Set for Servos and led --

 pinMode(sv_dir,  OUTPUT);
 pinMode(sv_esq,  OUTPUT);
 pinMode(sv_gar,  OUTPUT);
 pinMode(led,     OUTPUT);
 pinMode(led2,    OUTPUT);
 pinMode(led3,    OUTPUT);
 // -- Inicialização do Controle NunChuck --
 nunchuck_setpowerpins();
 nunchuck_init(); 
 hold_flag = 0x01; //seta hold_flag
 nunchuck_get_data();
 delay(10); 
 // -- Ligação dos Servo Motores --
 dir.attach(sv_dir);
 esq.attach(sv_esq);
 gar.attach(sv_gar); 
digitalWrite(led3,LOW);
} //end setup
// 
// --- Loop Infinito ---
void loop()
{
 Serial.begin(9600);
 if ( loop_cnt > 100 ) //taxa de atualização de 100ms
 { 
   loop_cnt = 0;      //reinicia loop_cnt
//analogico
   nunchuck_get_data();      //coleta dados do nunchuck
   joyx = nunchuck_joyx();   //joystick X 
   joyy = nunchuck_joyy();   //joystick Y 
// acelerômetro
   accx  = nunchuck_accelx(); //acelerômetro X (controlo eixo X)
   accy  = nunchuck_accely(); //acelerômetro Y (controlo eixo y)
   zbut = nunchuck_zbutton();
   cbut = nunchuck_cbutton(); //botão C (quando pressionado, congela)
// hold_flag2 = nunchuck_zbutton(); //botão Z (controlo apenas acelerometro)
   arm_control();             //chama função de controle
   ler_led2();
 } //end if principal
 read_cbut();   //lê e trata o botão C
 loop_cnt++;    //incrementa loop_cnt
 delay(1);      //a cada 1ms
} //end loop
// ======================================================================
// --- Development of Functions---
void arm_control()      //Função de controle do braço robótico
{
 if(hold_flag) digitalWrite(led, HIGH);
  else
  {
   joyx = map(joyx, 128, 0,  90, 0);     //normaliza servo da esquerda 

   esq.write(joyx);  
   accy = map(accy,  20, 150,  35, 170),
  dir.write(accy);
 } //end else
} //end arm_control
//---------------------------------------------------------------------
void ler_led2(){
if(zbut) digitalWrite(led2, HIGH);
else
{ 
   digitalWrite(led2, LOW);
 } //end else
if(nunchuck_zbutton()==1){
 digitalWrite(led3, HIGH);
}
else
{ 
   digitalWrite(led3, LOW);
 } //end else
 }//End void ler_led2
void read_cbut()              //C button reading and processing function
{
 if(cbut) cbut_flag = 0x01;  //seta flag se cbut pressionado

 if(!cbut && cbut_flag)      //cbut solto e flag setada?
 {                           //sim...
    cbut_flag = 0x00;        //limpa flag
    hold_flag = !hold_flag;  //inverte o estado de hold_flag
 } //end if !cbut
} //end read_cbut

This is the one that sends:

//Transmitter code with analog
//
#include <SPI.h>
#include "RF24.h" 
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL; 
int msg[1]; 
int potpin_1 = A0; 
int val_1; 
int potpin_2 = A1; 
int val_2; 
void setup(void){
radio.begin();
radio.openWritingPipe(pipe); 
}
void loop() {
val_1 = analogRead(potpin_1),val_1 = map(val_1, 0, 1023, 0, 127),msg[0] = val_1,radio.write(msg, 1);
val_2 = analogRead(potpin_2),val_2 = map(val_2, 0, 1023, 128, 255),msg[0] = val_2,radio.write(msg, 1);
}

Finally, the receiver:

//receptor com analógico
//
//
#include <Servo.h>
#include <SPI.h>
#include "RF24.h"
Servo servo1;
Servo servo2;
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL; 
int msg[1]; 
int data; 
int pos;
void setup()
{
servo1.attach(3); 
servo2.attach(7);
radio.begin();
radio.openReadingPipe(1,pipe); 
radio.startListening();
}
void loop()
{
if (radio.available())radio.read(msg, 1);
if (msg[0] <128 && msg[0] >-1)data = msg[0], pos = map(data, 0, 127, 7, 177),servo1.write(pos);
if (msg[0] >127 && msg[0] <255)data = msg[0], pos = map(data, 128, 254, 9, 177),servo2.write(pos);
}

Please, anyone who realizes a little of this helps me in my problem. I do not know what else to do to get this working!

nunchuck_funcs.h (5.35 KB)

Please read this. http://forum.arduino.cc/index.php/topic,148850.0.html

then come back and edit your post to add some code tags and make the code readable. People are here volunteering their time to help you for free. Please show enough respect for those people to read and follow the forum rules.

Also note that it is a REALLY bad idea to post your email address in plain text on a forum post. That is just inviting spam and abuse from every corner of the internet.

Delta_G:
Please read this. http://forum.arduino.cc/index.php/topic,148850.0.html

then come back and edit your post to add some code tags and make the code readable. People are here volunteering their time to help you for free. Please show enough respect for those people to read and follow the forum rules.

I'm really sorry.

CDIcomics:
I have 2 codes that work well apart and I leave at the end of this post;

Have a look a the examples in this Simple nRF24L01+ Tutorial. I hope they will illustrate how to use the nRF24.

You need to realize that it just sends a number of bytes (max 32 per message) and it has no idea what the data represents. So you just need to package the data from your nunchuck into an array (or a struct if the datatypes are not all the same) so it can be sent and received. You need the same data structure (array or struct) at both the sender and receiver.

And don't write code like this

val_1 = analogRead(potpin_1),val_1 = map(val_1, 0, 1023, 0, 127),msg[0] = val_1,radio.write(msg, 1);

with several things on one line. It is just too confusing. I'm not even sure that that line does what you think it does.

In fact I am 99% certain it doesn't because, at best, it is sending the first byte of the array msg

...R