Arduino Remote Controlled Car Code
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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
// Receiver PART /* By Roee Bloch nRF24L01 Receive Joystick values --MY CAR V 1_3 TOUCH buttons A0 and A1. A0 = Forward A1 = Backword A0 && A1 = TURN Right Connection - each of A0,A1 - connect with 100K Resistor to GND Add 5V VCC - so if you between A0 to VCC with Finger command will work Same between A1 to VCC - just touch if you touch all together A0,A1 both to VCC with Finger it will turn - CONNECTIONS: nRF24L01 Modules See: http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo 1 - GND 2 - VCC 3.3V !!! NOT 5V 3 - CE to Arduino pin 9 4 - CSN to Arduino pin 10 5 - SCK to Arduino pin 13 6 - MOSI to Arduino pin 11 7 - MISO to Arduino pin 12 8 - UNUSED - V1.00 11/26/13 Based on examples at http://www.bajdi.com/ Questions: terry@yourduino.com */ // Digital read from 4 keys (up to 16 values) #define KEY1 0 #define KEY2 0 #define KEY3 0 #define KEY4 0 /*-----( Import needed libraries )-----*/ #include <SPI.h> #include <nRF24L01.h> #include <RF24.h> /*-----( Declare Constants and Pin Numbers )-----*/ #define CE_PIN 9 #define CSN_PIN 10 // NOTE: the "LL" at the end of the constant is "LongLong" type const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe /*-----( Declare objects )-----*/ RF24 radio(CE_PIN, CSN_PIN); // Create a Radio /*-----( Declare Variables )-----*/ // array of my reading values, since NRF2401 in this mode return an array int mykeys[] = {KEY1, KEY2, KEY3, KEY4}; // keys void setup() /****** SETUP: RUNS ONCE ******/ { Serial.begin(57600); delay(1000); pinMode(3, OUTPUT); // just for test during debug mode pinMode(4, OUTPUT); // control wheels1 H-bridge 1 pinMode(5, OUTPUT); // control wheels1 H-bridge 1 pinMode(6, OUTPUT); // control wheels2 H-bridge 2 pinMode(7, OUTPUT); // control wheels2 H-bridge 2 Serial.println("Nrf24L01 Receiver Starting"); radio.begin(); radio.openReadingPipe(1, pipe); radio.startListening(); }//--(end setup )--- void loop() /****** LOOP: RUNS CONSTANTLY ******/ { if ( radio.available() ) { // Read the data payload until we've received everything bool done = false; while (!done) { // Fetch the data payload done = radio.read( mykeys, sizeof(mykeys) ); Serial.print("KEY1 = "); if ((mykeys[0] == 1) && (mykeys[1] == 0) ) { digitalWrite(3, HIGH); forward(); Serial.println("FORWARD"); } if ((mykeys[1] == 1) && (mykeys[0] == 0)) { digitalWrite(3, HIGH); backword(); Serial.println("BACKWORD"); } if ((mykeys[1] == 1) && (mykeys[0] == 1)) { digitalWrite(3, HIGH); right(); Serial.println("RIGHT"); } if ((mykeys[0] == 0) && (mykeys[1] == 0) && (mykeys[2] == 0)) mystop(); Serial.println(mykeys[0]); Serial.print(" KEY2= "); Serial.println(mykeys[1]); Serial.print(" KEY3= "); Serial.println(mykeys[2]); Serial.print(" KEY4= "); Serial.println(mykeys[3]); } } else { Serial.println("No radio available"); mykeys[0] = 0; mykeys[1] = 0; mykeys[2] = 0; mykeys[3] = 0; } }//--(end main loop )--- /*-----( Declare User-written Functions )-----*/ //NONE //*********( THE END )*********** void mystop() { digitalWrite(4, LOW); digitalWrite(5, LOW); digitalWrite(6, LOW); digitalWrite(7, LOW); } void forward() { digitalWrite(4, LOW); digitalWrite(5, HIGH); digitalWrite(6, LOW); digitalWrite(7, HIGH); } void backword() { digitalWrite(4, HIGH); digitalWrite(5, LOW); digitalWrite(6, HIGH); digitalWrite(7, LOW); } void right() { digitalWrite(4, HIGH); digitalWrite(5, LOW); digitalWrite(6, LOW); digitalWrite(7, HIGH); } //Transmitter Part /* YourDuinoStarter Example: nRF24L01 Modified by Roee Bloch to CAR using just KEY1 and KEY2 TOUCH buttons A0 and A1. A0 = Forward A1 = Backword A0 && A1 = TURN Right Connection - each of A0,A1 - connect with 100K Resistor to GND Add 5V VCC - so if you between A0 to VCC with Finger command will work Same between A1 to VCC - just touch if you touch all together A0,A1 both to VCC with Finger it will turn Transmit Joystick values - WHAT IT DOES: Reads Analog values on A0, A1 and transmits them over a nRF24L01 Radio Link to another transceiver. - SEE the comments after "//" on each line below - CONNECTIONS: nRF24L01 Modules See: http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo 1 - GND 2 - VCC 3.3V !!! NOT 5V 3 - CE to Arduino pin 9 4 - CSN to Arduino pin 10 5 - SCK to Arduino pin 13 6 - MOSI to Arduino pin 11 7 - MISO to Arduino pin 12 8 - UNUSED - Analog Joystick or two 10K potentiometers: GND to Arduino GND VCC to Arduino +5V X Pot to Arduino A0 Y Pot to Arduino A1 - V1.00 11/26/13 Based on examples at http://www.bajdi.com/ Questions: terry@yourduino.com */ /*-----( Import needed libraries )-----*/ #include <SPI.h> #include <nRF24L01.h> #include <RF24.h> /*-----( Declare Constants and Pin Numbers )-----*/ #define CE_PIN 9 #define CSN_PIN 10 #define KEY1 A0 #define KEY2 A1 #define KEY3 A2 #define KEY4 A3 #define TRESHHOLD 20 // NOTE: the "LL" at the end of the constant is "LongLong" type const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe /*-----( Declare objects )-----*/ RF24 radio(CE_PIN, CSN_PIN); // Create a Radio /*-----( Declare Variables )-----*/ int mykeys[] = {KEY1, KEY2, KEY3, KEY4}; // up to 8 keys void setup() /****** SETUP: RUNS ONCE ******/ { Serial.begin(57600); radio.begin(); pinMode(10, OUTPUT); pinMode(6, OUTPUT); radio.openWritingPipe(pipe); }//--(end setup )--- void loop() /****** LOOP: RUNS CONSTANTLY ******/ { int key1, key2, key3, key4; digitalWrite(6, HIGH); if (analogRead(KEY1) > TRESHHOLD) key1 = 1; else key1 = 0; if (analogRead(KEY2) > TRESHHOLD) key2 = 1; else key2 = 0; if (analogRead(KEY3) > TRESHHOLD) key3 = 1; else key3 = 0; if (analogRead(KEY4) > TRESHHOLD) key4 = 1; else key4 = 0; Serial.print("1: "); Serial.println(key1); Serial.print("2: "); Serial.println(key2); Serial.print("3: "); Serial.println(key3); Serial.print("4: "); Serial.println(key4); mykeys[0] = key1; mykeys[1] = key2; mykeys[2] = key3; mykeys[3] = key4; // tx=key1+2*key2+4*key3+8*key4; radio.write( mykeys, sizeof(mykeys) ); }//--(end main loop )--- /*-----( Declare User-written Functions )-----*/ //NONE //*********( THE END )*********** |
Video based on this code: Arduino Wireless Car Project Video
Arduino Nano Ebay Link Arduino Nano
Arduino Uno Ebay link Arduino Uno
Arduino Mega2560 Ebay link Arduino Mega 2560
Arduino Pro-mini ebay link Arduino Pro Mini
Parts for this Project: