Arduino Remote Controlled Car Code
// 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: