Advertisement
KarolKita

Untitled

Mar 17th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * CE D7
  3.  * CSN D8
  4.  * SCK D13
  5.  * MO D11
  6.  * MI D12
  7. */
  8.  
  9. #include <PWM.h>
  10. #include <SPI.h>
  11. #include <nRF24L01.h>
  12. #include <RF24.h>
  13. #include <Servo.h>
  14.  
  15. #define CE_PIN   7
  16. #define CSN_PIN 8
  17.  
  18. #define go 9
  19. #define L 6
  20. #define P 4
  21.  
  22. //int32_t frequency = 2500;
  23.  
  24. // NOTE: the "LL" at the end of the constant is "LongLong" type
  25. const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
  26.  
  27. //---( Declare objects )---/
  28. RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
  29.  
  30. Servo Turn;
  31.  
  32. //---( Declare Variables )---/
  33. int joystick[3];
  34. int forward = 0;
  35. int reverse = 0;
  36. int sterering = 90;
  37. int fail = 0;
  38.  
  39. void setup()
  40. {
  41.   delay(1000);
  42. //  InitTimersSafe();
  43. //  bool success = SetPinFrequencySafe(9, frequency);
  44.   Serial.println("Nrf24L01 Receiver Starting");
  45.   radio.begin();
  46.   radio.setDataRate(RF24_250KBPS);
  47.   radio.openReadingPipe(1, pipe);
  48.   radio.startListening();
  49.   Turn.attach(3);
  50.   pinMode(P, OUTPUT);
  51.   digitalWrite(P, 0);
  52.   pinMode(L, OUTPUT);
  53.   digitalWrite(L, 0);
  54. }
  55.  
  56. void loop(){
  57.   if (radio.available()){
  58.     {
  59.       radio.read(joystick, sizeof(joystick));
  60.       sterering = map(joystick[1], 286, 757, 35, 145);
  61.       Turn.write(sterering);
  62.       if(joystick[2] == 0){
  63.       if(460 < joystick[0] < 550){
  64.         forward = 0;
  65.       }
  66.       if(550 < joystick[0]){
  67.         forward = map(joystick[0], 550, 800, 0, 255);
  68.         digitalWrite(L, LOW);
  69.         digitalWrite(P, HIGH);
  70.       }
  71.       else if(450 > joystick[0]){
  72.         forward = map(joystick[0], 460, 200, 0, 255);
  73.         digitalWrite(P, LOW);
  74.         digitalWrite(L, HIGH);
  75.       }
  76.       analogWrite(go, forward);
  77.       fail = 0;
  78.     }
  79.     if(joystick[2] == 1){
  80.       digitalWrite(P, LOW);
  81.       digitalWrite(L, LOW);
  82.       analogWrite(go, 0);
  83.     }
  84.    }
  85.   }
  86. /*  else
  87.   {
  88.     fail++;
  89.     if (fail > 200){
  90.     analogWrite(go, 0);
  91.     analogWrite(L, 0);
  92.     sterering = 90;
  93.     }
  94.    ]
  95. */
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement