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
}
}
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!!
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:-
except at master arduino there are three potentiometers and at slave arduino there are three servo motors
This is how I would handle the transmit side:
/* Arduino Wireless Network - Multiple NRF24L01 Tutorial == Example 01 - Servo Control / Node 00 - Potentiometer == by Dejan, www.HowToMechatronics.com Libraries: nRF24/RF24, 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; int lastPotValue0; int lastPotValue1; int lastPotValue2; int newPotValue0; int newPotValue1; int newPotValue2; int angleValue; void setup() { SPI.begin(); radio.begin(); network.begin(90, this_node); //(channel, node address) //get the initial pot values lastPotValue0 = analogRead(A0); lastPotValue1 = analogRead(A1); lastPotValue2 = analogRead(A2); } void loop() { network.update(); // read the values on Analog Pins A0, A1, A2 newPotValue0 = analogRead(A0); newPotValue1 = analogRead(A1); newPotValue2 = analogRead(A2); // only transmit if the values have changed if(newPotValue0 != lastPotValue0){ angleValue = map(newPotValue0, 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 lastPotValue0 = newPotValue0; } if(newPotValue1 != lastPotValue1){ angleValue = map(newPotValue0, 0, 1023, 0, 180); // Convert the value to 0-180 angleValue = angleValue + 180; RF24NetworkHeader header(node01); // (Address where the data is going) bool ok = network.write(header, &angleValue, sizeof(angleValue)); // Send the data lastPotValue1 = newPotValue1; } if(newPotValue2 != lastPotValue2){ angleValue = map(newPotValue0, 0, 1023, 0, 180); // Convert the value to 0-180 angleValue = angleValue + 360; RF24NetworkHeader header(node01); // (Address where the data is going) bool ok = network.write(header, &angleValue, sizeof(angleValue)); // Send the data lastPotValue2 = newPotValue2; } }
Why I am adding 180 and 360 to the angleValue variable will become clearer on the receive side. It is to differentiate between which Servo should be addressed, I HOPE!
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
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!
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.
The code that you sent me for transmit, I tried it but it is not compiling.
And this is my receive sketch
/* 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 myservo1; // create servo object to control a servo Servo myservo2; // create servo object to control a servo Servo myservo3; // create servo object to control a servo void setup() { SPI.begin(); radio.begin(); network.begin(90, this_node); //(channel, node address) myservo1.attach(3); // (servo pin) myservo2.attach(5); // (servo pin) myservo3.attach(6); // (servo pin) } void loop() { network.update(); while ( network.available() ) { // Is there any incoming data? RF24NetworkHeader header; int incomingData; network.read(header, &incomingData, sizeof(incomingData)); // Read the incoming data if(incomingData < 181){ myservo1.write(incomingData); // tell servo1 to go to a particular angle } if(incomingData > 180 && < 361){ myservo2.write(incomingData - 180); // tell servo2 to go to a particular angle } if(incomingData > 360){ myservo2.write(incomingData - 360); // tell servo3 to go to a particular angle } else{ Serial.println("Pugwash has blown it!"); } } }
It looks like you have duplicate libraries that need deleting!
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.
if(incomingData > 180 && < 361){
change to
if(incomingData > 180 && incomingData < 361){
should fix that problem!
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 ?