|
Post by RC Tractor Guy on Apr 1, 2015 23:41:12 GMT
Here is the code for the NRF24 tutorial. If someone figures out how to add servo control make sure to post an updated version for others to find.
Controller Code
/* NRF24L01 Module Demo Controller Code Pin Assignment D0 D1 D2 D3 D4 D5 D6 D7 Joystick Push Button D8 NRF24 CE D9 D10 NRF24 CSN D11 NRF24 MOSI D12 NRF24 MISO D13 NRF24 SCK A0 Joystick Y Axis A1 Joystick X Axis A2 A3 A4 A5 */
// Define variables int Right_Joy_X = 0x00; int Right_Joy_Y = 0x00; int Right_Joy_X_Old = 0x00; int Right_Joy_Y_Old = 0x00; int Right_Joy_Button = LOW; int Head_Lights = 0;
// Include Libraries #include <SPI.h> #include <RH_NRF24.h>
RH_NRF24 nrf24; // Create NRF24 object
uint8_t command[11]; // Create array for data to be sent
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN]; // Create an array buffer for the NRF24 module
void setup() { pinMode(7, INPUT_PULLUP); // Right joystick button
// initialize the radio module nrf24.init(); nrf24.setChannel(1); nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm);
command[0] = 0x4A; command[1] = 0x44; command[2] = 0x38; command[3] = 0x33; command[4] = 0x06; command[5] = 0x01;
}
void loop() { Check_Vehicle_ID(); Check_Buttons(); Check_Pots(); }
void Check_Vehicle_ID(){
command[0] = 0x4A; // J command[1] = 0x44; // D command[2] = 0x38; // 8 command[3] = 0x33; // 3
}
void Check_Buttons(){
// Check Joystick Push Button Right_Joy_Button = digitalRead(7);
if (Right_Joy_Button==LOW) {
if (Head_Lights == 0){ Head_Lights = 1; command[10]=1; } else if (Head_Lights == 1){ Head_Lights = 0; command[10]=0; }
// Wait for switch to be released while (Right_Joy_Button == LOW) { Right_Joy_Button = digitalRead(7); }
Send_Data();
}
}
void Check_Pots(){ Right_Joy_X = analogRead(A1); Right_Joy_X= map(Right_Joy_X, 0, 1024, 0xFF, 0x00); Right_Joy_Y = analogRead(A0); Right_Joy_Y= map(Right_Joy_Y, 0, 1024, 0xFF, 0x00);
// Only send data if something has changed, to try an minimise interference if(Right_Joy_X != Right_Joy_X_Old || Right_Joy_Y != Right_Joy_Y_Old){ command[8]=Right_Joy_X; command[9]=Right_Joy_Y;
// Send the data Send_Data();
// Store the sent values Right_Joy_X_Old=Right_Joy_X; Right_Joy_Y_Old=Right_Joy_Y; }
}
// Function to send the data void Send_Data(){ uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN]; uint8_t len = sizeof(buf); nrf24.send(command, sizeof(command)); nrf24.waitPacketSent(); }
Receiver Code
/* NRF24L01 Module Demo Receiver Code Pin Assignment D0 D1 D2 D3 D4 LED Pin D5 PWMA D6 AIN1 D7 AIN2 D8 NRF24 CE D9 D10 NRF24 CSN D11 NRF24 MOSI D12 NRF24 MISO D13 NRF24 SCK A0 A1 A2 A3 A4 A5 */
// This section describes the vehicle (this ID is JD83) byte ID1 = 0x4A; // Enter a four byte ID for the vehicle byte ID2 = 0x44; // for example F936 for the Fendt 936 model byte ID3 = 0x38; // would be ID1 = 0x46; ID2 = 0x39; ID3 = 0x33; ID4 = 0x36 byte ID4 = 0x33; // because asci character F is 46 in hexidecimal and so on
// Variables used in the program int Move_1 = 125; // Variable used to determine the motor direction byte Command_Val = 0x00; // Stores the command value byte Data_Val_1 = 0x00; // Stores the data value byte Data_Val_2 = 0x00; // Stores the data value byte Data_Val_3 = 0x00; // Stores the data value byte Data_Val_4 = 0x00; // Stores the data value byte Data_Val_5 = 0x00; // Stores the data value byte Data_Val_6 = 0x00; // Stores the data value
// Include Libraries #include <SPI.h> #include <RH_NRF24.h>
RH_NRF24 nrf24; // Create NRF24 object
void setup() { pinMode(4, OUTPUT); // Set digital pin 4 as an output
nrf24.init(); // Initiailize NRF24 radio module nrf24.setChannel(1); // Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm); }
void loop() { if (nrf24.available()) // If the NRF24 has recieved data { uint8_t buf[11]; // Create array to store it in uint8_t len = sizeof(buf); if (nrf24.recv(buf, &len)) // Store the data { // We recieved a string of ten bytes, four ID, one command and six data values but they may not be for this tractor if (buf[0] == ID1 && buf[1] == ID2 && buf[2] == ID3 && buf[3] == ID4){ // Confirm that the correct vehicle ID has been recieved Command_Val = buf[4]; // Store the new command value Data_Val_1 = buf[5]; // Store the new data value Data_Val_2 = buf[6]; // Store the new data value Data_Val_3 = buf[7]; // Store the new data value Data_Val_4 = buf[8]; // Store the new data value Data_Val_5 = buf[9]; // Store the new data value Data_Val_6 = buf[10]; // Store the new data value }
}
// Servo not simple, NRF24 and servo libraries trying to use the same timer
// Motor Control // Data recieved is between 0 and 255. The center is 255/2=127.5 and we want a buffer zone around here to stop the tractor from moving unexpectedly. if (Data_Val_5 < 120){ // This if function create a buffer zone of 7.5 bits below 127.5 Move_1 = map(Data_Val_5, 120, 0, 0,255); // PWM is 0% at 0 and 100% at 255 so scale the data value for use with the motor analogWrite(5,Move_1); // Set the PWM value on pin 5 to move the motor digitalWrite(6, HIGH); // Set the direction with pins 6 and 7 digitalWrite(7, LOW); } else if (Data_Val_5 > 135){ // This if function create a buffer zone of 7.5 bits above 127.5 Move_1 = map(Data_Val_5, 135, 255, 0,255); // PWM is 0% at 0 and 100% at 255 so scale the data value for use with the motor analogWrite(5,Move_1); // Set the PWM vlue on pin 5 to move the motor digitalWrite(7, HIGH); // Set the direction with pins 6 and 7 digitalWrite(6, LOW);
} else{ // Every other value is in the buffer zone Move_1=0; analogWrite(5,Move_1); // Set the PWM to 0% digitalWrite(6, LOW); // Set the direction with pins 6 and 7 but not necessary digitalWrite(7, LOW); }
// LED Control if(Data_Val_6==1){ digitalWrite(4, HIGH); // Turn LED on } else { digitalWrite(4, LOW); // Turn LED off } } }
|
|
|
Post by rctractorwizard on Apr 19, 2015 22:40:23 GMT
I just tried to c&p this sketch to my IDE and this is the error compiling message that I got.
any suggestions would be greatly appreciated!
Best regards.
Arduino: 1.6.3 (Mac OS X), Board: "Arduino Uno"
NRF24_.ino:37:22: fatal error: RH_NRF24.h: No such file or directory compilation terminated. Error compiling.
This report would have more information with "Show verbose output during compilation" enabled in File > Preferences.
|
|
|
Post by RC Tractor Guy on Apr 20, 2015 22:00:37 GMT
|
|
|
Post by rctractorwizard on Apr 24, 2015 17:18:50 GMT
|
|
|
Post by Leif on May 6, 2015 18:53:35 GMT
Hi, thanks for your time.
Actually i do not recevie anything. I am useing an Arduino nano as controler and an Uno as receiver.
Receiver code:
/* NRF24L01 Module Demo Receiver Code Pin Assignment D0 D1 D2 D3 D4 LED Pin D5 PWMA D6 AIN1 D7 AIN2 D8 NRF24 CE D9 D10 NRF24 CSN D11 NRF24 MOSI D12 NRF24 MISO D13 NRF24 SCK A0 A1 A2 A3 A4 A5 */
// This section describes the vehicle (this ID is JD83) byte ID1 = 0x4A; // Enter a four byte ID for the vehicle byte ID2 = 0x44; // for example F936 for the Fendt 936 model byte ID3 = 0x38; // would be ID1 = 0x46; ID2 = 0x39; ID3 = 0x33; ID4 = 0x36 byte ID4 = 0x33; // because asci character F is 46 in hexidecimal and so on
// Variables used in the program int Move_1 = 125; // Variable used to determine the motor direction byte Command_Val = 0x00; // Stores the command value byte Data_Val_1 = 0x00; // Stores the data value byte Data_Val_2 = 0x00; // Stores the data value byte Data_Val_3 = 0x00; // Stores the data value byte Data_Val_4 = 0x00; // Stores the data value byte Data_Val_5 = 0x00; // Stores the data value byte Data_Val_6 = 0x00; // Stores the data value
float Value;
// Include Libraries #include <SPI.h> #include <RH_NRF24.h>
RH_NRF24 nrf24; // Create NRF24 object
void setup() { pinMode(4, OUTPUT); // Set digital pin 4 as an output Serial.begin(9600); nrf24.init(); // Initiailize NRF24 radio module nrf24.setChannel(1); // Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm); }
void loop() { if (nrf24.available()) // If the NRF24 has recieved data { uint8_t buf[11]; // Create array to store it in uint8_t len = sizeof(buf); if (nrf24.recv(buf, &len)) // Store the data { // We recieved a string of ten bytes, four ID, one command and six data values but they may not be for this tractor if (buf[0] == ID1 && buf[1] == ID2 && buf[2] == ID3 && buf[3] == ID4){ // Confirm that the correct vehicle ID has been recieved Command_Val = buf[4]; // Store the new command value Data_Val_1 = buf[5]; // Store the new data value Data_Val_2 = buf[6]; // Store the new data value Data_Val_3 = buf[7]; // Store the new data value Data_Val_4 = buf[8]; // Store the new data value Data_Val_5 = buf[9]; // Store the new data value Data_Val_6 = buf[10]; // Store the new data value Serial.println("jop"); // Debug }
}
// Servo not simple, NRF24 and servo libraries trying to use the same timer
// Motor Control // Data recieved is between 0 and 255. The center is 255/2=127.5 and we want a buffer zone around here to stop the tractor from moving unexpectedly. if (Data_Val_5 < 120){ // This if function create a buffer zone of 7.5 bits below 127.5 Move_1 = map(Data_Val_5, 120, 0, 0,255); // PWM is 0% at 0 and 100% at 255 so scale the data value for use with the motor analogWrite(5,Move_1); // Set the PWM value on pin 5 to move the motor digitalWrite(6, HIGH); // Set the direction with pins 6 and 7 digitalWrite(7, LOW); } else if (Data_Val_5 > 135){ // This if function create a buffer zone of 7.5 bits above 127.5 Move_1 = map(Data_Val_5, 135, 255, 0,255); // PWM is 0% at 0 and 100% at 255 so scale the data value for use with the motor analogWrite(5,Move_1); // Set the PWM vlue on pin 5 to move the motor digitalWrite(7, HIGH); // Set the direction with pins 6 and 7 digitalWrite(6, LOW);
} else{ // Every other value is in the buffer zone Move_1=0; analogWrite(5,Move_1); // Set the PWM to 0% digitalWrite(6, LOW); // Set the direction with pins 6 and 7 but not necessary digitalWrite(7, LOW); }
// LED Control if(Data_Val_6==1){ digitalWrite(4, HIGH); // Turn LED on } else { digitalWrite(4, LOW); // Turn LED off } } }
//Read more: http://forum.rctractors.net/thread/81#ixzz3ZNuFna4i
Controler code;
/* NRF24L01 Module Demo Controller Code Pin Assignment D0 D1 D2 D3 D4 D5 D6 D7 Joystick Push Button D8 NRF24 CE D9 D10 NRF24 CSN D11 NRF24 MOSI D12 NRF24 MISO D13 NRF24 SCK A0 Joystick Y Axis A1 Joystick X Axis A2 A3 A4 A5 */
// Define variables int Right_Joy_X = 0x00; int Right_Joy_Y = 0x00; int Right_Joy_X_Old = 0x00; int Right_Joy_Y_Old = 0x00; int Right_Joy_Button = LOW; int Head_Lights = 0;
// Include Libraries #include <SPI.h> #include <RH_NRF24.h>
RH_NRF24 nrf24; // Create NRF24 object
uint8_t command[11]; // Create array for data to be sent
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN]; // Create an array buffer for the NRF24 module
void setup() { pinMode(7, INPUT_PULLUP); // Right joystick button Serial.begin(9600); // initialize the radio module nrf24.init(); nrf24.setChannel(1); nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm);
command[0] = 0x4A; command[1] = 0x44; command[2] = 0x38; command[3] = 0x33; command[4] = 0x06; command[5] = 0x01;
}
void loop() { Check_Vehicle_ID(); Check_Buttons(); Check_Pots(); }
void Check_Vehicle_ID(){
command[0] = 0x4A; // J command[1] = 0x44; // D command[2] = 0x38; // 8 command[3] = 0x33; // 3
}
void Check_Buttons(){
// Check Joystick Push Button Right_Joy_Button = digitalRead(7);
if (Right_Joy_Button==LOW) {
if (Head_Lights == 0){ Head_Lights = 1; command[10]=1; } else if (Head_Lights == 1){ Head_Lights = 0; command[10]=0; }
// Wait for switch to be released while (Right_Joy_Button == LOW) { Right_Joy_Button = digitalRead(7); }
Send_Data();
}
}
void Check_Pots(){ Right_Joy_X = analogRead(A1); Serial.println(Right_Joy_X); //Debug Right_Joy_X= map(Right_Joy_X, 0, 1024, 0xFF, 0x00); Right_Joy_Y = analogRead(A0); Right_Joy_Y= map(Right_Joy_Y, 0, 1024, 0xFF, 0x00);
// Only send data if something has changed, to try an minimise interference if(Right_Joy_X != Right_Joy_X_Old || Right_Joy_Y != Right_Joy_Y_Old){ command[8]=Right_Joy_X; command[9]=Right_Joy_Y;
// Send the data Send_Data();
// Store the sent values Right_Joy_X_Old=Right_Joy_X; Right_Joy_Y_Old=Right_Joy_Y; }
}
// Function to send the data void Send_Data(){ uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN]; uint8_t len = sizeof(buf); nrf24.send(command, sizeof(command)); nrf24.waitPacketSent(); }
//Read more: http://forum.rctractors.net/thread/81#ixzz3ZNuFna4i
|
|
|
Post by gfink on May 7, 2015 1:06:16 GMT
RC Tractor Guy, Nice Post. I've looked over the web for a simple way to communicate between (2) Arduinos, Pro Mini -- Uno, so I can create a simple alarm. I came across your code and thought it was well written, so I tried to morph it into what I was looking to accomplish. However, i'm struggling to get it to talk. I've tested my setup with several examples, for the NRF24L01's so I know I have it wired correctly. So If I may pick your brain, can I get you to glance over and/or make a recommendation of where I've gone wrong, please sir? This is to simply use the transmitter to alarm once a IR Sensor trips, communicate back to receiver, UNO, and then play some tone for notification. Any bit of wisdom would be greatly appreciated, and thanks for sharing your code with others to get off the ground... Warm Regards, gfink Transmitter Code: /*
NRF24L01 Module Demo Receiver Code
Pin Assignment
D0
D1
D2
D3
D4
D5
D6
D7 IRSensor
D8 NRF24 CE
D9
D10 NRF24 CSN
D11 NRF24 MOSI
D12 NRF24 MISO
D13 NRF24 SCK
A0
A1
A2
A3
A4
A5
*/
// Variables used in the program
//int Move_1 = 125; // Variable used to determine the motor direction delete
int v_IRGate = 0x00;
int v_IRGate_Old = 0x00;
int v_IRGate_Button = LOW;
// Include Libraries
#include <SPI.h>
#include <RH_NRF24.h>
RH_NRF24 nrf24; // Create NRF24 object
uint8_t command[11]; // Create array for data to be sent
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN]; // Create an array buffer for the NRF24 module
void setup()
{
Serial.begin(9600);
pinMode(7, INPUT_PULLUP); // IRGate Sensor
nrf24.init(); // Initiailize NRF24 radio module
nrf24.setChannel(1); // Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm);
}
void loop()
{
IRGate_ID();
Check_Sensors();
Serial.print (buf[RH_NRF24_MAX_MESSAGE_LEN]);
}
void IRGate_ID(){
command[0] = 0x73; //sending Hex "s"
command[1] = 0x63; //sending Hex "c"
command[2] = 0x30; //sending Hex "0"
command[3] = 0x31; //sending Hex "1"
}
void Check_Sensors(){
// Check Sensors
v_IRGate_Button = digitalRead(7);
//Serial.print (v_IRGate_Button);
if (v_IRGate_Button==LOW)
{
command[5]=1;
}
else{
command[5]=0;
}
// Wait for switch to be released
while (v_IRGate_Button == LOW)
{
v_IRGate_Button = digitalRead(7);
}
Send_Data();
}
// Function to send the data
void Send_Data(){
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
nrf24.send(command, sizeof(command));
nrf24.waitPacketSent();
}
************** Receiver Code *********************** /*
NRF24L01 Module Demo Controller Code
Pin Assignment
D0
D1
D2
D3
D4
D5
D6
D7 IRGate Speaker Alarm
D8 NRF24 CE
D9
D10 NRF24 CSN
D11 NRF24 MOSI
D12 NRF24 MISO
D13 NRF24 SCK
A0
A1
A2
A3
A4
A5
*/
// Variables used in the program
// This section describes the Sensor (this ID is sc01)
byte ID1 = 0x73; // Enter a four byte ID for the vehicle
byte ID2 = 0x63; // for example F936 for the Fendt 936 model
byte ID3 = 0x38; // would be ID1 = 0x46; ID2 = 0x39; ID3 = 0x33; ID4 = 0x36
byte ID4 = 0x33; // because asci character F is 46 in hexidecimal and so on
// Variables used in the program
int Move_1 = 125; // Variable used to determine the motor direction
byte Command_Val = 0x00; // Stores the command value
byte Data_Val_1 = 0x00; // Stores the data value
byte Data_Val_2 = 0x00; // Stores the data value
byte Data_Val_3 = 0x00; // Stores the data value
byte Data_Val_4 = 0x00; // Stores the data value
byte Data_Val_5 = 0x00; // Stores the data value
byte Data_Val_6 = 0x00; // Stores the data value
// Music Info Start
int ledPin = 13;
int speakerOut = 7;
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
byte melody[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2g2p8p8p8p";
// count length: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
// 10 20 30
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 24;
int statePin = LOW;
// Music Info End
// Include Libraries
#include <SPI.h>
#include <RH_NRF24.h>
// NRF24 Communication Setup
RH_NRF24 nrf24; // Create NRF24 object
uint8_t command[11]; // Create array for data to be sent
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN]; // Create an array buffer for the NRF24 module
void setup()
{
Serial.begin(9600);
pinMode(7, OUTPUT); // Speaker
// initialize the radio module
nrf24.init();
nrf24.setChannel(1);
nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm);
// hex to ascii reference: http://www.rapidtables.com/convert/number/hex-to-ascii.htm
command[0] = 0x73; //sending Hex "s"
command[1] = 0x63; //sending Hex "c"
command[2] = 0x30; //sending Hex "0"
command[3] = 0x31; //sending Hex "1"
command[4] = 0x06; //sending Hex "ACK"
command[5] = 0x01; //sending Hex "NUL"
}
void loop()
{
if (nrf24.available()) // If the NRF24 has recieved data
{
uint8_t buf[11]; // Create array to store it in
uint8_t len = sizeof(buf);
Serial.print (Data_Val_1);
if (nrf24.recv(buf, &len)) // Store the data
{
// We recieved a string of ten bytes, four ID, one command and six data values but they may not be for this tractor
//if (buf[0] == ID1 && buf[1] == ID2 && buf[2] == ID3 && buf[3] == ID4){ // Confirm that the correct vehicle ID has been recieved
Serial.print (buf[RH_NRF24_MAX_MESSAGE_LEN]);
Command_Val = buf[4]; // Store the new command value
Data_Val_1 = buf[5]; // Store the new data value
Data_Val_2 = buf[6]; // Store the new data value
Data_Val_3 = buf[7]; // Store the new data value
Data_Val_4 = buf[8]; // Store the new data value
Data_Val_5 = buf[9]; // Store the new data value
Data_Val_6 = buf[10]; // Store the new data value
//}
// Security Sensor - sc01 Control
if(Data_Val_1==1){
//for (int i=0; i<1000; i++) {
//digitalWrite(7, HIGH); // Turn Speaker On
Music();
}}
else {
digitalWrite(7, LOW); // Turn Speaker off
}
}
}
void Music() {
analogWrite(speakerOut, 0);
for (count = 0; count < MAX_COUNT; count++) {
statePin = !statePin;
digitalWrite(ledPin, statePin);
for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
for (count2=0;count2<8;count2++) {
if (names[count2] == melody[count*2 + 1]) {
analogWrite(speakerOut,500);
delayMicroseconds(tones[count2]);
analogWrite(speakerOut, 0);
delayMicroseconds(tones[count2]);
}
if (melody[count*2 + 1] == 'p') {
// make a pause of a certain size
analogWrite(speakerOut, 0);
delayMicroseconds(500);
}
}
}
}
}
|
|
|
Post by RC Tractor Guy on May 7, 2015 9:56:47 GMT
Hi Leif,
Firstly since you are having coms issues I'd comment out the button controls in the main loop of the controller code. You already set some values in the array during setup so I'd replace the main loop with the following for a simple debug.
void loop() { //Check_Vehicle_ID(); //Check_Buttons(); //Check_Pots(); Send_Data(); delay(250); }
You are using serial.println("jop") to debug but I think println is for values only not strings but I'm not 100% sure. Try serial.print("jop") instead. When you want to print the sent value try using Serial.write(Command_Val) and you should get the Hex value 06 since you set command[4] = 0x06 in the controller setup.
Let me know if that works.
|
|
|
Post by RC Tractor Guy on May 7, 2015 10:31:59 GMT
Hi gfink,
Are you sure the examples you tested with use the same Arduino pins, did they use the same library? Some libraries use different pins for example I was looking at one last week which used pin 9 for CE instead of pin 8.
I haven't seen Serial.print (buf[RH_NRF24_MAX_MESSAGE_LEN]); before, what does that output? The whole array?
Is the sensor on pin 7 something like a switch where it is either 0 volts or vcc?
I see you've commented out the id check but just in case you haven't spotted it the ID on the receiver side is set to SC83 instead of SC01
I wonder could you simply be swamping the receiver with too much data because your code seems to transmit constantly, have you tried putting a delay in the transmitter loop. I think you should probably have the code test for the IR sensor constantly, then when it is triggered flag that change of state and keep it flagged for a few seconds but only transmit the signal every 100ms or 200ms.
Another thing you might try is to write a transmitter code which just transmits the intruder signal for 5 seconds and then the no intruder signal for 5 seconds to make sure your receiver code is working.
Sorry I haven't spotted anything obvious, tricky enough code you are working on.
|
|
|
Post by Leif on May 7, 2015 17:40:41 GMT
Hi, Serial.println("String") works fine. I simplified the code. the receiver is not going in the if (nrf24.available()) Part.
I checkt every hardware thing thrue but did not find something. I switched the arduinos(sender, receiver code), nothing. Maybe i screwd the Modules??! dont know. Yesterday i ordered 2 new ones. tomorrow i dont have time so i hope the new modules are here friday/sataday so i can try again on sataday. excuse the spelling. And thanks for your help.
/* NRF24L01 Module Demo Receiver Code Pin Assignment D0 D1 D2 D3 D4 LED Pin D5 PWMA D6 AIN1 D7 AIN2 D8 NRF24 CE D9 D10 NRF24 CSN D11 NRF24 MOSI D12 NRF24 MISO D13 NRF24 SCK A0 A1 A2 A3 A4 A5 */
// This section describes the vehicle (this ID is JD83) byte ID1 = 0x4A; // Enter a four byte ID for the vehicle byte ID2 = 0x44; // for example F936 for the Fendt 936 model byte ID3 = 0x38; // would be ID1 = 0x46; ID2 = 0x39; ID3 = 0x33; ID4 = 0x36 byte ID4 = 0x33; // because asci character F is 46 in hexidecimal and so on
// Variables used in the program int Move_1 = 125; // Variable used to determine the motor direction byte Command_Val = 0x00; // Stores the command value byte Data_Val_1 = 0x00; // Stores the data value byte Data_Val_2 = 0x00; // Stores the data value byte Data_Val_3 = 0x00; // Stores the data value byte Data_Val_4 = 0x00; // Stores the data value byte Data_Val_5 = 0x00; // Stores the data value byte Data_Val_6 = 0x00; // Stores the data value
// Include Libraries #include <SPI.h> #include <RH_NRF24.h>
RH_NRF24 nrf24; // Create NRF24 object
void setup() { pinMode(4, OUTPUT); // Set digital pin 4 as an output Serial.begin(9600); nrf24.init(); // Initiailize NRF24 radio module nrf24.setChannel(1); // Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm); Serial.println("Setup Ok"); }
void loop() { Serial.println("Loop start"); if (nrf24.available()) // If the NRF24 has recieved data { Serial.println("Data available"); uint8_t buf[11]; // Create array to store it in uint8_t len = sizeof(buf); if (nrf24.recv(buf, &len)) // Store the data { // We recieved a string of ten bytes, four ID, one command and six data values but they may not be for this tractor if (buf[0] == ID1 && buf[1] == ID2 && buf[2] == ID3 && buf[3] == ID4){ // Confirm that the correct vehicle ID has been recieved Command_Val = buf[4]; // Store the new command value Data_Val_1 = buf[5]; // Store the new data value Data_Val_2 = buf[6]; // Store the new data value Data_Val_3 = buf[7]; // Store the new data value Data_Val_4 = buf[8]; // Store the new data value Data_Val_5 = buf[9]; // Store the new data value Data_Val_6 = buf[10]; // Store the new data value Serial.println(Command_Val); // Debug }
}
} }
Send Code:
/* NRF24L01 Module Demo Controller Code Pin Assignment D0 D1 D2 D3 D4 D5 D6 D7 Joystick Push Button D8 NRF24 CE D9 D10 NRF24 CSN D11 NRF24 MOSI D12 NRF24 MISO D13 NRF24 SCK A0 Joystick Y Axis A1 Joystick X Axis A2 A3 A4 A5 */
// Define variables int Right_Joy_X = 0x00; int Right_Joy_Y = 0x00; int Right_Joy_X_Old = 0x00; int Right_Joy_Y_Old = 0x00; int Right_Joy_Button = LOW; int Head_Lights = 0;
// Include Libraries #include <SPI.h> #include <RH_NRF24.h>
RH_NRF24 nrf24; // Create NRF24 object uint8_t command[11]; // Create array for data to be sent uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN]; // Create an array buffer for the NRF24 module
void setup() { pinMode(7, INPUT_PULLUP); // Right joystick button Serial.begin(9600); // initialize the radio module nrf24.init(); nrf24.setChannel(1); nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm);
command[0] = 0x4A; command[1] = 0x44; command[2] = 0x38; command[3] = 0x33; command[4] = 0x06; command[5] = 0x01;
}
void loop() { //Check_Vehicle_ID(); //Check_Buttons(); //Check_Pots(); Serial.println("Loop"); Send_Data(); delay(250); Serial.println(command[4]); }
// Function to send the data void Send_Data(){ Serial.println("Send Start..."); uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN]; uint8_t len = sizeof(buf); nrf24.send(command, sizeof(command)); nrf24.waitPacketSent(); Serial.println("Send stop"); }
|
|
|
Post by RC Tractor Guy on May 11, 2015 10:17:28 GMT
That is very strange, especially when you had them communicating before. Did you try putting back on code that worked for you previously?
Another forum member mentioned a possible problem requiring a smoothing capacitor between GND and the 3.3 volt line of the NRF24, it's probably not the problem but it's easy to rule out if you have space capacitors around.
|
|
|
Post by Gfink on May 11, 2015 14:29:39 GMT
Hi gfink, Are you sure the examples you tested with use the same Arduino pins, did they use the same library? Some libraries use different pins for example I was looking at one last week which used pin 9 for CE instead of pin 8. I haven't seen Serial.print (buf[RH_NRF24_MAX_MESSAGE_LEN]); before, what does that output? The whole array? Is the sensor on pin 7 something like a switch where it is either 0 volts or vcc? I see you've commented out the id check but just in case you haven't spotted it the ID on the receiver side is set to SC83 instead of SC01 I wonder could you simply be swamping the receiver with too much data because your code seems to transmit constantly, have you tried putting a delay in the transmitter loop. I think you should probably have the code test for the IR sensor constantly, then when it is triggered flag that change of state and keep it flagged for a few seconds but only transmit the signal every 100ms or 200ms. Another thing you might try is to write a transmitter code which just transmits the intruder signal for 5 seconds and then the no intruder signal for 5 seconds to make sure your receiver code is working. Sorry I haven't spotted anything obvious, tricky enough code you are working on. Hey RC Tractor Guy, Thanks for looking over this code and offering up ideas. So some responses.. - I did move my pin over to pin 8 as you,'d mentioned i did catch that other lib's were using pin 9. - turned on serial print so i could see I could see the switch closures while troubleshooting. - also, while troubleshooting I'd turned off the id portion in an effort to simplify testing. Also, you may notice that i'd kinda flipped your trnsmitter/receiver code as I was hoping the simple sensor arduino would just alarm when the sensor is tripped with the uno being the alarming unit that could be used for other functions as my understanding of coding grows... - Great point on transmitting data only when needed, and certainly makes sense however again, I'm stuck at not receiving any data on the receiver side. Again I really appreciate you providing some guidance and I'll be happy to share the code with you once i/we get this working. If you're interested, if not i can go away and thanks for your cycles already spent... Thank you sir...
|
|
|
Post by RC Tractor Guy on May 12, 2015 9:51:12 GMT
Again I really appreciate you providing some guidance and I'll be happy to share the code with you once i/we get this working. If you're interested, if not i can go away and thanks for your cycles already spent... Thank you sir... I don't mind trying to help, I learn stuff along the way too so it's all good. In this case I can't see why the program wouldn't work, I can't see anything obvious. Are the NRF24 modules really close together like on the same breadboard? or is the antenna covered over by wires or anything like that which might be blocking the signal? And just to be sure something hasn't fried them, did you try going back to code that was working to check they are still good?
|
|
|
Post by Gfink on May 12, 2015 14:31:31 GMT
Again I really appreciate you providing some guidance and I'll be happy to share the code with you once i/we get this working. If you're interested, if not i can go away and thanks for your cycles already spent... Thank you sir... I don't mind trying to help, I learn stuff along the way too so it's all good. In this case I can't see why the program wouldn't work, I can't see anything obvious. Are the NRF24 modules really close together like on the same breadboard? or is the antenna covered over by wires or anything like that which might be blocking the signal? And just to be sure something hasn't fried them, did you try going back to code that was working to check they are still good? So i've tried different NRF's - no worky, i'd also went back to some standard Lib's for NRF's communication and that piece worked fine. I do have a small cap on both NRF's vcc/gnd connections... And they're laying fairly close together... Although the other examples had worked i'll spread them apart a bit... Also, just fyi... I'd flip/flopped the transmitter/receivers eg put transmitter on uno and no worky worky... . Stumped on this end... Hee hee i'll keep poking at it and let you know what i find.. Do you have the equipment to test and/or time?
|
|
|
Post by RC Tractor Guy on May 12, 2015 14:42:01 GMT
I don't have the Arduinos with me this week sorry, maybe at the weekend if I get a chance.
The original code I posted, that did work for you right?
|
|
|
Post by Gfink on May 14, 2015 0:46:02 GMT
I don't have the Arduinos with me this week sorry, maybe at the weekend if I get a chance. The original code I posted, that did work for you right? I didn't try it honestly as i was working through understanding it.. I will try it this werkend as well. Thnaks, Gfink
|
|