Advertisement
pleasedontcode

RC_CAR rev_45

Nov 3rd, 2023
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: RC_CAR
  13.     - Source Code compiled for: Arduino Nano
  14.     - Source Code created on: 2023-11-03 22:34:40
  15.     - Source Code generated by: Francesco Alessandro
  16.  
  17. ********* Pleasedontcode.com **********/
  18. /****** DEFINITION OF LIBRARIES *****/
  19. #include <Arduino.h>
  20. #include <Servo.h>
  21. #include <DHT.h>
  22.  
  23. /****** SYSTEM REQUIREMENT 1 *****/
  24. /* Potentiometer reads a pressure sensor. If there is */
  25. /* an high pressure so close first servo. If there */
  26. /* is a low pressure so close the second servo. If there */
  27. /* is no pressure, so open both servos. */
  28. /****** SYSTEM REQUIREMENT 2 *****/
  29. /* read temperature and humidity from sensor and */
  30. /* close all servos if temperature is above 40°C or */
  31. /* humidity is lower than 20%. Read humidity from */
  32. /* sensor2 and perform average with sensor1. */
  33.  
  34. /****** FUNCTION PROTOTYPES *****/
  35. void setup(void);
  36. void loop(void);
  37.  
  38. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  39. const uint8_t sensor_DHT22_DOUT_PIN_D4 = 4;
  40. const uint8_t sensor2_DHT11_DOUT_PIN_D2 = 2;
  41.  
  42. /***** DEFINITION OF ANALOG INPUT PINS *****/
  43. const uint8_t myPot_Potentiometer_Vout_PIN_A0 = A0;
  44.  
  45. /***** DEFINITION OF PWM OUTPUT PINS *****/
  46. const uint8_t servo_Servomotor_PWMSignal_PIN_D3 = 3;
  47. const uint8_t servo2_Servomotor_PWMSignal_PIN_D5 = 5;
  48.  
  49. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  50. Servo servo;
  51. Servo servo2;
  52. DHT dht(sensor_DHT22_DOUT_PIN_D4, DHT22);
  53. DHT dht2(sensor2_DHT11_DOUT_PIN_D2, DHT11);
  54.  
  55. void setup(void)
  56. {
  57.   // put your setup code here, to run once:
  58.  
  59.   pinMode(sensor_DHT22_DOUT_PIN_D4, INPUT_PULLUP);
  60.   pinMode(sensor2_DHT11_DOUT_PIN_D2, INPUT_PULLUP);
  61.   pinMode(myPot_Potentiometer_Vout_PIN_A0, INPUT);
  62.  
  63.   servo.attach(servo_Servomotor_PWMSignal_PIN_D3);
  64.   servo2.attach(servo2_Servomotor_PWMSignal_PIN_D5);
  65.  
  66.   dht.begin();
  67.   dht2.begin();
  68. }
  69.  
  70. void loop(void)
  71. {
  72.   // put your main code here, to run repeatedly:
  73.  
  74.   // Read pressure from potentiometer
  75.   int pressure = analogRead(myPot_Potentiometer_Vout_PIN_A0);
  76.  
  77.   // Check pressure level and control servos accordingly
  78.   if (pressure > 512)
  79.   {
  80.     // High pressure, close first servo
  81.     servo.write(0);
  82.     // Open second servo
  83.     servo2.write(180);
  84.   }
  85.   else if (pressure < 512)
  86.   {
  87.     // Low pressure, close second servo
  88.     servo.write(180);
  89.     // Open first servo
  90.     servo2.write(0);
  91.   }
  92.   else
  93.   {
  94.     // No pressure, open both servos
  95.     servo.write(0);
  96.     servo2.write(0);
  97.   }
  98.  
  99.   // Read temperature and humidity from sensor
  100.   float temperature = dht.readTemperature();
  101.   float humidity = dht.readHumidity();
  102.  
  103.   // Read humidity from sensor2 and calculate average
  104.   float humidity2 = dht2.readHumidity();
  105.   float averageHumidity = (humidity + humidity2) / 2;
  106.  
  107.   // Check temperature and humidity levels and close all servos if conditions are met
  108.   if (temperature > 40 || averageHumidity < 20)
  109.   {
  110.     servo.write(180);
  111.     servo2.write(180);
  112.   }
  113.  
  114.   delay(1000);
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement