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