Guest User

Untitled

a guest
Aug 14th, 2020
41
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #include <AFMotor.h>
  3. #include <SPI.h>
  4. #include <nRF24L01.h>
  5. #include <RF24.h>
  6.  
  7. #define enA 5 // Note: Pin 9 in previous video ( pin 10 is used for the SPI communication of the NRF24L01)
  8. #define in1 3
  9. #define in2 4
  10. #define enB 6 // Note: Pin 10 in previous video
  11. #define in3 7
  12. #define in4 8
  13.  
  14. // Create one motor instance
  15. AF_DCMotor motor1(1,MOTOR12_64KHZ); // set up motors.
  16. AF_DCMotor motor2(2, MOTOR12_8KHZ);
  17.  
  18. RF24 radio(7,8); // nRF24L01 (CE, CSN)
  19. const byte address[6] = "00001";
  20. unsigned long lastReceiveTime = 0;
  21. unsigned long currentTime = 0;
  22.  
  23. // Max size of this struct is 32 bytes
  24. struct Data_Package {
  25. byte j1PotX;
  26. byte j1PotY;
  27. byte j2PotX;
  28. byte j2PotY;
  29. byte button1;
  30. byte button2;
  31. byte button3;
  32. byte button4;
  33. };
  34.  
  35. Data_Package data; //Create a variable with the above structure
  36. void setup() {
  37.  
  38. Serial.begin(115200);
  39. Serial.println("receiver");
  40. radio.begin();
  41. radio.openReadingPipe(0, address);
  42. radio.setAutoAck(false);
  43. radio.setPALevel(RF24_PA_LOW);
  44. radio.startListening(); // Set the module as receiver
  45.  
  46. }
  47. void loop() {
  48. if (radio.available()) {
  49. radio.read(&data, sizeof(Data_Package));
  50. printdata();
  51. }
  52.  
  53. int btnState1 = data.button1;
  54. int btnState2 = data.button2;
  55. int forward_back = map(data.j1PotY, 0, 1023, -128, 127);
  56. int left_right = map(data.j2PotX, 0, 1023, -128, 127);
  57. int left_motor = (forward_back + left_right);
  58. int right_motor = (forward_back - left_right);
  59.  
  60. // Scale factor defaults to 1
  61.  
  62. int scale_factor = 1;
  63.  
  64. // Calculate scale factor
  65.  
  66. if (abs(left_motor) > 100 or abs(right_motor) > 100) {
  67.  
  68. // Find highest of the 2 values, since both could be above 100
  69.  
  70. int x = max(abs(left_motor), abs(right_motor));
  71.  
  72. // Calculate scale factor
  73.  
  74. int scale_factor = 100.0 / x;
  75. }
  76. // Use scale factor, and turn values back into integers
  77.  
  78. left_motor = round(left_motor * scale_factor);
  79. right_motor = round(right_motor * scale_factor);
  80. motor1.setSpeed(left_motor);
  81. motor2.setSpeed(right_motor);
  82.  
  83. // Actually move the motors
  84.  
  85. if (forward_back < 0) {
  86. motor1.run(BACKWARD);
  87. motor2.run(BACKWARD);
  88. }
  89. else {
  90. motor1.run(FORWARD);
  91. motor2.run(FORWARD);
  92. }
  93.  
  94.  
  95. /* int CurrentTime = millis();
  96.  
  97. int motorspeed = constrain(motorspeed, 0, 255);
  98. motor1.setSpeed(motorspeed);
  99. motor2.setSpeed(motorspeed);
  100. if (btnState1 == 1) {
  101. int buttonpressed = millis();
  102. motorspeed = round((buttonpressed - CurrentTime) * .1);
  103. motor1.run(FORWARD);
  104. motor2.run(FORWARD);
  105. }
  106.  
  107. if (btnState2 == 1){
  108.  
  109. int button2pressed = millis();
  110. motorspeed = round((button2pressed - CurrentTime) * .1);
  111. motor1.run(BACKWARD);
  112. motor2.run(BACKWARD);
  113. }
  114. else {
  115.  
  116. motorspeed = 0;
  117. }
  118.  
  119. */
  120.  
  121. }
  122.  
  123. void printdata() {
  124.  
  125. Serial.println(data.j1PotX);
  126.  
  127. }
  128.  
  129. void resetData() {
  130. // Reset the values when there is no radio connection - Set initial default values
  131. data.j1PotX = 127;
  132. data.j1PotY = 127;
  133. data.j2PotX = 127;
  134. data.j2PotY = 127;
  135. data.button1 = 1;
  136. data.button2 = 1;
  137. data.button3 = 1;
  138. data.button4 = 1;
  139. }
  140.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×