Hi,
I am using 2 Nano boards. The first one, with a NRF24L01, will be a remote with a button that when it is pushed it will send the signal to the second Nano (with another NRF24L01) which will activate a servo.
I am using Radiohead library.
I am able to upload the code for the "transmitter", and it seems to work fine. Here the code for the "transmitter" and a picture of the serial monitor when it is working:
//Programa del Emisor (Mando a distancia)
#include <SPI.h>
#include <RH_NRF24.h>
RH_NRF24 nrf24;
#define Pulsador 2
bool Estado_Pulsador = 0;
void setup()
{
pinMode (Pulsador, INPUT_PULLUP);
Serial.begin(9600);
if (!nrf24.init())
Serial.println("fallo inicializacion");
// Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
if (!nrf24.setChannel(2))
Serial.println("fallo en establecer canal");
if (!nrf24.setRF(RH_NRF24::DataRate250kbps, RH_NRF24::TransmitPower0dBm))
Serial.println("fallo en opciones RF");
}
void loop()
{
//Si presionamos el pulsador enviaremos el caracter "1"
Estado_Pulsador = !digitalRead(Pulsador);
Serial.println(Estado_Pulsador);
//delay (500);
if (digitalRead(Pulsador) == LOW){
Serial.println("Boton activado, Enviando señal para activar servo");
uint8_t data[] = "Activa el servo";
nrf24.send(data, sizeof(data));
nrf24.waitPacketSent();
}
// Now wait for a reply
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
if (nrf24.available())
{
// Should be a reply message for us now
if (nrf24.recv(buf, &len))
{
Serial.print("Recibido: ");
Serial.println((char*)buf);
}
else
{
Serial.println("fallo en recepcion");
}
}
delay(1000);
}
The problem is when I check and upload the code for the "receiver". It shows some "compile error problems" that I do not understand. Here the code and a picture of the error messages:
//Programa del Receptor (Maquina de pelotas)
#include <SPI.h>
#include <RH_NRF24.h>
RH_NRF24 nrf24;
#include <Servo.h>
Servo miservo;
int PINSERVO = 5; // Pin D5 del arduino nano, que controlará el servo
/*int PULSOMIN = 1000; // Un ancho de pulso de 1ms (1000 microsegundos) equivale a 0 grados
int PULSOMAX = 2000; // Un ancho de pulso de 2ms equivale a 180 grados
// // Un ancho de pulso de 1.5ms equivale a 90 grados*/
void setup()
{
Serial.begin(9600);
miservo.attach (PINSERVO); //, PULSOMIN, PULSOMAX);
miservo.write (0);
if (!nrf24.init())
Serial.println("fallo de inicializacion");
// Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
if (!nrf24.setChannel(2))
Serial.println("fallo en establecer canal");
if (!nrf24.setRF(RH_NRF24::DataRate250kbps, RH_NRF24::TransmitPower0dBm))
Serial.println("fallo en opciones RF");
Serial.println("Base iniciada");
}
void loop()
{
if (nrf24.available())
{
// Should be a message for us now.
//En la variable "buf" almacenamos la cadena recibida (texto/mensaje enviado por el emisor) y en la variable "len" la longitud de la cadena recibida
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
if (nrf24.recv(buf, &len))
{
// NRF24::printBuffer("request: ", buf, len);
Serial.print("Recibido: ");
Serial.println((char*)buf);
//compara dato recibido para poder activar el servo
//if ((char*)buf[0] == 'Activa el servo') { //El codigo recibido debe ser "1"
miservo.write (180);
Serial.println ("servo on");
delay (1000);
miservo.write (0);
delay (1000);
//}
// Send a reply
uint8_t data[] = "Recibido 1: Activando Servo";
nrf24.send(data, sizeof(data));
nrf24.waitPacketSent();
Serial.println("Respondiendo");
}
else
{
Serial.println("fallo en recepcion");
}
}
}
It is very strange because if I take the code and remove the "servo section" I am able to upload the code to Arduino. Here the code and a picture, but there is no communication with the transmitter:
#include <SPI.h>
#include <RH_NRF24.h>
RH_NRF24 nrf24;
void setup()
{
Serial.begin(9600);
if (!nrf24.init())
Serial.println("fallo de inicializacion");
// Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
if (!nrf24.setChannel(2))
Serial.println("fallo en establecer canal");
if (!nrf24.setRF(RH_NRF24::DataRate250kbps, RH_NRF24::TransmitPower0dBm))
Serial.println("fallo en opciones RF");
Serial.println("Base iniciada");
}
void loop()
{
if (nrf24.available())
{
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
if (nrf24.recv(buf, &len))
{
Serial.print("Recibido: ");
Serial.println((char*)buf);
uint8_t data[] = "Recibida señal de activacion servo: Servo Activado";
nrf24.send(data, sizeof(data));
nrf24.waitPacketSent();
Serial.println("Respondiendo");
}
else
{
Serial.println("fallo en recepcion");
}
}
}
Could anyone help me? I have not experience enough in Arduino.
PD.- sorry for my English ![]()

