rhandycan1

NRF24L01_RC CAR_transmitter_ESP32C3

Nov 8th, 2025 (edited)
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 KB | Source Code | 0 0
  1. //PROGRAM BY: RANDY N. CAÑADA (NOV. 2025)
  2.  
  3. //CONNECTIONS NRF24L01
  4. /*CE_PIN 7
  5. CSN_PIN 10
  6. SCK_PIN 4
  7. MOSI_PIN 6
  8. MISO_PIN 5
  9. GND-GND
  10. VCC-5Vdc/3.3vdc
  11.  
  12. JOYSTICKS:
  13. VRX = A1
  14. VRY= A0
  15. gnd-grnd
  16. vcc-vcc
  17.  
  18. note: when you are using breakout board for nrfl01, you can supply 5vdc. If not use 3.3vdc
  19.  
  20.  
  21. MY YOUTUBE CHANNEL: ElectroCrafters:
  22. https://www.youtube.com/channel/UCu1aqzUdTqSnTN5BHNlbVgA
  23.  
  24. MY FB CHANNEL: FB PAGE:
  25. Arduino:
  26. https://www.facebook.com/rhacan2?__tn__=R
  27.  
  28. PLS ALSO SUPPORT MY OTHER PAGE:FB PAGES:
  29. Playlist.ph:
  30. https://www.facebook.com/profile.php?id=61573177858722
  31.  
  32. SUPPORT MY OTHER YOUTUBE CHANNEL:
  33. PlaylistPH
  34. https://www.youtube.com/@PlaylistPh-h2v
  35.  
  36. */
  37. #include <SPI.h>
  38. #include <RF24.h>
  39.  
  40. #define CE_PIN 7
  41. #define CSN_PIN 10
  42.  
  43. #define joyVert A0 // VRy → forward/backward
  44. #define joyHorz A1 // VRx → turning
  45.  
  46. RF24 radio(CE_PIN, CSN_PIN);
  47. const byte address[6] = "00001";
  48.  
  49. struct Data_Package {
  50.   int8_t motorLeft;
  51.   int8_t motorRight;
  52. };
  53.  
  54. Data_Package data;
  55.  
  56. int centerVert = 0;
  57. int centerHorz = 0;
  58.  
  59. void setup() {
  60.   Serial.begin(115200);
  61.   pinMode(joyVert, INPUT);
  62.   pinMode(joyHorz, INPUT);
  63.  
  64.   SPI.begin();
  65.   //SPI.begin(4, 5, 6); // SCK, MISO, MOSI
  66.  
  67.  
  68.   if (!radio.begin()) {
  69.     Serial.println("NRF24 not responding!");
  70.     while (1);
  71.   }
  72.  
  73.   radio.openWritingPipe(address);
  74.   radio.setChannel(100);
  75.   radio.setPALevel(RF24_PA_LOW);
  76.   radio.setDataRate(RF24_250KBPS);
  77.   radio.stopListening();
  78.  
  79.   // Calibrate joystick center
  80.   Serial.println("Calibrating joystick, keep it untouched...");
  81.   delay(2000);
  82.   centerVert = analogRead(joyVert);
  83.   centerHorz = analogRead(joyHorz);
  84.   Serial.print("Center Vert: "); Serial.print(centerVert);
  85.   Serial.print("  Center Horz: "); Serial.println(centerHorz);
  86.   Serial.println("Calibration done!");
  87. }
  88.  
  89. void loop() {
  90.   int deadZone = 30;
  91.  
  92.   int joyV_raw = analogRead(joyVert) - centerVert;
  93.   int joyH_raw = analogRead(joyHorz) - centerHorz;
  94.  
  95.   int joyV = 0;
  96.   int joyH = 0;
  97.  
  98.   // Map Vertical (forward/backward)
  99.   if (abs(joyV_raw) > deadZone) {
  100.     joyV = map(joyV_raw, -512, 512, -127, 127);
  101.   }
  102.  
  103.   // Map Horizontal (turning)
  104.   if (abs(joyH_raw) > deadZone) {
  105.     joyH = map(joyH_raw, -512, 512, -127, 127);
  106.   }
  107.  
  108.   // Combine for motor speeds
  109.   int MotorLeft = joyV + joyH;
  110.   int MotorRight = joyV - joyH;
  111.  
  112.   MotorLeft = constrain(MotorLeft, -127, 127);
  113.   MotorRight = constrain(MotorRight, -127, 127);
  114.  
  115.   if (abs(MotorLeft) < 5) MotorLeft = 0;
  116.   if (abs(MotorRight) < 5) MotorRight = 0;
  117.  
  118.   data.motorLeft = MotorLeft;
  119.   data.motorRight = MotorRight;
  120.  
  121.   radio.write(&data, sizeof(Data_Package));
  122.  
  123.   Serial.print("joyV_raw: "); Serial.print(joyV_raw);
  124.   Serial.print("  joyH_raw: "); Serial.print(joyH_raw);
  125.   Serial.print("  MotorLeft: "); Serial.print(MotorLeft);
  126.   Serial.print("  MotorRight: "); Serial.println(MotorRight);
  127.  
  128.   delay(100);
  129. }
  130.  
Advertisement
Add Comment
Please, Sign In to add comment