Advertisement
pleasedontcode

"Light-Controlled Automation" rev_01

Jun 19th, 2024
561
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: "Light-Controlled Automation"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-06-19 07:03:35
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Develop a solar tracker system using Servo and */
  21.     /* Relay libraries to control a servomotor and a */
  22.     /* relay module. The system should adjust the solar */
  23.     /* panel's position based on sunlight intensity, */
  24.     /* optimizing energy capture. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Servo.h>  //https://github.com/arduino-libraries/Servo
  29. #include <Relay.h>  //https://github.com/rafaelnsantos/Relay
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void updateOutputs(void);
  35.  
  36. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  37. const uint8_t relay_RelayModule_Signal_PIN_D2 = 2;
  38.  
  39. /***** DEFINITION OF PWM OUTPUT PINS *****/
  40. const uint8_t servo1_Servomotor_PWMSignal_PIN_D3 = 3;
  41.  
  42. /***** DEFINITION OF ANALOG INPUT PINS *****/
  43. const uint8_t lightSensor_PIN_A0 = A0;
  44.  
  45. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  46. /***** used to store raw data *****/
  47. bool relay_RelayModule_Signal_PIN_D2_rawData = 0;
  48. uint8_t servo1_Servomotor_PWMSignal_PIN_D3_rawData = 0;
  49.  
  50. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  51. /***** used to store data after characteristic curve transformation *****/
  52. float relay_RelayModule_Signal_PIN_D2_phyData = 0.0;
  53. float servo1_Servomotor_PWMSignal_PIN_D3_phyData = 0.0;
  54.  
  55. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  56. Servo myservo;  // Create an instance of the Servo class
  57. Relay light(relay_RelayModule_Signal_PIN_D2, 10);  // Create an instance of the Relay class with a period of 10 seconds
  58.  
  59. void setup(void)
  60. {
  61.     // put your setup code here, to run once:
  62.  
  63.     pinMode(relay_RelayModule_Signal_PIN_D2, OUTPUT);
  64.     myservo.attach(servo1_Servomotor_PWMSignal_PIN_D3);  // Attach the servo to the specified pin
  65.    
  66.     light.setRelayMode(relayModeAutomatic);  // Set the relay mode to automatic
  67. }
  68.  
  69. void loop(void)
  70. {
  71.     // put your main code here, to run repeatedly:
  72.  
  73.     int lightIntensity = analogRead(lightSensor_PIN_A0);  // Read the light intensity from the sensor
  74.     servo1_Servomotor_PWMSignal_PIN_D3_rawData = map(lightIntensity, 0, 1023, 0, 180);  // Map the light intensity to servo angle
  75.  
  76.     updateOutputs(); // Refresh output data
  77.     light.loop();  // Control the relay based on period and duty cycle
  78. }
  79.  
  80. void updateOutputs()
  81. {
  82.     digitalWrite(relay_RelayModule_Signal_PIN_D2, relay_RelayModule_Signal_PIN_D2_rawData);
  83.     myservo.write(servo1_Servomotor_PWMSignal_PIN_D3_rawData);  // Write the servo position
  84. }
  85.  
  86. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement