Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Control RC Car with Arduino and RF module Nrf2401
- * Nrf2401 Pin Configuration
- * 1 - GND
- * 2 - !!! VCC 3.3V !!!
- * 3 - CE to Arduino pin 9
- * 4 - CS(N) to Arduino pin 10
- * 5 - SCK to Arduino pin 13
- * 6 - MOSI to Arduino pin 11
- * 7 - MISO to Arduino pin 12
- * Transmitter Code and Receiver Code
- */
- /* Transmiter Code */
- #include <SPI.h>
- #include "nRF24L01.h"
- #include "RF24.h"
- #define CE_PIN 9
- #define CSN_PIN 10
- #define direction_pin 0 // A0 read the direction
- #define accelerator_pin 1 //A1 read forward backward
- int val_direction,val_accelerator,data_transmit[2]; // variable for direction and accelerator
- RF24 radio(CE_PIN,CSN_PIN); // Create a Radio
- const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
- void setup(void)
- {
- //Serial.begin(57600);
- delay(300); // Delay 0.3s before next step
- radio.begin();
- radio.openWritingPipe(pipe);
- radio.stopListening (); //switch the modem to data transmission mode.
- }
- void loop(void)
- {
- val_direction = analogRead(direction_pin);
- val_accelerator = analogRead(accelerator_pin);
- val_direction = ((map(val_direction,343,588, 20, 145))/10)*10; //scale the value
- val_accelerator = ((map(val_accelerator, 380, 670, 39, 166))/10)*10; // scale the value
- /* View the data in serial monitor */
- /*
- Serial.print("direction= ");
- Serial.print(val_direction);
- Serial.print("\t");
- Serial.print("acceleration= ");
- Serial.println(val_accelerator);
- */
- data_transmit[0] = val_direction;
- data_transmit[1] = val_accelerator;
- radio.write(data_transmit,sizeof(data_transmit)); // Radio sending's command
- }
- ______________________________________________________________________________________________________________________________________
- /* Receiver Code */
- #include <SoftwareServo.h>
- SoftwareServo servo_direction; // create servo to control the direction
- SoftwareServo servo_accelerator; // create servo to control the speed
- #include <SPI.h>
- #include "nRF24L01.h"
- #include "RF24.h"
- #define CE_PIN 9
- #define CSN_PIN 10
- #define middle_direction 80 //define direction in middle
- int data_receive[2]; //data receive from transmitter. "direction" and "acclerator"
- RF24 radio(CE_PIN,CSN_PIN); // Create a Radio
- const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
- void setup(void)
- {
- //delay(3000); //wait until the esc starts in case of Arduino got power first
- //Serial.begin(57600);
- servo_direction.attach(8); //pin 8 servo data
- servo_accelerator.attach(7); //pin 7 for ESC speed
- SoftwareServo::refresh();
- servo_direction.write(middle_direction); //servo to the midle
- delay(15); //stabiliser le servo
- radio.begin();
- radio.openReadingPipe(1,pipe);
- radio.startListening(); //switch the modem to data receiving mode.
- }
- void loop(void)
- {
- if (radio.available())
- {
- int ESC_Speed,servo_data;
- bool done = false;
- while (!done)
- {
- done = radio.read(data_receive, sizeof(data_receive));
- SoftwareServo::refresh();
- servo_data=constrain(data_receive[0],30,140);
- servo_direction.write(servo_data);
- ESC_Speed=constrain(data_receive[1],40,150);
- servo_accelerator.write(ESC_Speed);
- /* View the value in serial monitor */
- /*
- Serial.print("direction= ");
- Serial.print(servo_data);
- Serial.print("\t");
- Serial.print("acceleration= ");
- Serial.println(ESC_Speed);
- */
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment