I was trying to make the hand gesture to the RC car by following:
Hand Gesture Controlled Car Using Arduino:fist_right: (youtube. com)
And I have a problem with receiving data from the transmitter. I can see it sends data from the transmitter code on a serial monitor, and I tested the NRF24L01+ module connected from my receiver on a serial monitor. But I couldn't receive data from my transmitter. Can someone help?
I can't follow the text, make a block diagram, please. Are you using TWO NRF24L01 devices?
No. I am using nano for transmitter and mega2560 for receiver. I just figured out how to receive data and was able to make the Rc car to move, but it moves slowly.
Reveiver code here:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define SIGNAL_TIMEOUT 500 // Signal timeout in milliseconds
#define MAX_MOTOR_SPEED 1023 // Maximum motor speed
const uint64_t pipeIn = 0xF9E8F0F0E1LL;
RF24 radio(8, 9); // CE, CSN pins
unsigned long lastRecvTime = 0;
struct PacketData
{
byte xAxisValue;
byte yAxisValue;
} receiverData;
// Motor pins
int enableRightMotor = 5;
int rightMotorPin1 = 3; // Change to match your setup
int rightMotorPin2 = 2; // Change to match your setup
int enableLeftMotor = 6;
int leftMotorPin1 = 7; // Change to match your setup
int leftMotorPin2 = 4; // Change to match your setup
void setup()
{
pinMode(enableRightMotor, OUTPUT);
pinMode(rightMotorPin1, OUTPUT);
pinMode(rightMotorPin2, OUTPUT);
pinMode(enableLeftMotor, OUTPUT);
pinMode(leftMotorPin1, OUTPUT);
pinMode(leftMotorPin2, OUTPUT);
rotateMotor(0, 0);
radio.begin();
radio.setDataRate(RF24_250KBPS);
radio.openReadingPipe(1, pipeIn);
radio.startListening(); // Start the radio receiver
#ifdef PRINT_DEBUG
Serial.begin(115200);
#endif
}
void loop()
{
int rightMotorSpeed = 0;
int leftMotorSpeed = 0;
// Check if RF is connected and packet is available
if (radio.isChipConnected() && radio.available())
{
radio.read(&receiverData, sizeof(PacketData));
// Map joystick values directly to motor speeds
rightMotorSpeed = map(receiverData.yAxisValue, 0, 254, -MAX_MOTOR_SPEED, MAX_MOTOR_SPEED);
leftMotorSpeed = map(receiverData.yAxisValue, 0, 254, -MAX_MOTOR_SPEED, MAX_MOTOR_SPEED);
// Invert motor speeds for backward movement
if (rightMotorSpeed < 0)
{
rightMotorSpeed = -rightMotorSpeed;
leftMotorSpeed = -leftMotorSpeed;
}
// Apply constraints to motor speeds
rightMotorSpeed = constrain(rightMotorSpeed, -MAX_MOTOR_SPEED, MAX_MOTOR_SPEED);
leftMotorSpeed = constrain(leftMotorSpeed, -MAX_MOTOR_SPEED, MAX_MOTOR_SPEED);
rotateMotor(rightMotorSpeed, leftMotorSpeed);
lastRecvTime = millis();
#ifdef PRINT_DEBUG
Serial.println(receiverData.xAxisValue);
Serial.println(receiverData.yAxisValue);
#endif
}
else
{
// Signal lost. Reset the motor to stop
unsigned long now = millis();
if (now - lastRecvTime > SIGNAL_TIMEOUT)
{
rotateMotor(0, 0);
}
}
}
void rotateMotor(int rightMotorSpeed, int leftMotorSpeed)
{
// Control right motor
if (rightMotorSpeed >= 0)
{
digitalWrite(rightMotorPin1, HIGH);
digitalWrite(rightMotorPin2, LOW);
}
else
{
digitalWrite(rightMotorPin1, LOW);
digitalWrite(rightMotorPin2, HIGH);
}
// Control left motor
if (leftMotorSpeed >= 0)
{
digitalWrite(leftMotorPin1, HIGH);
digitalWrite(leftMotorPin2, LOW);
}
else
{
digitalWrite(leftMotorPin1, LOW);
digitalWrite(leftMotorPin2, HIGH);
}
// Set motor speeds
analogWrite(enableRightMotor, abs(rightMotorSpeed));
analogWrite(enableLeftMotor, abs(leftMotorSpeed));
}
and transmitter code here:
//#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define PRINT_DEBUG //Uncomment this line if you want to print the MPU6050 initialization information on serial monitor
const uint64_t pipeOut = 0xF9E8F0F0E1LL;
RF24 radio(8, 9); // select CE,CSN pin
struct PacketData
{
byte xAxisValue;
byte yAxisValue;
} data;
MPU6050 mpu;
bool dmpReady = false;
uint8_t devStatus;
uint16_t packetSize;
uint8_t fifoBuffer[64];
Quaternion q;
VectorFloat gravity;
float ypr[3];
void setupRadioTransmitter()
{
radio.begin();
radio.setDataRate(RF24_250KBPS);
radio.openWritingPipe(pipeOut);
radio.stopListening(); //start the radio communication for Transmitter
data.xAxisValue = 127; // Center
data.yAxisValue = 127; // Center
}
void setupMPU()
{
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
Wire.setClock(400000);
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif
#ifdef PRINT_DEBUG
Serial.begin(115200);
while (!Serial);
Serial.println(F("Initializing I2C devices..."));
#endif
mpu.initialize();
#ifdef PRINT_DEBUG
Serial.println(F("Testing device connections..."));
Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
Serial.println(F("\nSend any character to begin DMP programming and demo: "));
while (Serial.available() && Serial.read());
while (!Serial.available());
while (Serial.available() && Serial.read());
Serial.println(F("Initializing DMP..."));
#endif
devStatus = mpu.dmpInitialize();
if (devStatus == 0)
{
mpu.CalibrateAccel(6);
mpu.CalibrateGyro(6);
#ifdef PRINT_DEBUG
mpu.PrintActiveOffsets();
Serial.println(F("Enabling DMP..."));
#endif
mpu.setDMPEnabled(true);
#ifdef PRINT_DEBUG
Serial.println(F("DMP ready! Waiting for first interrupt..."));
#endif
dmpReady = true;
packetSize = mpu.dmpGetFIFOPacketSize();
}
else
{
#ifdef PRINT_DEBUG
Serial.print(F("DMP Initialization failed (code "));
Serial.print(devStatus);
Serial.println(F(")"));
#endif
}
}
void setup()
{
setupRadioTransmitter();
setupMPU();
}
void loop()
{
if (!dmpReady) return;
if (mpu.dmpGetCurrentFIFOPacket(fifoBuffer))
{
mpu.dmpGetQuaternion(&q, fifoBuffer);
mpu.dmpGetGravity(&gravity, &q);
mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
int xAxisValue = constrain(ypr[2] * 180/M_PI, -90, 90);
int yAxisValue = constrain(ypr[1] * 180/M_PI, -90, 90);
data.xAxisValue = map(xAxisValue, -90, 90, 0, 254);
data.yAxisValue = map(yAxisValue, -90, 90, 254, 0);
radio.write(&data, sizeof(PacketData));
#ifdef PRINT_DEBUG
Serial.print("x:");
Serial.println(xAxisValue);
Serial.print("y:");
Serial.println(yAxisValue);
#endif
}
}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.