Advertisement
GozuMezu

SMARS - receiver

Jan 12th, 2025
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 5.72 KB | Software | 0 0
  1. #include <SPI.h>
  2. #include <nRF24L01.h>
  3. #include <RF24.h>
  4. #include <NewPing.h>
  5.  
  6. #define TRIGGER_PIN 2     // Arduino pin tied to trigger pin on the ultrasonic sensor.
  7. #define ECHO_PIN 10       // Arduino pin tied to echo pin on the ultrasonic sensor.
  8. #define MAX_DISTANCE 200  // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
  9.  
  10. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);  // NewPing setup of pins and maximum distance.
  11.  
  12. int in1 = 3;  //Motor Input 1
  13. int in2 = 4;  //Motor Input 2
  14. int in3 = 5;  //Motor Input 1
  15. int in4 = 6;  //Motor Input 2
  16.  
  17. int Array[3];
  18.  
  19. bool AutoDrive = 0;
  20.  
  21. unsigned int ping = 0;
  22.  
  23. unsigned long lastFeedbackTime = 0;
  24. unsigned long delayTime = 750;
  25.  
  26. RF24 radio(8, 9);
  27. const byte address[6] = "00001";
  28.  
  29. const int deadZone = 5;
  30. int xAxis, yAxis, Btn;
  31. int baseSpeed, turnSpeed;
  32. String direction;
  33. String yaw;
  34.  
  35. void setup() {
  36.   //set motor pins to output
  37.   pinMode(in1, OUTPUT);
  38.   pinMode(in2, OUTPUT);
  39.   pinMode(in3, OUTPUT);
  40.   pinMode(in4, OUTPUT);
  41.  
  42.   pinMode(TRIGGER_PIN, OUTPUT);
  43.   pinMode(ECHO_PIN, INPUT);
  44.  
  45.   Serial.begin(9600);
  46.  
  47.   radio.begin();
  48.   radio.openReadingPipe(0, address);
  49.   radio.setPALevel(RF24_PA_MIN);
  50.   radio.startListening();
  51.  
  52.   digitalWrite(in1, LOW);
  53.   digitalWrite(in2, LOW);
  54.   digitalWrite(in3, LOW);
  55.   digitalWrite(in4, LOW);
  56.  
  57.   lastFeedbackTime = millis();
  58.  
  59.   Serial.println("It has begun");
  60. }
  61.  
  62. void loop() {
  63.  
  64.   delay(20);
  65.   ping = sonar.ping_cm();
  66.  
  67.   if (radio.available()) {
  68.  
  69.     radio.read(&Array, sizeof(Array));
  70.  
  71.     xAxis = Array[0];
  72.     yAxis = Array[1];
  73.     Btn = Array[2];
  74.   }
  75.  
  76.   drive();
  77.  
  78.   if ((millis() - lastFeedbackTime) > delayTime) {
  79.     Serial.print("Btn: ");
  80.     Serial.print(Array[2]);
  81.     Serial.print(", ");
  82.    
  83.     Serial.println(ping);
  84.     lastFeedbackTime = millis();
  85.     //feedback();
  86.   }
  87.  
  88.   // if (Btn == 1) {
  89.   //   AutoDrive = !AutoDrive;
  90.   // }
  91.  
  92.   // if (AutoDrive == 1) {
  93.   //   AutomatedDriving();
  94.   // } else {
  95.   //   drive();
  96.   // }
  97. }
  98.  
  99. void feedback() {
  100.   Serial.print("X: ");
  101.   Serial.print(Array[0]);
  102.   Serial.print(", ");
  103.   Serial.print("Y: ");
  104.   Serial.print(Array[1]);
  105.   Serial.print(", ");
  106.   Serial.print("Btn: ");
  107.   Serial.print(Array[2]);
  108.   Serial.print(", ");
  109.   Serial.println(AutoDrive);
  110. }
  111.  
  112. void drive() {
  113.   if (xAxis > 522) {
  114.     direction = "forward";
  115.     baseSpeed = map(xAxis, 522, 1023, 0, 255);
  116.   } else if (xAxis < 510) {
  117.     direction = "backwards";
  118.     baseSpeed = map(xAxis, 0, 510, 255, 0);
  119.   } else {
  120.     direction = "hold";
  121.     baseSpeed = 0;
  122.   }
  123.  
  124.   if (yAxis > 522) {
  125.     yaw = "right";
  126.     turnSpeed = map(yAxis, 522, 1023, 0, 225);
  127.   } else if (yAxis < 510) {
  128.     yaw = "left";
  129.     turnSpeed = map(yAxis, 0, 510, 225, 0);
  130.   } else {
  131.     yaw = "hold";
  132.   }
  133.  
  134.   if (direction == "forward") {
  135.     if (yaw == "left") {
  136.       int motor1Speed = baseSpeed + turnSpeed;
  137.       int motor2Speed = baseSpeed - turnSpeed;
  138.       analogWrite(in1, motor1Speed);
  139.       analogWrite(in2, 0);
  140.       analogWrite(in3, motor2Speed);
  141.       analogWrite(in4, 0);
  142.     } else if (yaw == "right") {
  143.       int motor1Speed = baseSpeed - turnSpeed;
  144.       int motor2Speed = baseSpeed + turnSpeed;
  145.       analogWrite(in1, motor1Speed);
  146.       analogWrite(in2, 0);
  147.       analogWrite(in3, motor2Speed);
  148.       analogWrite(in4, 0);
  149.     } else if (yaw == "hold") {
  150.       analogWrite(in1, baseSpeed);
  151.       analogWrite(in2, 0);
  152.       analogWrite(in3, baseSpeed);
  153.       analogWrite(in4, 0);
  154.     }
  155.   } else if (direction == "backwards") {
  156.     if (yaw == "left") {
  157.       int motor1Speed = baseSpeed + turnSpeed;
  158.       int motor2Speed = baseSpeed - turnSpeed;
  159.       analogWrite(in1, 0);
  160.       analogWrite(in2, motor1Speed);
  161.       analogWrite(in3, 0);
  162.       analogWrite(in4, motor2Speed);
  163.     } else if (yaw == "right") {
  164.       int motor1Speed = baseSpeed - turnSpeed;
  165.       int motor2Speed = baseSpeed + turnSpeed;
  166.       analogWrite(in1, 0);
  167.       analogWrite(in2, motor1Speed);
  168.       analogWrite(in3, 0);
  169.       analogWrite(in4, motor2Speed);
  170.     } else if (yaw == "hold") {
  171.       analogWrite(in1, 0);
  172.       analogWrite(in2, baseSpeed);
  173.       analogWrite(in3, 0);
  174.       analogWrite(in4, baseSpeed);
  175.     }
  176.   } else if (direction == "hold") {
  177.     if (yaw == "left") {
  178.       analogWrite(in1, turnSpeed);
  179.       analogWrite(in2, 0);
  180.       analogWrite(in3, 0);
  181.       analogWrite(in4, turnSpeed);
  182.     } else if (yaw == "right") {
  183.       analogWrite(in1, 0);
  184.       analogWrite(in2, turnSpeed);
  185.       analogWrite(in3, turnSpeed);
  186.       analogWrite(in4, 0);
  187.     } else if (yaw == "hold") {
  188.       analogWrite(in1, 0);
  189.       analogWrite(in2, 0);
  190.       analogWrite(in3, 0);
  191.       analogWrite(in4, 0);
  192.     }
  193.   }
  194. }
  195.  
  196. void AutomatedDriving() {
  197.   delay(5);  // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  198.   // Serial.print("Ping: ");
  199.   // Serial.print(sonar.ping_cm());  // Send ping, get distance in cm and print result (0 = outside set distance range)
  200.   // Serial.println("cm");
  201.  
  202.   int ping = sonar.ping_cm();
  203.   Serial.println(ping);
  204.  
  205.   if (ping < 8) {
  206.     Serial.println("Collision Imminent! Take evasive action!");
  207.     //Back
  208.     digitalWrite(in1, LOW);
  209.     digitalWrite(in2, HIGH);
  210.     digitalWrite(in3, LOW);
  211.     digitalWrite(in4, HIGH);
  212.     delay(500);
  213.  
  214.     //Right
  215.     digitalWrite(in1, LOW);
  216.     digitalWrite(in2, HIGH);
  217.     digitalWrite(in3, HIGH);
  218.     digitalWrite(in4, LOW);
  219.     delay(350);
  220.   } else {
  221.     Serial.println("Steady as she goes...");
  222.     //Forward
  223.     digitalWrite(in1, HIGH);
  224.     digitalWrite(in2, LOW);
  225.     digitalWrite(in3, HIGH);
  226.     digitalWrite(in4, LOW);
  227.   }
  228. }
  229.  
  230. void Sonartest() {
  231.   delay(5);
  232.   ping = sonar.ping_cm();
  233.   Serial.println(ping);
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement