LDRを使用して、Arduinoで光があるかどうかを教えてくれます。私のコードは非常に単純ですが、「ライト ライト ライト ライト」をスパムする代わりに、「ライト」と一度言い、ライトが消えたら「ライトなし」と一度言いたいと思います。「readanalogvoltage」から編集されたコード:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
if (voltage > 1.5)
{
Serial.print("Light");
Serial.println();
delay(5000);
}
if (voltage < 1.5)
{
Serial.print("No Light");
Serial.println();
delay(50);
}
}