Notifications
Clear all

How Can I modify this code to Control two or more servo motors using the nrf24l01 Module With These codes ?

69 Posts
2 Users
0 Likes
21.2 K Views
Gulshan
(@gulshan)
Member
Joined: 4 years ago
Posts: 127
Topic starter  

Here I have Got a Code From Howtomechatronics.com For controlling the servo motor using A Potentiometer Through Nrf24l01 Modules but the problem is I am unable to modify it to control three servo motors Please Can anyone help me ?

Here is the transmitter and the receiver code

Transmitter:-

/*
Arduino Wireless Network - Multiple NRF24L01 Tutorial
== Example 01 - Servo Control / Node 00 - Potentiometer ==
by Dejan, www.HowToMechatronics.com
Libraries:
nRF24/RF24, https://github.com/nRF24/RF24
nRF24/RF24Network, https://github.com/nRF24/RF24Network
*/
#include <RF24.h>
#include <RF24Network.h>
#include <SPI.h>

RF24 radio(8, 10); // nRF24L01 (CE,CSN)
RF24Network network(radio); // Include the radio in the network
const uint16_t this_node = 00; // Address of this node in Octal format ( 04,031, etc)
const uint16_t node01 = 01;

void setup() {
SPI.begin();
radio.begin();
network.begin(90, this_node); //(channel, node address)
}

void loop() {
network.update();
unsigned long potValue = analogRead(A0); // Read the potentiometer value
unsigned long angleValue = map(potValue, 0, 1023, 0, 180); // Convert the value to 0-180
RF24NetworkHeader header(node01); // (Address where the data is going)
bool ok = network.write(header, &angleValue, sizeof(angleValue)); // Send the data
}

 

Receiver Code:-

/*
Arduino Wireless Network - Multiple NRF24L01 Tutorial
== Example 01 - Servo Control / Node 01 - Servo motor ==
*/
#include <RF24.h>
#include <RF24Network.h>
#include <SPI.h>
#include <Servo.h>

RF24 radio(8, 10); // nRF24L01 (CE,CSN)
RF24Network network(radio); // Include the radio in the network
const uint16_t this_node = 01; // Address of our node in Octal format ( 04,031, etc)

Servo myservo; // create servo object to control a servo

void setup() {
SPI.begin();
radio.begin();
network.begin(90, this_node); //(channel, node address)
myservo.attach(3); // (servo pin)
}

void loop() {
network.update();
while ( network.available() ) { // Is there any incoming data?
RF24NetworkHeader header;
unsigned long incomingData;
network.read(header, &incomingData, sizeof(incomingData)); // Read the incoming data
myservo.write(incomingData); // tell servo to go to a particular angle
}
}


   
Quote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

I take it that you want to have three Arduinos controlling 3 servos?

Or do you want all three servos to be controlled by one Arduino?

And there is a master Arduino broadcasting instructions using NRF24L01?

Please describe the whole setup!!


   
ReplyQuote
Gulshan
(@gulshan)
Member
Joined: 4 years ago
Posts: 127
Topic starter  

@pugwash

yes There is a master arduino with 3 potentiometers that is transmitting data though nrf24l01 module to an another slave arduino at which the three servo motors are connected is receiving the data through it's nrf24l01 module.  like this:-

Arduino Wireless Servo Motor Control Circuit Diagram.png

 

except at master arduino there are three potentiometers and at slave arduino there are three servo motors


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

@gulshan

Can you post the .ino files?

And has this code been tested? Does it work?


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

   
ReplyQuote
Gulshan
(@gulshan)
Member
Joined: 4 years ago
Posts: 127
Topic starter  

@pugwash

Yes the .inofile works with the RF24 library from  https-github.com/TMRh20RF24.zip

These are the ino files [attac

h]875[/attach] 

don't use rf24 library from maniacbug the code won't compile


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

@gulshan

If you change

bool ok = network.write(header, &angleValue, sizeof(angleValue)); // Send the data

to

network.write(header, &angleValue, sizeof(angleValue)); // Send the data

Does the sketch still work?

I can't see the necessity for the "bool ok" variable!


   
ReplyQuote
Gulshan
(@gulshan)
Member
Joined: 4 years ago
Posts: 127
Topic starter  

@pugwash

wait a sec

 


   
ReplyQuote
Gulshan
(@gulshan)
Member
Joined: 4 years ago
Posts: 127
Topic starter  

no it is giving this following error

 

Arduino: 1.8.10 (Windows 10), Board: "Arduino/Genuino Uno"

G:\aurdino projects with L293D and L298N\Howtomechatronics potentiometer-NRF24L01\Transmitter_Howtomechatronics\Transmitter_Howtomechatronics.ino: In function 'void loop()':

Transmitter_Howtomechatronics:29:11: error: invalid use of member function 'bool RF24Network::write(uint16_t, uint8_t)' (did you forget the '()' ?)

network.write = network.write(header, &angleValue, sizeof(angleValue)); // Send the data

~~~~~~~~^~~~~

Multiple libraries were found for "RF24.h"
Used: C:\Users\GULSHAN\Documents\Arduino\libraries\RF24-master
Multiple libraries were found for "SPI.h"
Used: C:\Program
Multiple libraries were found for "RF24Network.h"
Used: C:\Users\GULSHAN\Documents\Arduino\libraries\RF24Network-master
exit status 1
invalid use of member function 'bool RF24Network::write(uint16_t, uint8_t)' (did you forget the '()' ?)

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.


   
ReplyQuote
Gulshan
(@gulshan)
Member
Joined: 4 years ago
Posts: 127
Topic starter  

The code that you sent me for transmit, I tried it but it is not compiling.


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

@gulshan

OK just leave bool in the sketch!


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

   
ReplyQuote
Gulshan
(@gulshan)
Member
Joined: 4 years ago
Posts: 127
Topic starter  

here it is showing this error while compiling

Arduino: 1.8.10 (Windows 10), Board: "Arduino/Genuino Uno"

C:\Users\GULSHAN\AppData\Local\Temp\arduino_modified_sketch_541351\sketch_nov11a.ino: In function 'void loop()':

sketch_nov11a:38:30: error: expected primary-expression before '<' token

if(incomingData > 180 && < 361){

^

Multiple libraries were found for "RF24.h"
Used: C:\Users\GULSHAN\Documents\Arduino\libraries\RF24-master
Multiple libraries were found for "SPI.h"
Used: C:\Program
Multiple libraries were found for "RF24Network.h"
Used: C:\Users\GULSHAN\Documents\Arduino\libraries\RF24Network-master
Multiple libraries were found for "Servo.h"
Used: C:\Program
Not used: C:\Users\GULSHAN\Documents\Arduino\libraries\Servo-master
exit status 1
expected primary-expression before '<' token

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

@gulshan

if(incomingData > 180 && < 361){

change to

if(incomingData > 180 && incomingData < 361){

should fix that problem!


   
ReplyQuote
Gulshan
(@gulshan)
Member
Joined: 4 years ago
Posts: 127
Topic starter  

@pugwash

Yeah it worked but the transmitting code is still problem and I want to ask you one thing. what is this "bool ok" function, is it necessary ? without bool ok will the network.write work ?


   
ReplyQuote
Page 1 / 5