A physical output device utilizing Arduino technology
#include <Servo.h>
Servo servo1, servo2, servo3;
const int photoresistorPin = A0;
const int number = 400;
void setup() {
Serial.begin(9600);
servo1.attach(9);
servo2.attach(10);
servo3.attach(11);
pinMode(photoresistorPin, INPUT);
}
void loop() {
int photoresistorValue = analogRead(photoresistorPin);
Serial.println(photoresistorValue); // Debugging line - print photoresistor value to serial monitor
if (photoresistorValue < number) { // If photoresistor is in the dark
int randomServo = random(1, 4); // Choose a random number between 1 and 3
if (randomServo == 1) {
servo1.write(90); // Activate Servo 1
} else if (randomServo == 2) {
servo2.write(90); // Activate Servo 2
} else if (randomServo == 3) {
servo3.write(90); // Activate Servo 3
}
delay(1000); // Wait 1 second
servo1.write(0); // Deactivate all servos
servo2.write(0);
servo3.write(0);
}
}