Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: **Sensor Navigation**
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2025-02-05 05:56:40
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Create an autonomous robot that integrates */
- /* ultrasonic object detection and infrared line- */
- /* following capabilities to navigate complex */
- /* environments while avoiding obstacles effectively. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <NewPing.h> // Library for ultrasonic sensor
- #include <IRremote.h> // Library for infrared communication
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- // Define constants for ultrasonic sensor
- #define TRIGGER_PIN 12 // Pin connected to the trigger of the ultrasonic sensor
- #define ECHO_PIN 11 // Pin connected to the echo of the ultrasonic sensor
- #define MAX_DISTANCE 200 // Maximum distance to check for obstacles (in cm)
- // Create an instance of the NewPing class
- NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
- // Define constants for infrared line following
- #define IR_RECEIVE_PIN 2 // Pin connected to the IR receiver
- // Create an instance of the IRrecv class
- IRrecv irrecv(IR_RECEIVE_PIN);
- decode_results results;
- void setup(void)
- {
- // Initialize serial communication for debugging
- Serial.begin(9600);
- // Start the infrared receiver
- irrecv.enableIRIn();
- }
- void loop(void)
- {
- // Check for obstacles using the ultrasonic sensor
- unsigned long distance = sonar.ping_cm(); // Get distance in cm
- Serial.print("Distance: ");
- Serial.print(distance);
- Serial.println(" cm");
- // Check for infrared signals
- if (irrecv.decode(&results)) {
- Serial.print("IR Signal Received: ");
- Serial.println(results.value);
- irrecv.resume(); // Receive the next value
- }
- // Implement obstacle avoidance and line following logic here
- if (distance < 20) { // If an obstacle is detected within 20 cm
- Serial.println("Obstacle detected! Stopping.");
- // Code to stop or change direction
- } else {
- Serial.println("Path is clear. Moving forward.");
- // Code to move forward
- }
- delay(100); // Delay for stability
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement