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);
}