transmitter code
//These are needed for MPU
#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
//These are need for RF handlling
#include
#include
#include
//#define PRINT_DEBUG //Uncomment this line if you want to print the MPU6050 initialization information on serial monitor
// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
// is used in I2Cdev.h
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
#include "Wire.h"
#endif
// MPU control/status vars
MPU6050 mpu;
bool dmpReady = false; // set true if DMP init was successful
uint8_t devStatus; // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize; // expected DMP packet size (default is 42 bytes)
uint8_t fifoBuffer[64]; // FIFO storage buffer
Quaternion q; // [w, x, y, z] quaternion container
VectorFloat gravity; // [x, y, z] gravity vector
float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector
// RF control
const uint64_t pipeOut = 0xF9E8F0F0E1LL; //IMPORTANT: The same as in the receiver 0xF9E8F0F0E1LL
RF24 radio(8, 9); // select CE,CSN pin
struct PacketData
{
byte xAxisValue;
byte yAxisValue;
} data;
void setupRadioTransmitter()
{
radio.begin();
radio.setDataRate(RF24_250KBPS);
radio.openWritingPipe(pipeOut);
radio.stopListening(); //start the radio comunication for Transmitter
data.xAxisValue = 127; // Center
data.yAxisValue = 127; // Center
}
void setupMPU()
{
// join I2C bus (I2Cdev library doesn't do this automatically)
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
Wire.setClock(400000); // 400kHz I2C clock. Comment this line if having compilation difficulties
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif
#ifdef PRINT_DEBUG
// initialize serial communication
Serial.begin(115200);
while (!Serial); // wait for Leonardo enumeration, others continue immediately
// initialize device
Serial.println(F("Initializing I2C devices..."));
#endif
mpu.initialize();
#ifdef PRINT_DEBUG
// verify connection
Serial.println(F("Testing device connections..."));
Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
// wait for ready
Serial.println(F("\nSend any character to begin DMP programming and demo: "));
while (Serial.available() && Serial.read()); // empty buffer
while (!Serial.available()); // wait for data
while (Serial.available() && Serial.read()); // empty buffer again
// load and configure the DMP
Serial.println(F("Initializing DMP..."));
#endif
devStatus = mpu.dmpInitialize();
// make sure it worked (returns 0 if so)
if (devStatus == 0)
{
// Calibration Time: generate offsets and calibrate our MPU6050
mpu.CalibrateAccel(6);
mpu.CalibrateGyro(6);
#ifdef PRINT_DEBUG
mpu.PrintActiveOffsets();
// turn on the DMP, now that it's ready
Serial.println(F("Enabling DMP..."));
#endif
mpu.setDMPEnabled(true);
// set our DMP Ready flag so the main loop() function knows it's okay to use it
#ifdef PRINT_DEBUG
Serial.println(F("DMP ready! Waiting for first interrupt..."));
#endif
dmpReady = true;
// get expected DMP packet size for later comparison
packetSize = mpu.dmpGetFIFOPacketSize();
}
else
{
// ERROR!
// 1 = initial memory load failed
// 2 = DMP configuration updates failed
// (if it's going to break, usually the code will be 1)
#ifdef PRINT_DEBUG
Serial.print(F("DMP Initialization failed (code "));
Serial.print(devStatus);
Serial.println(F(")"));
#endif
}
}
void setup()
{
//This is to set up radio transmitter for rf
setupRadioTransmitter();
//This is to set up MPU6050 sensor
setupMPU();
}
void loop()
{
// if programming failed, don't try to do anything
if (!dmpReady) return;
// read a packet from FIFO. Get the Latest packet
if (mpu.dmpGetCurrentFIFOPacket(fifoBuffer))
{
// display Euler angles in degrees
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.println(xAxisValue);
Serial.println(yAxisValue);
#endif
}
}`
Your topic has been moved. Please do not post in "Uncategorized"; see the sticky topics in Uncategorized - Arduino Forum.
What kind of help do you need? Are you stuck somewhere? If yes, where and what did you try?
i do all instructions and connected wires as an image but still not work
You might have more luck in your native language - and USE CODE YAGS
code transmitter
//These are needed for MPU
#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
//These are need for RF handlling
#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
// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
// is used in I2Cdev.h
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
#include "Wire.h"
#endif
// MPU control/status vars
MPU6050 mpu;
bool dmpReady = false; // set true if DMP init was successful
uint8_t devStatus; // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize; // expected DMP packet size (default is 42 bytes)
uint8_t fifoBuffer[64]; // FIFO storage buffer
Quaternion q; // [w, x, y, z] quaternion container
VectorFloat gravity; // [x, y, z] gravity vector
float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector
// RF control
const uint64_t pipeOut = 0xF9E8F0F0E1LL; //IMPORTANT: The same as in the receiver 0xF9E8F0F0E1LL
RF24 radio(8, 9); // select CE,CSN pin
struct PacketData
{
byte xAxisValue;
byte yAxisValue;
} data;
void setupRadioTransmitter()
{
radio.begin();
radio.setDataRate(RF24_250KBPS);
radio.openWritingPipe(pipeOut);
radio.stopListening(); //start the radio comunication for Transmitter
data.xAxisValue = 127; // Center
data.yAxisValue = 127; // Center
}
void setupMPU()
{
// join I2C bus (I2Cdev library doesn't do this automatically)
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
Wire.setClock(400000); // 400kHz I2C clock. Comment this line if having compilation difficulties
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif
#ifdef PRINT_DEBUG
// initialize serial communication
Serial.begin(115200);
while (!Serial); // wait for Leonardo enumeration, others continue immediately
// initialize device
Serial.println(F("Initializing I2C devices..."));
#endif
mpu.initialize();
#ifdef PRINT_DEBUG
// verify connection
Serial.println(F("Testing device connections..."));
Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
// wait for ready
Serial.println(F("\nSend any character to begin DMP programming and demo: "));
while (Serial.available() && Serial.read()); // empty buffer
while (!Serial.available()); // wait for data
while (Serial.available() && Serial.read()); // empty buffer again
// load and configure the DMP
Serial.println(F("Initializing DMP..."));
#endif
devStatus = mpu.dmpInitialize();
// make sure it worked (returns 0 if so)
if (devStatus == 0)
{
// Calibration Time: generate offsets and calibrate our MPU6050
mpu.CalibrateAccel(6);
mpu.CalibrateGyro(6);
#ifdef PRINT_DEBUG
mpu.PrintActiveOffsets();
// turn on the DMP, now that it's ready
Serial.println(F("Enabling DMP..."));
#endif
mpu.setDMPEnabled(true);
// set our DMP Ready flag so the main loop() function knows it's okay to use it
#ifdef PRINT_DEBUG
Serial.println(F("DMP ready! Waiting for first interrupt..."));
#endif
dmpReady = true;
// get expected DMP packet size for later comparison
packetSize = mpu.dmpGetFIFOPacketSize();
}
else
{
// ERROR!
// 1 = initial memory load failed
// 2 = DMP configuration updates failed
// (if it's going to break, usually the code will be 1)
#ifdef PRINT_DEBUG
Serial.print(F("DMP Initialization failed (code "));
Serial.print(devStatus);
Serial.println(F(")"));
#endif
}
}
void setup()
{
//This is to set up radio transmitter for rf
setupRadioTransmitter();
//This is to set up MPU6050 sensor
setupMPU();
}
void loop()
{
// if programming failed, don't try to do anything
if (!dmpReady) return;
// read a packet from FIFO. Get the Latest packet
if (mpu.dmpGetCurrentFIFOPacket(fifoBuffer))
{
// display Euler angles in degrees
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.println(xAxisValue);
Serial.println(yAxisValue);
#endif
}
}
code receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
//#define PRINT_DEBUG //Uncomment this line if you want to print the values on serial monitor
#define SIGNAL_TIMEOUT 500 // This is signal timeout in milli seconds.
#define MAX_MOTOR_SPEED 200
const uint64_t pipeIn = 0xF9E8F0F0E1LL;
RF24 radio(8, 9);
unsigned long lastRecvTime = 0;
struct PacketData
{
byte xAxisValue;
byte yAxisValue;
} receiverData;
//Right motor
int enableRightMotor=5;
int rightMotorPin1=2;
int rightMotorPin2=3;
//Left motor
int enableLeftMotor=6;
int leftMotorPin1=4;
int leftMotorPin2=7;
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));
//We will receive value as 0 to 254. Center value is 127
if (receiverData.yAxisValue >= 175) //Move car Forward
{
rotateMotor(MAX_MOTOR_SPEED, MAX_MOTOR_SPEED);
}
else if (receiverData.yAxisValue <= 75) //Move car Backward
{
rotateMotor(-MAX_MOTOR_SPEED, -MAX_MOTOR_SPEED);
}
else if (receiverData.xAxisValue >= 175) //Move car Right
{
rotateMotor(-MAX_MOTOR_SPEED, MAX_MOTOR_SPEED);
}
else if (receiverData.xAxisValue <= 75) //Move car Left
{
rotateMotor(MAX_MOTOR_SPEED, -MAX_MOTOR_SPEED);
}
else //Stop the car
{
rotateMotor(0, 0);
}
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)
{
if (rightMotorSpeed < 0)
{
digitalWrite(rightMotorPin1,LOW);
digitalWrite(rightMotorPin2,HIGH);
}
else if (rightMotorSpeed > 0)
{
digitalWrite(rightMotorPin1,HIGH);
digitalWrite(rightMotorPin2,LOW);
}
else
{
digitalWrite(rightMotorPin1,LOW);
digitalWrite(rightMotorPin2,LOW);
}
if (leftMotorSpeed < 0)
{
digitalWrite(leftMotorPin1,LOW);
digitalWrite(leftMotorPin2,HIGH);
}
else if (leftMotorSpeed > 0)
{
digitalWrite(leftMotorPin1,HIGH);
digitalWrite(leftMotorPin2,LOW);
}
else
{
digitalWrite(leftMotorPin1,LOW);
digitalWrite(leftMotorPin2,LOW);
}
analogWrite(enableRightMotor, abs(rightMotorSpeed));
analogWrite(enableLeftMotor, abs(leftMotorSpeed));
}
Please edit your post with the code, select all code and click the <CODE/>
button; next save your post. The will apply code tags which makes it easier to read, easier to copy and the forum software will display it correctly.
"still not work" does not mean anything.
Start by breaking it down. Start with e.g. the motor part and use serial monitor to control it. Once you have succeeded with that, leave it as is and start with e.g. the communication and serial monitor. Once both work you can merge them together.
i understand you but i do before instructions that you tell me and not work , sorry for my bad grammar
Please fix this
It's missing the actual includes, probably because you did not use code tags the first time.
Please explain what you expect it to do and what it actually does. Is it not communicating with the receiver (receiver code seems to be missing)? Or does it not compile? Or can't you upload? Or ...
Assuming that you can compile and upload, please change
//#define PRINT_DEBUG //Uncomment this line if you want to print the MPU6050 initialization information on serial monitor
to
#define PRINT_DEBUG //Uncomment this line if you want to print the MPU6050 initialization information on serial monitor
That way you can see what is happening in the transmitter.
@ahmedhegazy153, your two identical topics have been merged. Do not cross-post as it wastes peoples time.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.