Transmitter / Receiver problem

Hi all, im new to Arduino but not in electronic. Well maybe is similiar question asked before but i didn’t find it.

Here is my problem:

I have 2 Arduino Nano, 2 NRF24l01 modules, 3 pot’s, 1 dc motor and 2 servos.

I have this code, 1 pot is for motor, one for servo and they works perfect. But, when i plug second servo, i can’t control that servo from third pot. Problem is, how to control separate servo by each pot.

This is Bait Boat, dc motor + rudder, now i want one servo for door. Can somebody tell me what is wrong here, please.

TX

#include <SPI.h>
#include “RF24.h”
RF24 radio(9,10);
const uint64_t pipe = 0xF0F0F0F000LL;
int msg[1];
int potpin_1 = A3;
int val_1;
int potpin_2 = A5;
int val_2;
int potpin_3 = A6;
int val_3;

void setup(void){
radio.begin();
radio.openWritingPipe(pipe);

}
void loop() {
val_1 = analogRead(potpin_1),val_1 = map(val_1, 0, 1023, 0, 127),msg[0] = val_1,radio.write(msg, 1);
val_2 = analogRead(potpin_2),val_2 = map(val_2, 0, 1023, 128, 255),msg[0] = val_2,radio.write(msg, 1);
val_3 = analogRead(potpin_3),val_3 = map(val_3, 0, 1023, 128, 127),msg[0] = val_3,radio.write(msg, 1);

}

RX

#include <Servo.h>
#include <SPI.h>
#include “RF24.h”
Servo servo1;
Servo servo2;

RF24 radio(9,10);
const uint64_t pipe = 0xF0F0F0F000LL;
int msg[1];
int data;
int pos;
int fspeed; // forward speed
int bspeed; // backward speed
const int in1 = 2; // direction pin 1
const int in2 = 4; // direction pin 2
const int ena = 5; // PWM pin to change speed

void setup()
{

pinMode(in1, OUTPUT); // connection to L298n
pinMode(in2, OUTPUT); // connection to L298n
pinMode(ena, OUTPUT); // connection to L298n
servo1.attach(6);
servo1.attach(7);
delay(50);
radio.begin();
radio.openReadingPipe(1,pipe);
radio.startListening();
}

void loop()
{
if (radio.available())radio.read(msg, 1);
if (msg[0] <127 && msg[0] >-1)data = msg[0], pos = map(data, 0, 126, 7, 67),servo1.write(pos);
if (msg[0] <127 && msg[0] >-1)data = msg[0], pos = map(data, 0, 126, 7, 67),servo2.write(pos);

if (msg[0] >=128 && msg[0] <=189)data = msg[0], bspeed = map(data, 255, 201, 0, 200), backward(bspeed);
if (msg[0] >=201 && msg[0] <=255)data = msg[0], fspeed = map(data, 189, 128, 0, 200), forward(fspeed);
if (msg[0] >190 && msg[0] <200)data = msg[0], stop();
}

void stop()
{

analogWrite(ena, 50);
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
}

void forward(int fspeed)
{

digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(ena, fspeed);
}

void backward(int bspeed)
{

digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(ena, bspeed);
}

val_3 = analogRead(potpin_3),val_3 = map(val_3, 0, 1023, 128, 127),msg[0] = val_3,radio.write(msg, 1);

Statements are supposed to be one per line. ending with ;, not strung together like this crap separated by commas.

You CAN send more than one byte in a message. Mapping everything from the third pot to 128, which is in the range of the second pot makes no sense.

Thank's PaulS. I dont know again what i need to write for third channel. :frowning:
Can somebody write?

I dont know again what i need to write for third channel.

How do YOU want three pots to control two servos?

This,

val_1 = analogRead(potpin_1),val_1 = map(val_1, 0, 1023, 0, 127),msg[0] = val_1,radio.write(msg, 1);

is much easier to read like this

val_1 = analogRead(potpin_1);
val_1 = map(val_1, 0, 1023, 0, 127);
msg[0] = val_1;
radio.write(msg, 1);

As written you are just sending one byte in each message. You could put all of the potentiometer values in a single message.

I got my NRF24 working following the guidance here. The basic example sends multiple bytes.

Please use the code button </> so your code looks like this and is easy to copy to a text editor

…R

PaulS:
How do YOU want three pots to control two servos?

Nooo. :slight_smile: I have three pots:

  1. for dc motor
  2. for rudder servo
    3.for door servo

First and second works, but when atach and type codes for door servo, both servos moving crazy or moving in sync.

  if (msg[0] >=128 && msg[0] <=189)data = msg[0], bspeed = map(data, 255, 201, 0, 200), backward(bspeed);
  if (msg[0] >=201 && msg[0] <=255)data = msg[0], fspeed = map(data, 189, 128, 0, 200), forward(fspeed);
  if (msg[0] >190 && msg[0] <200)data = msg[0], stop();

What if msg[0] is equal to 190 or 200?

aarg:

  if (msg[0] >=128 && msg[0] <=189)data = msg[0], bspeed = map(data, 255, 201, 0, 200), backward(bspeed);

