Notifications
Clear all

Upgrading This code to control 4 Servos

31 Posts
4 Users
0 Likes
6,691 Views
Gulshan
(@gulshan)
Member
Joined: 5 years ago
Posts: 127
Topic starter  

@pugwash

?Sorry, I changed it. I was saying that the servo side is not responding at all like nothing is going over receiver

/*
---- Receiver Code ----
Mert Arduino Tutorial & Projects (YouTube)
Please Subscribe for Support
*/

#include <Servo.h> //the library which helps us to control the servo motor
#include <SPI.h> //the communication interface with the modem
#include "RF24.h" //the library which helps us to control the radio modem

Servo myServo;
Servo myServo1;
Servo myServo2;
Servo myServo3;
Servo myServo4;//define the servo name

RF24 radio(8,10); /*This object represents a modem connected to the Arduino.
Arguments 5 and 10 are a digital pin numbers to which signals
CE and CSN are connected.*/

const uint64_t pipe = 0xE8E8F0F0E1LL; //the address of the modem,that will receive data from the Arduino.

int msg1[5];

void setup(){
myServo.attach(3); //3 is a digital pin to which servo signal connected.
myServo1.attach(5);
myServo2.attach(6);
myServo3.attach(9);
myServo4.attach(10);

radio.begin(); //it activates the modem.
radio.openReadingPipe(5, pipe); //determines the address of our modem which receive data.
radio.startListening(); //enable receiving data via modem
}

void loop(){
if(radio.available()){ //checks whether any data have arrived at the address of the modem
bool done = false; //returns a “true” value if we received some data, or “false” if no data.
while (!done) {
done = radio.read(msg1, 5);
myServo.write(msg1[0]);
myServo1.write(msg1[1]);
myServo2.write(msg1[2]);
myServo3.write(msg1[3]);
myServo4.write(msg1[4]);

}
}
}

---------------------------------------------------------------------------------------------------

How about this ?


   
ReplyQuote
(@zeferby)
Member
Joined: 5 years ago
Posts: 355
 

At least pin 2 is not PWM on the UNO.

Pins 3, 5, 6, 9, 10, 11 are PWM pins on both UNO and MEGA :

https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/

Eric


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@gulshan

Same issue


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

@frogandtoad

what if we check if receiving end is getting anything or not ?


   
ReplyQuote
(@zeferby)
Member
Joined: 5 years ago
Posts: 355
 

By the way I never noticed that info at the bottom of the page until now :

Notes and Warnings

The PWM outputs generated on pins 5 and 6 will have higher-than-expected duty cycles. This is because of interactions with the millis() and delay() functions, which share the same internal timer used to generate those PWM outputs. This will be noticed mostly on low duty-cycle settings (e.g. 0 - 10) and may result in a value of 0 not fully turning off the output on pins 5 and 6.

Eric


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@gulshan

It's missing the operator as well... so fix that first and see what happens 🙂


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@zeferby

I've read that before too, that the pins also operate at a higher frequencies.


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

@frogandtoad

But if it is missing that operator then it shouldn't work for even one servo


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@gulshan

Sorry, been here a while and it's getting very late, not to mentioned I'm now confused what is actually the problem now?  Are you telling me it's working for one servo and not the rest?  One pin could be receiving ghost values, I just don't know.  Go to your IDE EDIT->PREFERENCES and enable some debugging features and try to compile again and find the errors - Try that for starters and I'll check back tomorrow, too tired now.

Though it is a little more complex for your skill level, I'll look into putting together some example code, for what I think is the best way to handle such an application.  It will be more versatile than what you've dealt with to date, and you or anyone else can adapt it to suit your needs.

 


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

@gulshan

Here is another conflict on pin 10:

RF24 radio(8,10);
myServo4.attach(10);

change to:

RF24 radio(8,7);

Now you have got 5 PWM pins to choose from 3, 5, 6, 9 & 10.

But be sure to take @zeferby warnings into account.

The PWM outputs generated on pins 5 and 6 will have higher-than-expected duty cycles. This is because of interactions with the millis() and delay() functions, which share the same internal timer used to generate those PWM outputs. This will be noticed mostly on low duty-cycle settings (e.g. 0 - 10) and may result in a value of 0 not fully turning off the output on pins 5 and 6.

I don't really see much of a problem with the delay() function as when it is initiated the AT328 stops everything but leaves all registers alone until the microprocessor starts running again. The conflict with the millis() function is definitely more acute.

A minor correction to some premises already uttered in this thread. The ampersand (&) is actually referred to as a "pointer" and points to the start address of the variable in question. So instead of passing a variable, the value of the variable is passed by referencing the storage address.


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

