Go Down

Topic: Controlling two servos over nRF24l01+ (Read 2 times) previous topic - next topic

elik745i

Here are changes I've done, now again one channel working Ok, other does not transmit or receiver does not receive it, don't know:

Transmitter:
Code: [Select]
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>

int vals;


void setup(){

  Serial.begin(9600);
 
  Mirf.spi = &MirfHardwareSpi;
  Mirf.init();
  Mirf.setRADDR((byte *)"clie1");
  Mirf.payload = sizeof(vals);
  Mirf.config();
}

void loop(){
  int vals[2];
  vals[0] = analogRead(A0);
Serial.println(vals[1]);
  vals[1] = analogRead(A1);
Serial.println(vals[0]);
  Mirf.setTADDR((byte *)"serv1");
  Mirf.send((byte *) &vals);
  while(Mirf.isSending()){
  }
  delay(10);
}


receiver:

Code: [Select]


#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
#include <Servo.h>


Servo myservo1;
Servo myservo2;
int vals;     
int val1;
int val2;

void setup(){

  Serial.begin(9600);
  myservo1.attach(3);
  myservo2.attach(4);
  Mirf.spi = &MirfHardwareSpi;
  Mirf.init();
  Mirf.setRADDR((byte *)"serv1");
  Mirf.payload = sizeof(vals);
  Mirf.config();
}

void loop(){
 
  while(!Mirf.dataReady()){
  }
  int vals[2];
  Mirf.getData((byte *) &vals);   
  val1 = vals[1];
  val1 = map(val1, 0, 1023, 0, 179);
  val2 = vals[0];
  val2 = map(val2, 0, 1023, 0, 179);
  myservo1.write(val1);
  myservo2.write(val2);
    Serial.println("Position Received..");   
  Serial.println(val1);
  Serial.println(val2);
  delay(10);
}

PaulS

Quote
On the sender, create an array of bytes:

So, you created an array of ints. Why?

You created that array as a local variable with the same name as a global (scalar) variable. Why?

The mapping of int to val is not happening on the sender. Why not?

Your sender is sending two bytes, not four.

Your receiver also has global and local variables of the same name. This is really NOT a good idea. So, stop it. NOW.

If you are going to ask for advice, and then ignore it, I'm going to stop offering it.

elik745i

Hi PaulS

Sorry I'm new to programming and trying my best to understand what is what and how.

the reason I changed byte o int is that when I put int type sender only sends data within range 0-255 and I need data to be transmitted in the range of 0-1023 to make sure Servo motors are working in the range

Could you please change the code and post it back, then I might understand better

Thanks

PaulS

Quote
I need data to be transmitted in the range of 0-1023 to make sure Servo motors are working in the range

No, you don't. The call to map converts the value in the range 0 to 1023 into an angle in the range 0 to 179.

Now, even without taking my shoes off, I can see that 179 is less than 255. So, the question is why not do the mapping on the sender end, and simply send the angle. As a byte.

Until you answer this question, we can make no progress.

elik745i

Yes, thanks for that, looks like now I start thinking :-)

Looks like transmitter side is ready:

Code: [Select]

#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>

byte vals [2];
int val1;
int val2;

void setup(){

  Serial.begin(9600);
 
  Mirf.spi = &MirfHardwareSpi;
  Mirf.init();
  Mirf.setRADDR((byte *)"clie1");
  Mirf.payload = sizeof(vals);
  Mirf.config();
}

void loop(){
  val1 = analogRead(A1);
  val1 = map(val1, 0, 1023, 0, 179);
  vals[0] = val1;
  Serial.println(vals[0]);
 
  val2 = analogRead(A2);
  val2 = map(val2, 0, 1023, 0, 179);
  vals[1] = val2;
  Serial.println(vals[1]);
 
  Mirf.setTADDR((byte *)"serv1");
  Mirf.send((byte *) &vals);
  while(Mirf.isSending()){
  }
  delay(100);
}


Now I'm going to make changes to the receiver, let's see if that will work!

Go Up