CCCPaste

"Distance Control" rev_159

/********* 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: "Distance Control"
	- Source Code compiled for: Arduino Uno
	- Source Code created on: 2023-12-06 23:22:52

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* if object read by ultrasonic sensor is less than */
	/* 20cm, so switch on red led and switch off green */
	/* led. If object is above 100cm so switch on green */
	/* led and switch off red led. */
/****** 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_sensor_HC_SR04_Echo_PIN_D3 = 3;

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t VERDE_LED_PIN_D11 = 11;
const uint8_t ROSSO_LED_PIN_D12 = 12;
const uint8_t ultrasonic_sensor_HC_SR04_Trigger_PIN_D2 = 2;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
EasyButton button(ultrasonic_sensor_HC_SR04_Echo_PIN_D3);
Ultrasonic ultrasonic(ultrasonic_sensor_HC_SR04_Trigger_PIN_D2, ultrasonic_sensor_HC_SR04_Echo_PIN_D3);

void setup(void)
{
  // Initialize the input and output pins
  pinMode(ultrasonic_sensor_HC_SR04_Echo_PIN_D3, INPUT);
  pinMode(VERDE_LED_PIN_D11, OUTPUT);
  pinMode(ROSSO_LED_PIN_D12, OUTPUT);
  pinMode(ultrasonic_sensor_HC_SR04_Trigger_PIN_D2, OUTPUT);
  
  // Initialize the EasyButton instance
  button.begin();
}

void loop(void)
{
  // Read the distance measured by the ultrasonic sensor
  unsigned int distance = ultrasonic.read();

  // Check if the object is less than 20cm away
  if (distance < 20)
  {
    // Switch on the red LED and switch off the green LED
    digitalWrite(ROSSO_LED_PIN_D12, HIGH);
    digitalWrite(VERDE_LED_PIN_D11, LOW);
  }
  // Check if the object is above 100cm away
  else if (distance > 100)
  {
    // Switch on the green LED and switch off the red LED
    digitalWrite(ROSSO_LED_PIN_D12, LOW);
    digitalWrite(VERDE_LED_PIN_D11, HIGH);
  }
  
  // Put the rest of your main code here
  
  // Update the button state
  button.read();
}