Advertisement
pleasedontcode

"Sensor-Controlled Servo" rev_03

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