Notifications
Clear all

Upgrading This code to control 4 Servos

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

I wanted to control 4 Servos by using this code which only allows me to control only one servo by potentiometer through NRF24L01 Module. But I am Having Trouble in Modifying the array of these codes. Can Any one Help me Please. These are the  codes:


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

@Gulshan,

Apologies, originally posted this to the wrong thread:

I mentioned in one of your other posts, that read does not return a bool, but rather void... why are you posting the same erroneous code again without fixing it?  I'm sure this code would have provided an error message too, but you have not told us about it?  In all honesty, to obtain the best help, you really need to help us to help you... I can never emphasise this point strongly enough.

 


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

@frogandtoad

No it works Fine . Maybe you are getting the error like "void is not ignored as it ought to be". it is actually because of wrong nrf24l01 library. This Library should be used with this code.

 


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

why are you posting the same erroneous code again without fixing it? 

Actually the original code works fine with one servo. maybe we can modify it. 


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

@gulshan

Posted by: @gulshan

No it works Fine . Maybe you are getting the error like "void is not ignored as it ought to be". it is actually because of wrong nrf24l01 library. This Library should be used with this code.

OMG... I see now you're using a different library 🙂


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

why are you posting the same erroneous code again without fixing it? 

Actually the original code works fine with one servo. maybe we can modify it. 

Ok, same issue though... didn't pugwash provide code for this exact purpose, using 3 servos?  Please be more specific about the trouble you're having with arrays... I've showed you how they work before too, so where exactly is the problem in your understanding?  I'm happy to help, but like I said, you need to help us too!


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

@frogandtoad

Yeah He did, but I only wanted to know why i couldn't modify this code while it looks so simple to understand. Actually last time when you told me about the modifications there was some problem in my Arduino software so I wasn't able to compile any code in it. I reinstalled arduino ide again but Accidentally i deleted that modified code you gave me earlier. That is why I thought to reintroduce this topic here.


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

@gulshan

Ok, first of all, you need to provide the "address of" operator for the msg using the ampersand character: '&'.

Also, try the following (not tested):

void loop() {
  while (radio.available() and radio.read(&msg, sizeof(msg))) {
    myServo.write(msg[0]);
   }
 }

Same for the following:

radio.write(&msg, sizeof(msg));

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

"address of"

I think this is the pipeline(address) which works as a link between the two codes

Posted by: @frogandtoad
void loop() {
  while (radio.available() and radio.read(&msg, sizeof(msg))) {
    myServo.write(msg[0]);
   }
 }

Same for the following:

radio.write(&msg, sizeof(msg));

Yes the code is compilying too. let me try it practically.


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

@gulshan

"address of"

I think this is the pipeline(address) which works as a link between the two codes

No... in C and C++, the ampersand operator '&' is known as the "address of" operator.

 


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

@frogandtoad

Practically the code works too. Actually in that video from where I got this code he didn't mentioned anything about any address. the Maker of this code is Mert Arduino. He only explained about how we can connect two arduino through single pipeline through this code. it is the same way he mentioned it in his robot car video. But last time you told me something about increasing the array size.maybe we can try it.


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

So did the code I provided fix the problem?

Yes, I showed you how to use array... it's still in the other post.
Let me know what problem you're having with them, because I'm pretty sure pugwash also provided a complete sample.


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

Yes so from our last conversation these are the codes that i modified. Here the transmitter code is working fine but at reciver the servo is not responding.

/*
---- 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,10); //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,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(2); //3 is a digital pin to which servo signal connected.
myServo1.attach(3);
myServo2.attach(4);
myServo3.attach(5);
myServo4.attach(6);

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
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@gulshan

Um... how can even the transmitter code be working without the "address of" operator I most recently spoke about, let alone the receiver?

 


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

@gulshan

Attaching servos to pins 2 & 4 are not going to have any effect.

They are not PWM pins. I thought I had already explained this before!

Servos are controlled by PWM signals.


   
ReplyQuote
Page 1 / 3