Advertisement
pleasedontcode

"Light-Controlled Servo" rev_02

Feb 15th, 2024
73
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 Servo"
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-02-15 18:17:01
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Двигатель, подключенный к контакту D3, должен */
  21.     /* поворачиваться ровно на 90 градусов, когда датчик */
  22.     /* освещенности обнаруживает источник света. */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <Servo.h>
  27. #include <TroykaLight.h>
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32. void updateOutputs(void);
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t sensor_PIN_D2 = 2;
  36.  
  37. /***** DEFINITION OF PWM OUTPUT PINS *****/
  38. const uint8_t motor_Servomotor_PWMSignal_PIN_D3 = 3;
  39.  
  40. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  41. uint16_t sensorValue;
  42. float lightLuxValue;
  43.  
  44. /******** DEFINITION OF LIBRARY CLASS INSTANCES*******/
  45. Servo myservo;
  46. TroykaLight light(A0);
  47.  
  48. void setup(void)
  49. {
  50.   // put your setup code here, to run once:
  51.   pinMode(sensor_PIN_D2, INPUT);
  52.   myservo.attach(motor_Servomotor_PWMSignal_PIN_D3);
  53.  
  54.   Serial.begin(9600);
  55. }
  56.  
  57. void loop(void)
  58. {
  59.   // put your main code here, to run repeatedly:
  60.   updateOutputs(); // Refresh output data
  61.   delay(500); // Delay for smoother operation
  62. }
  63.  
  64. void updateOutputs()
  65. {
  66.   // Read sensor input
  67.   sensorValue = analogRead(sensor_PIN_D2);
  68.   light.read();
  69.   lightLuxValue = light.getLightLux();
  70.  
  71.   // Map sensor value to servo angle
  72.   int mappedVal = map(sensorValue, 0, 1023, 0, 180);
  73.  
  74.   // Rotate servo to 90 degrees if light is detected
  75.   if (lightLuxValue > 0) {
  76.     mappedVal = 90;
  77.   }
  78.  
  79.   // Write mapped value to servo
  80.   myservo.write(mappedVal);
  81.  
  82.   // Print sensor and lux values
  83.   Serial.print("Sensor value: ");
  84.   Serial.println(sensorValue);
  85.   Serial.print("Light Lux: ");
  86.   Serial.println(lightLuxValue);
  87.  
  88.   // Check if servo is at 90 degrees
  89.   if (mappedVal == 90) {
  90.     Serial.println("Servo is at 90 degrees");
  91.   }
  92. }
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement