Skip to content

Commit 29336e2

Browse files
authored
Add files via upload
1 parent 9976549 commit 29336e2

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed

nrf24L01_Gestur_Transmitte_code.ino

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
#include <SPI.h> //SPI library for communicate with the nRF24L01+
3+
#include "RF24.h" //The main library of the nRF24L01+
4+
#include "Wire.h" //For communicate
5+
#include "I2Cdev.h" //For communicate with MPU6050
6+
#include "MPU6050.h" //The main library of the MPU6050
7+
8+
9+
//Define the object to access and cotrol the Gyro and Accelerometer (We don't use the Gyro data)
10+
MPU6050 mpu;
11+
int16_t ax, ay, az;
12+
int16_t gx, gy, gz;
13+
14+
//Define packet for the direction (X axis and Y axis)
15+
int data[2];
16+
17+
//Define object from RF24 library - 8 and 9 are a digital pin numbers to which signals CE and CSN are connected.
18+
RF24 radio(8,9);
19+
20+
//Create a pipe addresses for the communicate
21+
const uint64_t pipe = 0xE8E8F0F0E1LL;
22+
23+
void setup(void){
24+
Serial.begin(9600);
25+
Wire.begin();
26+
mpu.initialize(); //Initialize the MPU object
27+
radio.begin(); //Start the nRF24 communicate
28+
radio.openWritingPipe(pipe); //Sets the address of the receiver to which the program will send data.
29+
}
30+
31+
void loop(void){
32+
33+
//With this function, the acceleration and gyro values of the axes are taken.
34+
//If you want to control the car axis differently, you can change the axis name in the map command.
35+
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
36+
37+
//In two-way control, the X axis (data [0]) of the MPU6050 allows the robot to move forward and backward.
38+
//Y axis (data [1]) allows the robot to right and left turn.
39+
data[0] = map(ax, -17000, 17000, 300, 400 ); //Send X axis data
40+
data[1] = map(ay, -17000, 17000, 100, 200); //Send Y axis data
41+
radio.write(data, sizeof(data));
42+
Serial.print("X axix data = ");
43+
Serial.println(data[0]);
44+
Serial.print("Y axix data = ");
45+
Serial.println(data[1]);
46+
}

nrf24L01_Gesture_Receiver_code.ino

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
2+
#include <SPI.h> //SPI library for communicate with the nRF24L01+
3+
#include "RF24.h" //The main library of the nRF24L01+
4+
5+
//Define enable pins of the Motors
6+
const int enbA = 3;
7+
const int enbB = 6;
8+
9+
//Define control pins of the Motors
10+
//If the motors rotate in the opposite direction, you can change the positions of the following pin numbers
11+
const int IN1 = 2; //Right Motor (-)
12+
const int IN2 = 4; //Right Motor (+)
13+
const int IN3 = 5; //Left Motor (+)
14+
const int IN4 = 7; //Right Motor (-)
15+
16+
//Define variable for the motors speeds
17+
//I have defined a variable for each of the two motors
18+
//This way you can synchronize the rotation speed difference between the two motors
19+
int RightSpd = 200;
20+
int LeftSpd = 250;
21+
22+
//Define packet for the direction (X axis and Y axis)
23+
int data[2];
24+
25+
//Define object from RF24 library - 8 and 9 are a digital pin numbers to which signals CE and CSN are connected
26+
RF24 radio(8,9);
27+
28+
//Create a pipe addresses for the communicate
29+
const uint64_t pipe = 0xE8E8F0F0E1LL;
30+
31+
void setup(){
32+
//Define the motor pins as OUTPUT
33+
pinMode(enbA, OUTPUT);
34+
pinMode(enbB, OUTPUT);
35+
pinMode(IN1, OUTPUT);
36+
pinMode(IN2, OUTPUT);
37+
pinMode(IN3, OUTPUT);
38+
pinMode(IN4, OUTPUT);
39+
40+
//digitalWrite(enbA, HIGH);
41+
//digitalWrite(enbB, HIGH);
42+
43+
Serial.begin(9600);
44+
radio.begin(); //Start the nRF24 communicate
45+
radio.openReadingPipe(1, pipe); //Sets the address of the transmitter to which the program will receive data.
46+
radio.startListening();
47+
}
48+
49+
void loop(){
50+
if (radio.available()){
51+
radio.read(data, sizeof(data));
52+
53+
if(data[0] < 340){
54+
//forward
55+
analogWrite(enbA, RightSpd);
56+
analogWrite(enbB, LeftSpd);
57+
digitalWrite(IN1, HIGH);
58+
digitalWrite(IN2, LOW);
59+
digitalWrite(IN3, HIGH);
60+
digitalWrite(IN4, LOW);
61+
Serial.println("forward");
62+
}
63+
64+
if(data[0] > 360){
65+
//backward
66+
analogWrite(enbA, RightSpd);
67+
analogWrite(enbB, LeftSpd);
68+
digitalWrite(IN1, LOW);
69+
digitalWrite(IN2, HIGH);
70+
digitalWrite(IN3, LOW);
71+
digitalWrite(IN4, HIGH);
72+
Serial.println("backward");
73+
}
74+
75+
if(data[1] > 160){
76+
//right
77+
analogWrite(enbA, RightSpd);
78+
analogWrite(enbB, LeftSpd);
79+
digitalWrite(IN1, LOW);
80+
digitalWrite(IN2, HIGH);
81+
digitalWrite(IN3, HIGH);
82+
digitalWrite(IN4, LOW);
83+
Serial.println("right");
84+
}
85+
86+
if(data[1] < 140){
87+
//left
88+
analogWrite(enbA, RightSpd);
89+
analogWrite(enbB, LeftSpd);
90+
digitalWrite(IN1, HIGH);
91+
digitalWrite(IN2, LOW);
92+
digitalWrite(IN3, LOW);
93+
digitalWrite(IN4, HIGH);
94+
Serial.println("left");
95+
}
96+
97+
if(data[0] > 340 && data[0] < 360 && data[1] > 140 && data[1] < 160){
98+
//stop car
99+
analogWrite(enbA, 0);
100+
analogWrite(enbB, 0);
101+
digitalWrite(IN1, LOW);
102+
digitalWrite(IN2, LOW);
103+
digitalWrite(IN3, LOW);
104+
digitalWrite(IN4, LOW);
105+
Serial.println("stop");
106+
}
107+
}
108+
}

0 commit comments

Comments
 (0)