Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.79 KB | None | 0 0
  1. /* Receiver code for the Arduino Radio control with PWM output
  2.  * Install the NRF24 library to your IDE
  3.  * Upload this code to the Arduino UNO, NANO, Pro mini (5V,16MHz)
  4.  * Connect a NRF24 module to it:
  5.  
  6.     Module // Arduino UNO,NANO
  7.    
  8.     GND    ->   GND
  9.     Vcc    ->   3.3V
  10.     CE     ->   D9
  11.     CSN    ->   D10
  12.     CLK    ->   D13
  13.     MOSI   ->   D11
  14.     MISO   ->   D12
  15.  
  16. This code receive 7 channels and create a PWM output for each one on D2, D3, D4, D5, D6, D7and D8
  17. Please, like share and subscribe : https://www.youtube.com/c/ELECTRONOOBS
  18. */
  19.  
  20.  
  21. #include <SPI.h>
  22. #include <nRF24L01.h>
  23. #include <RF24.h>
  24. #include <Servo.h>  //To create PWM signals we need this lybrary
  25.  
  26. const uint64_t pipeIn = 0xE8E8F0F0E1LL;     //Remember that this code is the same as in the transmitter
  27. RF24 radio(9, 10);  //CSN and CE pins
  28.  
  29. // The sizeof this struct should not exceed 32 bytes
  30. struct Received_data {
  31.   byte ch1;
  32.   byte ch2;
  33.   byte ch3;
  34.   byte ch4;
  35.   byte ch5;
  36.   byte ch6;
  37.   byte ch7;
  38. };
  39.  
  40. Received_data received_data;
  41.  
  42. Servo channel_1;
  43. Servo channel_2;
  44. Servo channel_3;
  45. Servo channel_4;
  46. Servo channel_5;
  47. Servo channel_6;
  48. Servo channel_7;
  49.  
  50. int ch1_value = 0;
  51. int ch2_value = 0;
  52. int ch3_value = 0;
  53. int ch4_value = 0;
  54. int ch5_value = 0;
  55. int ch6_value = 0;
  56. int ch7_value = 0;
  57.  
  58.  
  59. void reset_the_Data()
  60. {
  61.   // 'safe' values to use when NO radio input is detected
  62.   received_data.ch1 = 0;      //Throttle (channel 1) to 0
  63.   received_data.ch2 = 127;
  64.   received_data.ch3 = 127;
  65.   received_data.ch4 = 127;
  66.   received_data.ch5 = 0;
  67.   received_data.ch6 = 0;
  68.   received_data.ch7 = 0;
  69. }
  70.  
  71.  
  72.  
  73. /**************************************************/
  74.  
  75. void setup()
  76. {
  77.   //Attach the servo signal on pins from D2 to D8
  78.   channel_1.attach(2);
  79.   channel_2.attach(3);
  80.   channel_3.attach(4);
  81.   channel_4.attach(5);
  82.   channel_5.attach(6);
  83.   channel_6.attach(7);
  84.   channel_7.attach(8);
  85.  
  86.   //We reset the received values
  87.   reset_the_Data();
  88.  
  89.   //Once again, begin and radio configuration
  90.   radio.begin();
  91.   radio.setAutoAck(false);
  92.   radio.setDataRate(RF24_250KBPS);  
  93.   radio.openReadingPipe(1,pipeIn);
  94.  
  95.   //We start the radio comunication
  96.   radio.startListening();
  97.  
  98. }
  99.  
  100. /**************************************************/
  101.  
  102. unsigned long lastRecvTime = 0;
  103.  
  104. //We create the function that will read the data each certain time
  105. void receive_the_data()
  106. {
  107.   while ( radio.available() ) {
  108.   radio.read(&received_data, sizeof(Received_data));
  109.   lastRecvTime = millis(); //Here we receive the data
  110. }
  111. }
  112.  
  113. /**************************************************/
  114.  
  115. void loop()
  116. {
  117.   //Receive the radio data
  118.   receive_the_data();
  119.  
  120. //////////This small if will reset the data if signal is lost for 1 sec.
  121. /////////////////////////////////////////////////////////////////////////
  122.   unsigned long now = millis();
  123.   if ( now - lastRecvTime > 1000 ) {
  124.     // signal lost?
  125.     reset_the_Data();
  126.     //Go up and change the initial values if you want depending on
  127.     //your aplications. Put 0 for throttle in case of drones so it won't
  128.     //fly away
  129.   }
  130.  
  131.  
  132.   ch1_value = map(received_data.ch1,0,255,1000,2000);
  133.   ch2_value = map(received_data.ch2,0,255,1000,2000);
  134.   ch3_value = map(received_data.ch3,0,255,1000,2000);
  135.   ch4_value = map(received_data.ch4,0,255,1000,2000);
  136.   ch5_value = map(received_data.ch5,0,1,1000,2000);
  137.   ch6_value = map(received_data.ch6,0,1,1000,2000);
  138.   ch7_value = map(received_data.ch7,0,255,1000,2000);
  139.  
  140.   //Creathe the PWM signals
  141.   channel_1.writeMicroseconds(ch1_value);  
  142.   channel_2.writeMicroseconds(ch2_value);  
  143.   channel_3.writeMicroseconds(ch3_value);  
  144.   channel_4.writeMicroseconds(ch4_value);  
  145.   channel_5.writeMicroseconds(ch5_value);  
  146.   channel_6.writeMicroseconds(ch6_value);  
  147.   channel_7.writeMicroseconds(ch7_value);  
  148.  
  149.  
  150. }//Loop end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement