2 Channel Remote Control (Part 2)
After 2 weeks of waiting, finally parts are arrived π. Let get to it and built 2 channel remote control. You can easily expand the number of channel, depending on number of IO port available, but for now lets built 2 channel for prove of concept. 2 channel probably enough for simple remote control car.
The Arduino Nano connection for transmitter can be seen below. There is no particular reason why using A5 and A7, you can use any other pin, just assign accordingly on the code. Remember if you need analog input, use the ADC pin.
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
const uint64_t pipeOut = 123456; // Must be same as in the receiver
RF24 radio(9, 10); // Select CE, CSN pin
struct Signal {
byte throttle;
byte rudder;
};
Signal data;
void ResetData() { // Signal lost position
data.throttle = 0;
data.rudder = 0;
}
void setup() {
radio.begin(); // Configure the NRF24 module
radio.openWritingPipe(pipeOut);
radio.setChannel(100);
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS); // Lowest data rate more stable communication
radio.setPALevel(RF24_PA_MAX); // Set output to maximum
radio.stopListening(); // Start radio communication for transmitter
ResetData();
}
// Joystick center and its borders
int Border_Map(int val, int lower, int middle, int upper, bool reverse) {
val = constrain(val, lower, upper);
if (val < middle)
val = map(val, lower, middle, 0, 128);
else
val = map(val, middle, upper, 128, 255);
return (reverse ? 255 - val : val);
}
void loop() {
data.throttle = Border_Map(analogRead(A5), 0, 512, 1023, true); // "true" or "false" to change direction
data.rudder = Border_Map(analogRead(A7), 0, 512, 1023, true);
radio.write(&data, sizeof(Signal));
}
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
const uint64_t pipeIn = 123456; // Must be same as in the transmiter
RF24 radio(9, 10); // Select CE, CSN pin
int ch_width_5 = 0;
int ch_width_7 = 0;
Servo ch5;
Servo ch7;
struct Signal {
byte throttle;
byte rudder;
};
Signal data;
void ResetData() {
data.throttle = 0;
data.rudder = 0;
}
void setup() {
ch5.attach(5);
ch7.attach(7);
ResetData();
radio.begin();
radio.openReadingPipe(1,pipeIn);
radio.setChannel(100);
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS); // Lowest data rate more stable communication
radio.setPALevel(RF24_PA_MAX); // Set output to maximum
radio.startListening(); // Start radio communication for receiver
}
unsigned long lastRecvTime = 0;
void recvData() { // Receive the data
while (radio.available()) {
radio.read(&data, sizeof(Signal));
lastRecvTime = millis();
}
}
void loop() {
recvData();
unsigned long now = millis();
if (now - lastRecvTime > 1000) { // signal lost... Reset data
ResetData();
}
ch_width_5 = map(data.throttle, 0, 255, 1000, 2000);
ch_width_7 = map(data.rudder, 0, 255, 1000, 2000);
ch5.writeMicroseconds(ch_width_5);
ch7.writeMicroseconds(ch_width_7);
}
Testing the built video:
Comments
Post a Comment