Arduino code

Zengzi Li and Cheng Pang

#include <Servo.h>  // Include the Servo library


// Define the pin where the photoresistor is connected

const int photoResistorPin = A0;


// Define the pin where the servo is connected

const int servoPin = 12;


// Create a Servo object

Servo myServo;


void setup() {

  // Start the Serial Monitor at 9600 baud

  Serial.begin(9600);

  

  // Attach the servo to pin 12

  myServo.attach(servoPin);

  

  // Move the servo to its initial position (0 degrees)

  myServo.write(0);

  delay(1000); // Wait 1 second to ensure the servo is at 0 degrees

}


void loop() {

  // Read the value from the photoresistor

  int sensorValue = analogRead(photoResistorPin);

  

  // Print the sensor value to the Serial Monitor

  Serial.print("Photoresistor Value: ");

  Serial.println(sensorValue);

  

  // Check if the sensor value is above the threshold

  if (sensorValue > 22) {

    // Rotate the servo to 180 degrees

    myServo.write(7200);

    delay(1000);  // Wait for 1 second

    

    // Rotate the servo back to 0 degrees

    myServo.write(0);

    delay(1000);  // Wait for 1 second before checking again

  }

  

  // A small delay before checking again

  delay(100);

}