Advertisement
Pilsonv

rc_rx

Sep 18th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.83 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <CyberLib.h>
  3. #include <SPI.h>
  4. #include "nRF24L01.h"
  5. #include "RF24.h"
  6. #include <ServoTimer2.h>
  7.  
  8. #define rfch 0x00
  9. #define tmax 1000
  10. #define dpin 9
  11. #define sdebug 1
  12.  
  13. uint8_t m1 = 0;
  14. uint8_t m2 = 0;
  15. uint8_t m3 = 0;
  16. uint8_t m4 = 0;
  17. uint8_t scount = 4;
  18. uint32_t tm1 = 0;
  19. uint32_t tm2 = 0;
  20. uint32_t tm3 = 0;
  21. uint32_t b5 = 0;
  22. bool lr = 0;
  23. bool dual = 0;
  24. bool strobe = 0;
  25. bool light = 0;
  26. bool nodata;
  27.  
  28. RF24 radio(7, 8);
  29. uint8_t address[][6] = {"z1", "z2", "z3", "z4", "z5", "z6"};
  30.  
  31. ServoTimer2 servo;
  32.  
  33. void setup() {
  34.     //For ports 9,10 set PWM freq to 62500 Hz
  35.     TCCR1A = TCCR1A & 0xe0 | 1;
  36.     TCCR1B = TCCR1B & 0xe0 | 0x09;
  37.  
  38.     D2_Out;
  39.     D4_Out;
  40.     D5_Out;
  41.     D6_Out;
  42.     D9_Out;
  43.     D10_Out;
  44.     D2_Low;
  45.     D4_Low;
  46.     D5_Low;
  47.     D6_Low;
  48.     D9_Low;
  49.     D10_Low;
  50.     if (sdebug) {
  51.         Serial.begin(115200);
  52.     }
  53.     radio.begin();
  54.     radio.setAutoAck(0);
  55.     radio.setRetries(0,15);
  56.     radio.enableAckPayload();
  57.     radio.setPayloadSize(5);
  58.     radio.openReadingPipe(1,address[0]);
  59.     radio.setChannel(rfch);
  60.     radio.setPALevel (RF24_PA_MAX); //RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH, RF24_PA_MAX
  61.     radio.setDataRate (RF24_250KBPS); //RF24_2MBPS, RF24_1MBPS, RF24_250KBPS
  62.     radio.powerUp();
  63.     radio.startListening();
  64.     servo.attach(3);
  65. }
  66.  
  67. void loop() {
  68.     uint32_t last_time;
  69.     uint8_t data[5] = {0};
  70.     uint8_t pipeNo;
  71.     int8_t dspeed = 0;
  72.     uint16_t srvdeg = 90;
  73.     uint8_t count = 0;
  74.     while(true) {
  75.         if (radio.available(&pipeNo)) {
  76.             last_time = millis();
  77.             nodata = 0;
  78.             radio.read(&data, sizeof(data));
  79.             srvdeg = map(data[2], 0, 255, 850, 2150);
  80.             if (srvdeg > 1475 && srvdeg < 1516) {
  81.                 srvdeg = 1480;
  82.             }
  83.             if (data[1] > 127) {
  84.                 D4_Low;
  85.                 D5_High;
  86.                 if (data[1] < 134) {
  87.                     dspeed = 0;
  88.                 } else {
  89.                     data[1] = map(data[1], 134, 255, 180, 255);
  90.                     dspeed = map(data[1], 128, 255, 0, 255);
  91.                 }
  92.             } else {
  93.                 D4_High;
  94.                 D5_Low;
  95.                 if (data[1] > 119) {
  96.                     dspeed = 0;
  97.                 } else {
  98.                     data[1] = map(data[1], 120, 0, 80, 0);
  99.                     dspeed = map(data[1], 128, 0, 0, 255);
  100.                 }
  101.             }
  102.             // Debug to serial
  103.             if (sdebug == 1) {
  104.                 count++;
  105.                 Serial.print(count);
  106.                 Serial.print("\t");
  107.                 Serial.print(data[0]);
  108.                 Serial.print("\t");
  109.                 Serial.print(data[1]);
  110.                 Serial.print("\t");
  111.                 Serial.print(data[2]);
  112.                 Serial.print("\t");
  113.                 Serial.print(data[3]);
  114.                 Serial.print("\t");
  115.                 Serial.print(data[4]);
  116.                 Serial.print("\t");
  117.                 Serial.print("dspeed ");
  118.                 Serial.print(dspeed);
  119.                 Serial.print("\t");
  120.                 Serial.print("srvdeg ");
  121.                 Serial.println(srvdeg);
  122.             }
  123.         }
  124.         if (millis() - last_time > tmax) {
  125.             srvdeg = 1480;
  126.             dspeed = 0;
  127.             nodata = 1;
  128.             if (D10_Read == 1) {
  129.                 D2_High;
  130.                 D6_High;
  131.                 D10_Low;
  132.             } else {
  133.                 D2_Low;
  134.                 D6_Low;
  135.                 D10_High;
  136.             }
  137.             last_time = millis();
  138.             if (sdebug == 1) {
  139.                 Serial.println("No data");
  140.             }
  141.         }
  142.         analogWrite(dpin, dspeed);
  143.         servo.write(srvdeg);
  144.         if (bitRead(data[4], 5) == 1) {
  145.             if (millis() - b5 > 500) {
  146.                 if (strobe == 1) {
  147.                     strobe = 0;
  148.                 } else if (light == 1) {
  149.                     strobe = 1;
  150.                     light = 0;
  151.                 } else {
  152.                     light = 1;
  153.                 }
  154.                 b5 = millis();
  155.             }
  156.         }
  157.         stroboscope();
  158.         lights();
  159.     }
  160. }
  161.  
  162. void lights() {
  163.     if (strobe == 1 || nodata == 1) {
  164.         return 0;
  165.     }
  166.     if (light == 1) {
  167.         D2_High;
  168.         D6_High;
  169.         D10_High;
  170.     } else {
  171.         D2_Low;
  172.         D6_Low;
  173.         D10_Low;
  174.     }
  175. }
  176.  
  177. void stroboscope() {
  178.     if (strobe == 0 || nodata == 1) {
  179.         m1 = 0;
  180.         m2 = 0;
  181.         m3 = 0;
  182.         m4 = 0;
  183.         return 0;
  184.     }
  185.     if (m1 < scount) {
  186.         if (m2 < 12) {
  187.             if (millis() > tm1 + 30) {
  188.                 if (! lr) {
  189.                     D2_Inv;
  190.                     D6_Low;
  191.                     D10_Low;
  192.                 } else {
  193.                     D2_Low;
  194.                     D6_Inv;
  195.                     D10_Inv;
  196.                 }
  197.                 tm1 = millis();
  198.                 m2++;
  199.             }
  200.         } else {
  201.             if (lr) {
  202.                 m1++;
  203.             }
  204.             lr = !lr;
  205.             m2 = 0;
  206.         }
  207.         tm2 = millis();
  208.     } else {
  209.         if (m3 < scount) {
  210.             if (m4 < 12) {
  211.                 if (millis() > tm3 + 30) {
  212.                     if (! dual) {
  213.                         D2_Inv;
  214.                         D6_Inv;
  215.                         D10_Low;
  216.                     } else {
  217.                         D2_Low;
  218.                         D6_Low;
  219.                         D10_Inv;
  220.                     }
  221.                     tm3 = millis();
  222.                     m4++;
  223.                 }
  224.             } else {
  225.                 if (dual) {
  226.                     m3++;
  227.                 }
  228.                 dual = !dual;
  229.                 m4 = 0;
  230.             }
  231.         } else {
  232.             m1 = 0;
  233.             m3 = 0;
  234.         }
  235.     }
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement