Advertisement
pleasedontcode

**Sensor Navigation** rev_01

Feb 4th, 2025
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: **Sensor Navigation**
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-02-05 05:56:40
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Create an autonomous robot that integrates */
  21.     /* ultrasonic object detection and infrared line- */
  22.     /* following capabilities to navigate complex */
  23.     /* environments while avoiding obstacles effectively. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /* START CODE */
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <NewPing.h>  // Library for ultrasonic sensor
  30. #include <IRremote.h> // Library for infrared communication
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35.  
  36. // Define constants for ultrasonic sensor
  37. #define TRIGGER_PIN  12 // Pin connected to the trigger of the ultrasonic sensor
  38. #define ECHO_PIN     11 // Pin connected to the echo of the ultrasonic sensor
  39. #define MAX_DISTANCE 200 // Maximum distance to check for obstacles (in cm)
  40.  
  41. // Create an instance of the NewPing class
  42. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
  43.  
  44. // Define constants for infrared line following
  45. #define IR_RECEIVE_PIN 2 // Pin connected to the IR receiver
  46.  
  47. // Create an instance of the IRrecv class
  48. IRrecv irrecv(IR_RECEIVE_PIN);
  49. decode_results results;
  50.  
  51. void setup(void)
  52. {
  53.     // Initialize serial communication for debugging
  54.     Serial.begin(9600);
  55.    
  56.     // Start the infrared receiver
  57.     irrecv.enableIRIn();
  58. }
  59.  
  60. void loop(void)
  61. {
  62.     // Check for obstacles using the ultrasonic sensor
  63.     unsigned long distance = sonar.ping_cm(); // Get distance in cm
  64.     Serial.print("Distance: ");
  65.     Serial.print(distance);
  66.     Serial.println(" cm");
  67.  
  68.     // Check for infrared signals
  69.     if (irrecv.decode(&results)) {
  70.         Serial.print("IR Signal Received: ");
  71.         Serial.println(results.value);
  72.         irrecv.resume(); // Receive the next value
  73.     }
  74.  
  75.     // Implement obstacle avoidance and line following logic here
  76.     if (distance < 20) { // If an obstacle is detected within 20 cm
  77.         Serial.println("Obstacle detected! Stopping.");
  78.         // Code to stop or change direction
  79.     } else {
  80.         Serial.println("Path is clear. Moving forward.");
  81.         // Code to move forward
  82.     }
  83.  
  84.     delay(100); // Delay for stability
  85. }
  86.  
  87. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement