working my way through this tutorial and having problems:
nRF24L01 Wireless Joystick for Arduino Robot Car | DroneBot Workshop
After this code there is no place to type and it says " No more attachments are allowed today" so this might take a few days.
Be patient.
transmitter code:
/* nRF24L01+ Joystick Transmitter nrf24l01-joy-xmit-demo.ino nRF24L01+ Transmitter with Joystick Use with Joystick Receiver Demo DroneBot Workshop 2018 https://dronebotworkshop.com */ // Include RadioHead ReliableDatagram & NRF24 Libraries #include <RHReliableDatagram.h> #include <RH_NRF24.h> // Include dependant SPI Library #include <SPI.h> // Define Joystick Connections #define JoyStick_X_PIN A0 #define JoyStick_Y_PIN A1 // Define addresses for radio channels #define CLIENT_ADDRESS 1 #define SERVER_ADDRESS 2 // Create an instance of the radio driver RH_NRF24 RadioDriver; // Sets the radio driver to NRF24 and the client address to 1 RHReliableDatagram RadioManager(RadioDriver, CLIENT_ADDRESS); // Declare unsigned 8-bit joystick array uint8_t joystick[3]; // Define the Message Buffer uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN]; void setup() { // Setup Serial Monitor Serial.begin(9600); // Initialize RadioManager with defaults - 2.402 GHz (channel 2), 2Mbps, 0dBm if (!RadioManager.init()) Serial.println("init failed"); } void loop() { // Print to Serial Monitor Serial.println("Reading joystick values "); // Read Joystick values and map to values of 0 - 255 joystick[0] = map(analogRead(JoyStick_X_PIN), 0, 1023, 0, 255); joystick[1] = map(analogRead(JoyStick_Y_PIN), 0, 1023, 0, 255); joystick[2] = 100; //Display the joystick values in the serial monitor. Serial.println("-----------"); Serial.print("x:"); Serial.println(joystick[0]); Serial.print("y:"); Serial.println(joystick[1]); Serial.println("Sending Joystick data to nrf24_reliable_datagram_server"); //Send a message containing Joystick data to manager_server if (RadioManager.sendtoWait(joystick, sizeof(joystick), SERVER_ADDRESS)) { // Now wait for a reply from the server uint8_t len = sizeof(buf); uint8_t from; if (RadioManager.recvfromAckTimeout(buf, &len, 2000, &from)) { Serial.print("got reply from : 0x"); Serial.print(from, HEX); Serial.print(": "); Serial.println((char*)buf); } else { Serial.println("No reply, is nrf24_reliable_datagram_server running?"); } } else Serial.println("sendtoWait failed"); delay(100); // Wait a bit before next transmission }
Still can't figure out how to type anything below the code so I'll post the wiring and pictures in another post.
Please be patient,... at least it posted this time, ha.
Here's the receiver code:
/* nRF24L01+ Joystick Receiver Demo nrf24l01-joy-rcv-demo.ino nRF24L01+ Receiver with Joystick Decode Use with Joystick Transmitter Demo DroneBot Workshop 2018 https://dronebotworkshop.com */ // Include RadioHead ReliableDatagram & NRF24 Libraries #include <RHReliableDatagram.h> #include <RH_NRF24.h> // Include dependant SPI Library #include <SPI.h> // Define addresses for radio channels #define CLIENT_ADDRESS 1 #define SERVER_ADDRESS 2 // Create an instance of the radio driver RH_NRF24 RadioDriver; // Sets the radio driver to NRF24 and the server address to 2 RHReliableDatagram RadioManager(RadioDriver, SERVER_ADDRESS); // Define a message to return if values received uint8_t ReturnMessage[] = "JoyStick Data Received"; // Define the Message Buffer uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN]; void setup() { // Setup Serial Monitor Serial.begin(9600); // Initialize RadioManager with defaults - 2.402 GHz (channel 2), 2Mbps, 0dBm if (!RadioManager.init()) Serial.println("init failed"); } void loop() { if (RadioManager.available()) { // Wait for a message addressed to us from the client uint8_t len = sizeof(buf); uint8_t from; if (RadioManager.recvfromAck(buf, &len, &from)) //Serial Print the values of joystick { Serial.print("got request from : 0x"); Serial.print(from, HEX); Serial.print(": X = "); Serial.print(buf[0]); Serial.print(" Y = "); Serial.print(buf[1]); Serial.print(" Z = "); Serial.println(buf[2]); // Send a reply back to the originator client, check for error if (!RadioManager.sendtoWait(ReturnMessage, sizeof(ReturnMessage), from)) Serial.println("sendtoWait failed"); } } }
It's not obvious to me where you're placing the joystick values into the buffer (but) before sending the buffer.
Anything seems possible when you don't know what you're talking about.
@toymaker It might speed things up if
1. Describe what you think should happen
2. Describe what did happen
3. Provide serial debug (if large zipped)
4. Provide code in a zip if more than about 30 logical non-repetitive lines.
5. It can't hurt to take a screen snapshot of the Tools menu as well.
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.
OK there's the code. The wiring is just like in the turtorial:
nRF24L01 Wireless Joystick for Arduino Robot Car | DroneBot Workshop
Seems like these first 3 connections are always the same on every nrf24 project I've seen.
SCK goes to pin 13
MOSI goes to pin 11
MISO goes to pin 12
While these CE and CSN can go many places.
On mine they go to:
CE goes to pin 8
CSN goes to pin 10
Just like in the tutorial.
I am using the adapter and powering it with 5volts from the nano.
and grounded to nano also.
I am powering them both from the USB ports on one laptop.
It has 1.8.19 arduino IDI on it.
COM 3 is being used for the receiver and serial monitor says:
init failed
The red LED and green center LED on the nano are on but DO NOT FLASH.
The outer green LED stays dark
COM 7 is being used for the transmitter and serial monitor says:
Reading motorcontrol values
Motor A: 0 - Motor B: 0 - Direction: 0
sendtoWait failed
overandoverandover, ha
The 2 outside lights (red and green) are flashing alternately (when one is OFF the other is ON) and the center one stays solid green
I do have the joystick connected to the one I call the transmitter and the values do change accordingly.
The receiver and transmitter use the same wiring.
Sooo what if I just swap the sketches?
If I go to tools in the receiver sketch and change it to COM 7
and upload it to the transmitter (the one with the joystick),
While on the transmitter sketch change it to COM3
Thereby loading it to the receiver,
on the serial monitor for COM3/receiver (no joy) I get:
init failed
Reading motorcontrol values
Motor A: 227 - Motor B: 85 - Direction: 1
red and center green steady on the LEDs, the other stays dark
On COM 7/transmitter serial monitor(joy) I get squat, nothing, blank
What if I physically swap ports, USB cable travels with nano?
Plug trans (joy) to COM3 and receive to COM7
Without changing the sketch?
Glad you asked.
Problem swaps with the ports. Same,... zilch on COM3 serial now
and COM 7 some self censoring of irate profanity then the same:
⸮⸮⸮⸮⸮⸮`xf⸮⸮⸮fx怘⸮f⸮fx⸮⸮⸮⸮init failed
Reading motorcontrol values
Motor A: 227 - Motor B: 89 - Direction: 1
Try swapping the cables you say,.. OK.
Cable that was between receiver and COM7 is now on the receiver and COM3
I'm kinda used to the trans being on COM7 so
upload receive sketch to COM3
and trans sketch back to COM7
and in the serial monitors everything is back to the way it was when trans was COM7.
and same on the LEDs, trans is flashing while receive is not.
Writing this all out has been good for me.
Last week the flashing was traveling with the sketch now not so much.
Looks like the receive nano/nerf isn't flashing no matter what.
Doesn't seem to change with the cable or COM port.
And that is the one that WAS working when the other nano got hot.
It's not getting hot like the other so that's good.
Not much you can do to help me.
I might try this with one of my unos.
or swapping nerfs just not right now.
and I do have another old xp laptop without internet access and an older arduino IDI on it. But I'll need to put the radiohead library on it to use it for this.
Dang, doesn't look like I can go back and edit my first post to warn you not to read all of this.
I don't consider this solved yet. Not until the serial monitors are talking.
Sorry to waste your time today.
I don't consider this solved yet. Not until the serial monitors are talking.
Sorry to waste your time today.
These issues can be a little tricky to resolve, and In my experience, the best way to resolve them is to strip out and use the least amount of code to narrow down the core issue, at least for basic communication, i.e:- No joystick etc. Even before that step, one of the best ways to determine basic communication between the devices is to load up the basic client and server examples that come with the library, and work from there.
Also double check that you're only supplying 3.3 volts to the devices, as they are not 5 volt tolerant.
Many people are here to help, so don't worry about wasting anyone's time.
And by the way... this forum software isn't the best, so to break your cursor out of the HTML environment you've been captured in, just type something below it - you can go back into the HTML code editor via:
... and type anything after the final HTML tag.
Alternatively, add a couple of more lines of spaces and text, and then place your cursor in between the text where you want your code to sit between.
Cheers
Just came back to check on replies, don't have time to do more right now.
It's good to see some of you are so eager to help on this forum.
So helpful in fact you tried to help while I was still trying to get that last post written, ha.
That did take me awhile to write.
I'll try to follow your advice:
1. figure out what HTML is so my posts work/look better
2. Use the more simple radiohead server and client sketches.
I had been planning that as my next step.
3. I think the adapter is supposed to bring the nano 5 volts down to 3.3,
I'll check and make sure that it does.
4. I'll take some screenshots of the tools menu and post them. Do I need a photo hosting site to do that?
5. figure out how to do serial debug
6. figure out how to provide code in a zip
7. and I still want to post a picture of my circuits.
Just came back to check on replies, don't have time to do more right now.
It's good to see some of you are so eager to help on this forum.
So helpful in fact you tried to help while I was still trying to get that last post written, ha.
That did take me awhile to write.
I'll try to follow your advice:
1. figure out what HTML is so my posts work/look better
2. Use the more simple radiohead server and client sketches.
I had been planning that as my next step.
3. I think the adapter is supposed to bring the nano 5 volts down to 3.3,
I'll check and make sure that it does.
4. I'll take some screenshots of the tools menu and post them. Do I need a photo hosting site to do that?
5. figure out how to do serial debug
6. figure out how to provide code in a zip
7. and I still want to post a picture of my circuits.
1. When you reply, you can click the button I posted previously to access the HTML source code window from the menu - You can then paste your HTML code in that window, but first you need to have the HTML code. You can obtain it via your Arduino IDE - Select the code you want to show the members here, and then copy and paste it, selecting the "Copy as HTML" option as shown below in your IDE:
4. You can just copy your photo and paste it directly into your reply window (if the forum rules consider you have passed your probationary period), or use the buttons in your reply screen... same for point 6 and 7.
5. A serial debug means nothing more that just printing some statements to the serial monitor, simply to verify where you are in the code and what the current values are - By printing these kinds of statements, you can determine if a variable has the value you were expecting, etc... Here is a very simple example:
void setup() { Serial.begin(9600); // Lets assume in our code, we are expecting // a specific value via an analog input pin int value = 42; if(42 == value) { Serial.println("In the if block with value: " + String(value)); } else { Serial.println("In the else block with value: " + String(value)); } } void loop() { // ... }
Cheers
6. figure out how to provide code in a zip
7. and I still want to post a picture of my circuits.
6. Don't know which OS yo are but zipping a selection of files can usually be done by right clicking.
7. At the bottom of the replay window it says Attach Files.
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.
Still running them off one laptop.
I've named the two nano/nerfs Joy and Nojoy
Joy still has the joystick wired to +- A0 and A1. Nojoy has none.
Uploaded the client sketch to Nojoy COM3
and server sketch to Joy COM7
Results:
CLIENT Nojoy: red and green outer LEDs flash.
Center LED stays green
serial monitor COM3 says:
init failed
Sending to nrf24_server
No reply, is nrf24_server running?
Sending to nrf24_server
SERVER Joy: red and center LEDs are solid, nothing from the other green LED.
COM7 serial monitor says:
Nothing just blank.
Since they are wired nearly the same
(Joy still has the joystick wires to +- A0 A1 and Nojoy doesn't have a joystick)
I'm going to swap the sketches by just trading the ports in the tool menu.
Everything is still connected the same electronically.
The client sketch is now connected to Joy on COM7
and the server sketch to Nojoy on COM3
And the problem traveled with the sketch.
CLIENT Joy: red and green outer LEDs flash.
Center LED stays green
Serial monitor on COM7:
Sending to nrf24_server
No reply, is nrf24_server running?
Sending to nrf24_server
No reply, is nrf24_server running? (better catch it, ha.)
SERVER Nojoy: stopped flashing, red and center/green LED stay lit, nothing from the other green LED flashing
Serial monitor on COM3:
init failed
The only difference I see is in the serial monitor.
"init failed" stays with COM3/Nojoy everything else flashing and message moves with the sketch.
OK even though the {;} window has surrounded my previous text with the arrows and p I can still write above that.
I see what you mean by serial debug doh obvious.
[snipped example code]
Cool... looks like you have mastered the source code window.
Please use the '@' symbol at the top of every post with the person's username, in that you wish to respond to... just as I am responding to you (@toymaker at the top of this reply) - That way, the person you respond to will revive a direct email notification, otherwise they will not be notified.
Cheers
Still running them off one laptop.
I've named the two nano/nerfs Joy and Nojoy
Joy still has the joystick wired to +- A0 and A1. Nojoy has none.
[snipped - still too much code for debugging a basic setup]
OK, please check out the following link... it provides a minimal piece of code for server and client... this is what I was talking about when I said to strip it all back.
Test it out, and lets go from there 🙂
[edit] - Oop's - Would be good if I provided the link:
Cheers
@toymaker That code posting procedure worked well.
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.
@toymaker It''s early AM for me, but if I am seeing your results right it seems there is some sort of issue with com3 in both cases. Is it possible to use a different com port?
I see @frogandtoad has posted a link to a smaller sketch for debugging purposes. That is an excellent idea. I will back off and defer to him as it sounds like he has the experience with this.
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.