noam76

Arduino RC Car with RF module Nrf2401

Jan 21st, 2016
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Control RC Car with Arduino and RF module Nrf2401
  3.  * Nrf2401 Pin Configuration
  4.  * 1 - GND
  5.  * 2 - !!! VCC 3.3V !!!
  6.  * 3 - CE to Arduino pin 9
  7.  * 4 - CS(N) to Arduino pin 10
  8.  * 5 - SCK to Arduino pin 13
  9.  * 6 - MOSI to Arduino pin 11
  10.  * 7 - MISO to Arduino pin 12
  11.  * Transmitter Code and Receiver Code
  12. */
  13.  
  14. /* Transmiter Code */
  15.  
  16. #include  <SPI.h>
  17. #include "nRF24L01.h"
  18. #include "RF24.h"
  19.  
  20. #define CE_PIN   9
  21. #define CSN_PIN 10
  22. #define direction_pin 0 // A0 read the direction
  23. #define accelerator_pin 1 //A1 read forward backward
  24.  
  25. int val_direction,val_accelerator,data_transmit[2]; // variable for direction and accelerator
  26.  
  27. RF24 radio(CE_PIN,CSN_PIN); // Create a Radio
  28. const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
  29.  
  30. void setup(void)
  31. {
  32.  //Serial.begin(57600);
  33.  delay(300); // Delay 0.3s before next step
  34.  radio.begin();
  35.  radio.openWritingPipe(pipe);
  36.  radio.stopListening (); //switch the modem to data transmission mode.
  37. }
  38.  
  39. void loop(void)
  40. {
  41.   val_direction = analogRead(direction_pin);
  42.   val_accelerator = analogRead(accelerator_pin);
  43.   val_direction = ((map(val_direction,343,588, 20, 145))/10)*10; //scale the value
  44.   val_accelerator = ((map(val_accelerator, 380, 670, 39, 166))/10)*10; // scale the value
  45.  
  46.   /* View the data in serial monitor */
  47.   /*
  48.   Serial.print("direction= ");
  49.   Serial.print(val_direction);
  50.   Serial.print("\t");
  51.   Serial.print("acceleration= ");
  52.   Serial.println(val_accelerator);
  53.  */
  54.   data_transmit[0] = val_direction;
  55.   data_transmit[1] = val_accelerator;
  56.   radio.write(data_transmit,sizeof(data_transmit));  // Radio sending's command
  57. }
  58.  
  59. ______________________________________________________________________________________________________________________________________
  60.  
  61. /* Receiver Code */
  62. #include <SoftwareServo.h>
  63. SoftwareServo servo_direction; // create servo to control the direction
  64. SoftwareServo servo_accelerator; // create servo to control the speed
  65.  
  66. #include <SPI.h>
  67. #include "nRF24L01.h"
  68. #include "RF24.h"
  69.  
  70. #define CE_PIN   9
  71. #define CSN_PIN 10
  72.  
  73. #define middle_direction 80 //define direction in middle
  74. int data_receive[2]; //data receive from transmitter. "direction" and "acclerator"
  75.  
  76. RF24 radio(CE_PIN,CSN_PIN); // Create a Radio
  77. const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
  78.  
  79. void setup(void)
  80. {
  81.  //delay(3000); //wait until the esc starts in case of Arduino got power first
  82.  //Serial.begin(57600);
  83.  servo_direction.attach(8); //pin 8 servo data
  84.  servo_accelerator.attach(7); //pin 7 for ESC speed
  85.  SoftwareServo::refresh();
  86.  servo_direction.write(middle_direction); //servo to the midle
  87.  delay(15); //stabiliser le servo
  88.  radio.begin();
  89.  radio.openReadingPipe(1,pipe);
  90.  radio.startListening(); //switch the modem to data receiving mode.
  91. }
  92.  
  93. void loop(void)
  94. {
  95.  if (radio.available())
  96.  {
  97.    int ESC_Speed,servo_data;
  98.    bool done = false;
  99.    while (!done)
  100.   {
  101.    done = radio.read(data_receive, sizeof(data_receive));
  102.    SoftwareServo::refresh();
  103.    servo_data=constrain(data_receive[0],30,140);
  104.    servo_direction.write(servo_data);
  105.    ESC_Speed=constrain(data_receive[1],40,150);
  106.    servo_accelerator.write(ESC_Speed);
  107.    
  108.    /* View the value in serial monitor */
  109.    /*
  110.    Serial.print("direction= ");
  111.    Serial.print(servo_data);
  112.    Serial.print("\t");
  113.    Serial.print("acceleration= ");
  114.    Serial.println(ESC_Speed);
  115.    */
  116.   }
  117.  }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment