|
| 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