CCCPaste

RC_CAR rev_44

/********* 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:28:49
	- 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 */
/* a high pressure, close the first servo. If there */
/* is a low pressure, close the second servo. If there */
/* is no pressure, 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%. */

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

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

/***** 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);

void setup(void)
{
  // put your setup code here, to run once:
  pinMode(sensor_DHT22_DOUT_PIN_D4, 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();
}

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

  // Close first servo if pressure is high, close second servo if pressure is low, open both servos if pressure is normal
  if (pressure > 800)
  {
    servo.write(180); // Close first servo
    servo2.write(0);  // Open second servo
  }
  else if (pressure < 200)
  {
    servo.write(0);   // Open first servo
    servo2.write(180); // Close second servo
  }
  else
  {
    servo.write(0);   // Open first servo
    servo2.write(0);  // Open second servo
  }

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

  // Close all servos if temperature is above 40°C or humidity is lower than 20%
  if (temperature > 40 || humidity < 20)
  {
    servo.write(180); // Close first servo
    servo2.write(180); // Close second servo
  }
  
  delay(1000); // Delay for 1 second before repeating the loop
}