Advertisement
pleasedontcode

"Sensor Control" rev_01

Nov 26th, 2024
170
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: "Sensor Control"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-11-26 12:24:05
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* DESIGN & IMPLEMENT A SYSTEM TO ADDRESS ANY */
  21.     /* CHALLENGE IN ANY FIELD OF APPLICATION. THE SYSTEM */
  22.     /* SHOULD INCLUDE AN LCD, A BLUETOOTH MODULE, TWO */
  23.     /* SENSORS & TWO ACTUATORS WITH ONE OF THE SENSORS & */
  24.     /* ACTUATORS BEING MODELLED. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /* START CODE */
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <LiquidCrystal.h> // Library for LCD
  31. #include <SoftwareSerial.h> // Library for Bluetooth communication
  32. #include <DHT.h> // Library for DHT sensor
  33. #include <Servo.h> // Library for Servo motor
  34.  
  35. /****** FUNCTION PROTOTYPES *****/
  36. void setup(void);
  37. void loop(void);
  38.  
  39. /****** GLOBAL VARIABLES *****/
  40. // LCD pins
  41. LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // RS, E, D4, D5, D6, D7
  42.  
  43. // Bluetooth pins
  44. SoftwareSerial bluetooth(10, 9); // RX, TX
  45.  
  46. // DHT sensor
  47. #define DHTPIN 7 // Pin where the DHT sensor is connected
  48. #define DHTTYPE DHT11 // DHT 11
  49. DHT dht(DHTPIN, DHTTYPE);
  50.  
  51. // Servo motor
  52. Servo myServo; // Create a servo object
  53.  
  54. /****** SETUP FUNCTION *****/
  55. void setup(void)
  56. {
  57.     // Initialize the LCD
  58.     lcd.begin(16, 2); // Set up the LCD's number of columns and rows
  59.     lcd.print("Initializing...");
  60.  
  61.     // Initialize Bluetooth
  62.     bluetooth.begin(9600); // Set the baud rate for Bluetooth
  63.  
  64.     // Initialize DHT sensor
  65.     dht.begin();
  66.  
  67.     // Attach the servo to pin 8
  68.     myServo.attach(8);
  69. }
  70.  
  71. /****** LOOP FUNCTION *****/
  72. void loop(void)
  73. {
  74.     // Read temperature and humidity from DHT sensor
  75.     float h = dht.readHumidity();
  76.     float t = dht.readTemperature();
  77.  
  78.     // Check if any reads failed and exit early (to try again).
  79.     if (isnan(h) || isnan(t)) {
  80.         lcd.print("Failed to read");
  81.         return;
  82.     }
  83.  
  84.     // Display temperature and humidity on LCD
  85.     lcd.clear();
  86.     lcd.print("T:");
  87.     lcd.print(t);
  88.     lcd.print("C H:");
  89.     lcd.print(h);
  90.     lcd.print("%");
  91.  
  92.     // Control the servo based on Bluetooth input
  93.     if (bluetooth.available()) {
  94.         char command = bluetooth.read();
  95.         if (command == '1') {
  96.             myServo.write(90); // Move servo to 90 degrees
  97.         } else if (command == '0') {
  98.             myServo.write(0); // Move servo to 0 degrees
  99.         }
  100.     }
  101.  
  102.     delay(2000); // Wait for 2 seconds before the next loop
  103. }
  104.  
  105. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement