Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: "Light-Controlled Servo"
- - Source Code compiled for: Arduino Uno
- - Source Code created on: 2024-02-15 18:17:01
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Двигатель, подключенный к контакту D3, должен */
- /* поворачиваться ровно на 90 градусов, когда датчик */
- /* освещенности обнаруживает источник света. */
- /****** END SYSTEM REQUIREMENTS *****/
- /****** DEFINITION OF LIBRARIES *****/
- #include <Servo.h>
- #include <TroykaLight.h>
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void updateOutputs(void);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t sensor_PIN_D2 = 2;
- /***** DEFINITION OF PWM OUTPUT PINS *****/
- const uint8_t motor_Servomotor_PWMSignal_PIN_D3 = 3;
- /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
- uint16_t sensorValue;
- float lightLuxValue;
- /******** DEFINITION OF LIBRARY CLASS INSTANCES*******/
- Servo myservo;
- TroykaLight light(A0);
- void setup(void)
- {
- // put your setup code here, to run once:
- pinMode(sensor_PIN_D2, INPUT);
- myservo.attach(motor_Servomotor_PWMSignal_PIN_D3);
- Serial.begin(9600);
- }
- void loop(void)
- {
- // put your main code here, to run repeatedly:
- updateOutputs(); // Refresh output data
- delay(500); // Delay for smoother operation
- }
- void updateOutputs()
- {
- // Read sensor input
- sensorValue = analogRead(sensor_PIN_D2);
- light.read();
- lightLuxValue = light.getLightLux();
- // Map sensor value to servo angle
- int mappedVal = map(sensorValue, 0, 1023, 0, 180);
- // Rotate servo to 90 degrees if light is detected
- if (lightLuxValue > 0) {
- mappedVal = 90;
- }
- // Write mapped value to servo
- myservo.write(mappedVal);
- // Print sensor and lux values
- Serial.print("Sensor value: ");
- Serial.println(sensorValue);
- Serial.print("Light Lux: ");
- Serial.println(lightLuxValue);
- // Check if servo is at 90 degrees
- if (mappedVal == 90) {
- Serial.println("Servo is at 90 degrees");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement