CCCPaste

RC_CAR rev_45

/********* 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: RC_CAR
	- Source Code compiled for: Arduino Nano
	- Source Code created on: 2023-11-03 22:34:40
	- Source Code generated by: Francesco Alessandro

********* Pleasedontcode.com **********/
/****** DEFINITION OF LIBRARIES *****/
#include 
#include 
#include 

/****** SYSTEM REQUIREMENT 1 *****/
/* Potentiometer reads a pressure sensor. If there is */
/* an high pressure so close first servo. If there */
/* is a low pressure so close the second servo. If there */
/* is no pressure, so open both servos. */
/****** SYSTEM REQUIREMENT 2 *****/
/* read temperature and humidity from sensor and */
/* close all servos if temperature is above 40°C or */
/* humidity is lower than 20%. Read humidity from */
/* sensor2 and perform average with sensor1. */

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t sensor_DHT22_DOUT_PIN_D4 = 4;
const uint8_t sensor2_DHT11_DOUT_PIN_D2 = 2;

/***** DEFINITION OF ANALOG INPUT PINS *****/
const uint8_t myPot_Potentiometer_Vout_PIN_A0 = A0;

/***** DEFINITION OF PWM OUTPUT PINS *****/
const uint8_t servo_Servomotor_PWMSignal_PIN_D3 = 3;
const uint8_t servo2_Servomotor_PWMSignal_PIN_D5 = 5;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
Servo servo;
Servo servo2;
DHT dht(sensor_DHT22_DOUT_PIN_D4, DHT22);
DHT dht2(sensor2_DHT11_DOUT_PIN_D2, DHT11);

void setup(void)
{
  // put your setup code here, to run once:

  pinMode(sensor_DHT22_DOUT_PIN_D4, INPUT_PULLUP);
  pinMode(sensor2_DHT11_DOUT_PIN_D2, INPUT_PULLUP);
  pinMode(myPot_Potentiometer_Vout_PIN_A0, INPUT);

  servo.attach(servo_Servomotor_PWMSignal_PIN_D3);
  servo2.attach(servo2_Servomotor_PWMSignal_PIN_D5);

  dht.begin();
  dht2.begin();
}

void loop(void)
{
  // put your main code here, to run repeatedly:

  // Read pressure from potentiometer
  int pressure = analogRead(myPot_Potentiometer_Vout_PIN_A0);

  // Check pressure level and control servos accordingly
  if (pressure > 512)
  {
    // High pressure, close first servo
    servo.write(0);
    // Open second servo
    servo2.write(180);
  }
  else if (pressure < 512)
  {
    // Low pressure, close second servo
    servo.write(180);
    // Open first servo
    servo2.write(0);
  }
  else
  {
    // No pressure, open both servos
    servo.write(0);
    servo2.write(0);
  }

  // Read temperature and humidity from sensor
  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();

  // Read humidity from sensor2 and calculate average
  float humidity2 = dht2.readHumidity();
  float averageHumidity = (humidity + humidity2) / 2;

  // Check temperature and humidity levels and close all servos if conditions are met
  if (temperature > 40 || averageHumidity < 20)
  {
    servo.write(180);
    servo2.write(180);
  }

  delay(1000);
}