Go Down

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

elik745i

Hi everyone,

I'm new to programming and microprocessing, however trying to understand and pushing hard.

Can anyone help to improve the following scetches so I can use 2 potentiometers to control 2 servos?

I have success doing only one at the moment!

Code for transmitter:

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


Servo myservo;  // create servo object to control a servo
int rate;
int val;    // variable to read the value from the analog pin

void setup(){

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

void loop(){

  while(!Mirf.dataReady()){
  }
  Mirf.getData((byte *) &rate);
  val = rate;            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);
  Serial.print("Position Received..");  // sets the servo position according to the scaled value
  Serial.println(val);
  delay(10);
}


Code for receiver:

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

int rate;

void setup(){

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

void loop(){
  rate = analogRead(A0);

  Mirf.setTADDR((byte *)"serv1");

  Mirf.send((byte *) &rate);

  while(Mirf.isSending()){
  }
}

PaulS

I think you have your code switched. Why send an int and then map to a byte? Map first, then send the byte sized value.

The payload size can be the size of two bytes (or two ints), rather than one. The payload can be an array, instead of a scalar.

What have you tried?

elik745i

Maybe, I'm not that good in programming, could you make necessary modification and post back, then I could upload and relpy if that will work or not? Potensiometers hooked to Analog input 0 and 1, servos to Digital 4 and 5 respectively

PaulS

Move this:
Code: [Select]
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
to the sender.

On the sender, create an array of bytes:
Code: [Select]
byte vals[2];

Read analog pin 0, map the value, and store the result in vals[0].
Read analog pin 1, map the value, and store the result in vals[1].

Change this:
Code: [Select]
  Mirf.payload = sizeof(rate);
to
Code: [Select]
  Mirf.payload = sizeof(vals);

Change this:
Code: [Select]
  Mirf.send((byte *) &rate);
to
Code: [Select]
  Mirf.send(vals);

You should, then, be able to see how to change the receiver code to receive two bytes, and send them to the appropriate servos.

elik745i

Well I did some changes, however it does not work, can someone have a look and say what is wrong?

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.setTADDR((byte *)"clie1");
  Mirf.payload = sizeof(vals);
  Mirf.config();
}

void loop(){
  byte vals[2];
  vals[0] = analogRead(A0);
  vals = map(vals, 0, 1023, 0, 179);
Serial.println(vals[0]);
  vals[1] = analogRead(A1);
  vals = map(vals, 0, 1023, 0, 179);
Serial.println(vals[1]);
  Mirf.setTADDR((byte *)"serv1");
  Mirf.send((byte *) &vals);

  while(Mirf.isSending()){
  }
}

it says "invalid conversion from byte to long int"

when I remove this line " vals = map(vals, 0, 1023, 0, 179);"

and code for receiver:
Code: [Select]

// http://www.bajdi.com
// Nrf24L01 connected to Arduino Uno
// Nrf24L01 connection details http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
// Receives analog value from transmitter and display it on serial monitor


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


Servo myservo1;
Servo myservo2; // create servo object to control a servo
int vals;    // variable to read the value from the analog pin
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()){
  }
  Mirf.getData((byte *) &vals);    // reads the value of the potentiometer (value between 0 and 1023)
 
  byte vals[2];
val1 = vals[0];
val2 = vals[1]; // scale it to use it with the servo (value between 0 and 180)
  myservo1.write(val1);
  myservo2.write(val2);
    Serial.print("Position Received..");  // sets the servo position according to the scaled value
  Serial.println(val1);
  Serial.println(val2);
  delay(10);
}

it's compilling and after upload still nothing works!

Go Up