@pugwash

No the servo responded for a little sec but it is still not receiving anything. Please take a look is still missing ?

 

/*
---- Transmitter Code ----
Mert Arduino Tutorial & Projects (YouTube)
Please Subscribe for Support
*/

#include <SPI.h> //the communication interface with the modem
#include "RF24.h" //the library which helps us to control the radio modem

int msg1[5];
RF24 radio(8,7); //8 and 10 are a digital pin numbers to which signals CE and CSN are connected.

const uint64_t pipe = 0xE8E8F0F0E1LL; //the address of the modem, that will receive data from Arduino.

void setup(void){
radio.begin(); //it activates the modem.
radio.openWritingPipe(pipe); //sets the address of the receiver to which the program will send data.
}

void loop(void){
msg1[0] = map (analogRead(0), 0, 1023, 0, 179);
msg1[1] = map (analogRead(1), 0, 1023, 0, 179);
msg1[2] = map (analogRead(2), 0, 1023, 0, 179);
msg1[3] = map (analogRead(3), 0, 1023, 0, 179);
msg1[4] = map (analogRead(4), 0, 1023, 0, 179);

radio.write(msg1, 5);

}

-------------------------------------------------------------------------------------------

/*
---- Receiver Code ----
Mert Arduino Tutorial & Projects (YouTube)
Please Subscribe for Support
*/

#include <Servo.h> //the library which helps us to control the servo motor
#include <SPI.h> //the communication interface with the modem
#include "RF24.h" //the library which helps us to control the radio modem

Servo myServo;
Servo myServo1;
Servo myServo2;
Servo myServo3;
Servo myServo4;//define the servo name

RF24 radio(8,7); /*This object represents a modem connected to the Arduino.
Arguments 5 and 10 are a digital pin numbers to which signals
CE and CSN are connected.*/

const uint64_t pipe = 0xE8E8F0F0E1LL; //the address of the modem,that will receive data from the Arduino.

int msg1[5];

void setup(){
myServo.attach(3); //3 is a digital pin to which servo signal connected.
myServo1.attach(5);
myServo2.attach(6);
myServo3.attach(9);
myServo4.attach(10);

radio.begin(); //it activates the modem.
radio.openReadingPipe(5, pipe); //determines the address of our modem which receive data.
radio.startListening(); //enable receiving data via modem
}

void loop(){
if(radio.available()){ //checks whether any data have arrived at the address of the modem
bool done = false; //returns a “true” value if we received some data, or “false” if no data.
while (!done) {
done = radio.read(msg1,5);
myServo.write(msg1[0]);
myServo1.write(msg1[1]);
myServo2.write(msg1[2]);
myServo3.write(msg1[3]);
myServo4.write(msg1[4]);

}
}
}


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

@gulshan

I don't know why you want to reinvent the wheel. I have already provided you with a method to control the individual servos, and for anybody who wishes to study them, the files have been posted in the "Motors & Control" subsection, under the post title "Remote Control of Three Servos". Which can be easily be adapted for use with this radio modem instead of the nrf24l01 modules and up to three more servos if the pin allocations are changed.

A brief explanation of the control logic:

Six pots are connected to A0...A6.

The value from each pot is mapped to 0...180

To distinguish which pot value is to be addressed, 1000 is added to the value from A1, 2000 is added to the value from A2, 3000 is added to the value from A3 etc. etc..

When the receiver receives a value, it can identify which servo is to be addressed by first digit i.e 0...6, which is then subtracted from the incoming value and sent to the respective servo.

There are undoubtedly other methods of doing this but my method seems to me to be logical, simple and compact.

KISS (Keep It Short & Simple)


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

 

Posted by: @pugwash

KISS (Keep It Short & Simple)

Yeah You are Right maybe i really should stop here cause maybe we can't change every code in our own way. Thank you for giving me your precious time.


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

@gulshan

I have two basic issues with the code you are trying to adapt.

  • I find it totally unnecessary to continually transmit an array, as only one value changes at a time.
  • And if you run the circuits from a battery, it will drain them faster than necessary. The fact that you are constantly transmitting the same values while the pot values are not changing is wasting battery life.

In my posted code, a transmission only occurs when the value on one of the pots changes.


   
ReplyQuote
Gulshan
(@gulshan)
Member
Joined: 5 years ago
Posts: 127
Topic starter  
Posted by: @pugwash

And if you run the circuits from a battery, it will drain them faster than necessary.

I heavily agree with this point cause it dropped me power bank 13% low while I was testing it.


   
ReplyQuote
Page 2 / 3