we updated the code and switch the sensor
#include <Servo.h> // Include the Servo library
// Define the pin connected to the photoresistor
const int photoPin = A0; // Photoresistor (Adafruit 161) connected to analog pin A0
// Define the pin for the servo motor
const int servoPin = 9; // Servo motor connected to pin 9
// Define the threshold value to detect light
int lightThreshold = 900; // Example threshold
// Define the threshold to detect if the sensor is disconnected
const int disconnectedThreshold = 5; // Adjust if necessary
// Create a Servo object
Servo myServo;
// Variable to track light detection state
bool isLightDetected = false; // Track if light level is currently above threshold
void setup() {
// Start serial communication
Serial.begin(9600);
while (!Serial) {
; // Wait for the serial port to connect (necessary for native USB port boards)
}
// Attach the servo to pin 9
myServo.attach(servoPin);
// Initialize the servo to its resting position (0 degrees)
myServo.write(0); // Start at 0 degrees
// Print a startup message
Serial.println("Photoresistor (Adafruit 161) with Servo Example Initialized");
}
void loop() {
// Read the analog value from the photoresistor (range 0 to 1023)
int sensorValue = analogRead(photoPin);
// Print the sensor value to the serial monitor
Serial.print("Photoresistor Value: ");
Serial.println(sensorValue);
// Check if the sensor is disconnected
if (sensorValue < disconnectedThreshold) {
Serial.println("Warning: Sensor Disconnected! Moving Servo to Safe Position (70°).");
myServo.write(70); // Move to a neutral position to avoid false readings
return; // Skip further processing to prevent incorrect behavior
}
// Check if the sensor value exceeds the threshold (light detected)
if (sensorValue > lightThreshold) {
if (!isLightDetected) {
// Light newly detected, move the servo to 0 degrees
Serial.println("Light Detected");
myServo.write(0); // Move servo to 0 degrees
isLightDetected = true; // Mark that light is detected
}
} else {
// Light is below threshold
if (isLightDetected) {
Serial.println("Light Below Threshold");
myServo.write(70); // Move servo to 70 degrees
isLightDetected = false; // Reset the detection state
}
}
// Longer delay to observe changes more clearly (1 second)
delay(1000);
}
#include <Servo.h> // Include the Servo library
// Define the pin connected to the photoresistor
const int photoPin = A0; // Photoresistor (Adafruit 161) connected to analog pin A0
// Define the pin for the servo motor
const int servoPin = 9; // Servo motor connected to pin 9
// Define the threshold value to detect light
// Adjust this value based on the specific lighting conditions
int lightThreshold = 50; // Example threshold
// Create a Servo object
Servo myServo;
// Variable to track light detection state
bool isLightDetected = false; // Track if light level is currently above threshold
void setup() {
// Start serial communication
Serial.begin(9600);
while (!Serial) {
; // Wait for the serial port to connect (necessary for native USB port boards)
}
// Attach the servo to pin 9
myServo.attach(servoPin);
// Initialize the servo to its resting position (0 degrees)
myServo.write(0); // Start at 0 degrees
// Print a startup message
Serial.println("Photoresistor (Adafruit 161) with Servo Example Initialized");
}
void loop() {
// Read the analog value from the photoresistor (range 0 to 1023)
int sensorValue = analogRead(photoPin);
// Print the sensor value to the serial monitor
Serial.print("Photoresistor Value: ");
Serial.println(sensorValue);
// Check if the sensor value exceeds the threshold (light detected)
if (sensorValue > lightThreshold) {
if (!isLightDetected) {
// Light newly detected, move the servo to 0 degrees
Serial.println("Light Detected");
myServo.write(0); // Move servo to 0 degrees
isLightDetected = true; // Mark that light is detected
}
} else {
// Light is below threshold
if (isLightDetected) {
Serial.println("Light Below Threshold");
myServo.write(70); // Move servo to 70 degrees
isLightDetected = false; // Reset the detection state
}
}
// Longer delay to observe changes more clearly (1 second)
delay(1000);
}
Maybe we can use growable material to make the roof. When a part is missing, it will automatically grow and be fixed.
Efficiency: A more advanced project will have only one backup piece and automatically move there when it detects the roof broken.
cant use cardboard bc it's burning in the laser cutter. We have to switch to wood
#include <Servo.h> // Include the Servo library
// Define the pin connected to the pressure sensor (FSR)
const int fsrPin = A0; // FSR sensor connected to analog pin A0
// Define the pin for the servo motor
const int servoPin = 9; // Servo motor connected to pin 9
// Define the threshold value to detect pressure (lowered for increased sensitivity)
int threshold = 50; // Adjusted threshold to 50 for higher sensitivity
// Create a Servo object
Servo myServo;
// Variable to track pressure state
bool isPressureApplied = false; // Track if pressure is currently applied
void setup() {
// Start the serial communication
Serial.begin(9600);
// Wait for the serial port to connect
while (!Serial) {
; // Wait for the serial port to connect (necessary for native USB port boards)
}
// Attach the servo to pin 9
myServo.attach(servoPin);
// Initialize the servo to its resting position (0 degrees)
myServo.write(0); // Set servo to 0 degrees at the start
// Print a startup message
Serial.println("Pressure Sensor with Servo Example Initialized");
} // <-- Closing brace for setup() here!
void loop() {
// Read the analog value from the sensor (0 to 1023)
int sensorValue = analogRead(fsrPin);
// Print the sensor value to the serial monitor (optional)
Serial.print("FSR Value: ");
Serial.println(sensorValue);
// Check if the sensor value exceeds the threshold (pressure detected)
if (sensorValue > threshold) {
if (!isPressureApplied) {
// Pressure applied, move the servo to 0 degrees
Serial.println("Pressure Detected");
myServo.write(0); // Move servo to 0 degrees when pressure is applied
isPressureApplied = true; // Mark that pressure is applied
}
} else {
// Pressure released (sensor value below threshold)
if (isPressureApplied) {
Serial.println("Pressure Released");
myServo.write(70); // Move servo to 70 degrees when pressure is released
isPressureApplied = false; // Reset the pressure state
}
}
// Minimize delay (set to 50ms for faster response)
delay(50); // Reduced delay to 50ms for faster response
}
improved code