/********* 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: "Ultrasonic Control"
- Source Code compiled for: Arduino Mega
- Source Code created on: 2024-01-02 02:01:05
********* Pleasedontcode.com **********/
/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
/* Measure la distance à droite et à gauche */
/****** END SYSTEM REQUIREMENTS *****/
/****** DEFINITION OF LIBRARIES *****/
#include
#include
#include
/********** FUNCTION PROTOTYPES **********/
void setup(void);
void loop(void);
/********** DEFINITION OF DIGITAL INPUT PINS **********/
const uint8_t Ultrasonic_HCSR04_Echo_PIN_RIGHT = 3;
const uint8_t Ultrasonic_HCSR04_Echo_PIN_LEFT = 4;
/********** DEFINITION OF DIGITAL OUTPUT PINS **********/
const uint8_t Ultrasonic_HCSR04_Trigger_PIN = 2;
/********** DEFINITION OF PWM OUTPUT PINS **********/
const uint8_t Servo_Servomotor_PWMSignal_PIN = 5;
/********** DEFINITION OF LIBRARY CLASS INSTANCES **********/
Ultrasonic ultrasonicRight(Ultrasonic_HCSR04_Echo_PIN_RIGHT);
Ultrasonic ultrasonicLeft(Ultrasonic_HCSR04_Echo_PIN_LEFT);
Servo servoMotor;
void setup(void)
{
// put your setup code here, to run once:
pinMode(Ultrasonic_HCSR04_Echo_PIN_RIGHT, INPUT);
pinMode(Ultrasonic_HCSR04_Echo_PIN_LEFT, INPUT);
pinMode(Ultrasonic_HCSR04_Trigger_PIN, OUTPUT);
pinMode(Servo_Servomotor_PWMSignal_PIN, OUTPUT);
servoMotor.attach(Servo_Servomotor_PWMSignal_PIN);
Serial.begin(9600);
}
void loop(void)
{
// Measure the distance to the right
unsigned int distanceRight = ultrasonicRight.read();
Serial.print("Distance to the right: ");
Serial.print(distanceRight);
Serial.println(" cm");
// Measure the distance to the left
unsigned int distanceLeft = ultrasonicLeft.read();
Serial.print("Distance to the left: ");
Serial.print(distanceLeft);
Serial.println(" cm");
delay(1000);
}