if (msg[0] >=201 && msg[0] <=255)data = msg[0], fspeed = map(data, 189, 128, 0, 200), forward(fspeed);
  if (msg[0] >190 && msg[0] <200)data = msg[0], stop();




What if msg[0] is equal to 190 or 200?

This function is used to stop dc motor.

You need to send more data. Each value read from a pot should be used to control ONE servo. Send the servo number and the mapped pot value as two bytes.

It is NOT at all clear what the third pot is for, but send it's value for use #3.

Expect, then, to always read two bytes - use number and value. If the use number is 1, move servo #1. If it is 2, move servo #2. If it is 3, let the magic smoke out (or whatever).

ReptiLe88:
This function is used to stop dc motor.

No. Read carefully. Stop motor is when msg[0]>190 or <200. That’s not what I said!

Suppose msg[0] is equal to 200. Look carefully at the if statements. None of them will be executed.

Please Paul, write some test code. Im beginer.
I dont know how to call two bytes. This thing confuse me totaly last night.

ReptiLe88:
Please Paul, write some test code.

This is not going to be pretty.

aarg:
No. Read carefully. Stop motor is when msg[0]>190 or <200. That’s not what I said!

Suppose msg[0] is equal to 200. Look carefully at the if statements. None of them will be executed.

Well, my understanding of codes is noob like, but this thing works perfect with two channels only.

ReptiLe88:
write some test code. Im beginer.
I dont know how to call two bytes. This thing confuse me totaly last night.

That's why I gave you this link in Reply #4

...R

ReptiLe88:
Well, my understanding of codes is noob like, but this thing works perfect with two channels only.

So, you have no idea what I'm talking about? The thing is, people will look at how you respond to my inquiry. If you can't respond to my help, maybe you wouldn't respond to their help. So their time would be wasted in answering.

aarg:
So, you have no idea what I'm talking about? The thing is, people will look at how you respond to my inquiry. If you can't respond to my help, maybe you wouldn't respond to their help. So their time would be wasted in answering.

If i wanna help someone, that's not wating time for me...

Please Paul, write some test code.

byte msg[2];
msg[0] = 1;
msg[1] = potValue;

You know how to read the pin that the pot is connected to. You know how to map that to a byte-sized value. You know how to send a message that is one byte long. I’m guessing that within an hour or two, you can figure out how to send a two byte message.

If i wanna help someone, that's not wating [sic] time for me...

If I try to help someone and they waste my help, that is a waste of my time.

PaulS:

byte msg[2];

msg[0] = 1;
msg[1] = potValue;




You know how to read the pin that the pot is connected to. You know how to map that to a byte-sized value. You know how to send a message that is one byte long. I'm guessing that within an hour or two, you can figure out how to send a two byte message.

Thanks PaulS.

Below is some test code for multiple pots and servos. You may not need the dead band part of the code. How are you powering the servos and motor? They need an external power supply.

//zoomkat multi pot/servo test 3-23-13
//includes dead band for testing and limit servo hunting
//view output using the serial monitor

#include <Servo.h> 
Servo myservo1;  //declare servos
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;

int potpin1 = 0;  //analog input pin A0
int potpin2 = 1;
int potpin3 = 2;
int potpin4 = 3;
int potpin5 = 4;

int newval1, oldval1;  //pot input values
int newval2, oldval2;
int newval3, oldval3;
int newval4, oldval4;
int newval5, oldval5;

void setup() 
{
  Serial.begin(9600);  
  myservo1.attach(2);  
  myservo2.attach(3);
  myservo3.attach(4);
  myservo4.attach(5);
  myservo5.attach(6);
  Serial.println("testing multi pot servo");  
}

void loop()
{ 
  newval1 = analogRead(potpin1);           
  newval1 = map(newval1, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval1 > (oldval1+2)){ //dead band 
    myservo1.write(newval1); //position the servo
    Serial.print("1- ");
    Serial.println(newval1); //print the new value for testing 
    oldval1=newval1; //set the current old value
  }

  newval2 = analogRead(potpin2);
  newval2 = map(newval2, 0, 1023, 0, 179);
  if (newval2 < (oldval2-2) || newval2 > (oldval2+2)){  
    myservo2.write(newval2);
    Serial.print("2- ");    
    Serial.println(newval2);
    oldval2=newval2;
  }

  newval3 = analogRead(potpin3);           
  newval3 = map(newval3, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval3 > (oldval3+2)){  
    myservo1.write(newval3);
    Serial.print("3- ");
    Serial.println(newval3);
    oldval3=newval3;
  }

  newval4 = analogRead(potpin4);           
  newval4 = map(newval4, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval4 > (oldval4+2)){  
    myservo1.write(newval4);
    Serial.print("4- ");
    Serial.println(newval4);
    oldval4=newval4;
  }

  newval5 = analogRead(potpin5);           
  newval5 = map(newval5, 0, 1023, 0, 179); 
  if (newval1 < (oldval5-2) || newval5 > (oldval5+2)){  
    myservo1.write(newval5);
    Serial.print("5- ");
    Serial.println(newval5);
    oldval5=newval5;
  } 
  delay(50);  //to slow loop for testing
}