I’m new to Arduino. I’m working on a project where I have to create a sort of hide and seek game with a light sensor and buzzer. When the light hits the sensor in the dark I need the sound to turn on. What kind of loop can I make for this?
What code have You made? No code? Then You need to find a loop shop. Forum is not that place.
If you are using an Light Dependant Resistor (LDR), a Google search for "arduino ldr tutorial" should yield some pages to get you started with how to connect the LDR and some example code. Once you can read the LDR use an if statement comparing the current light value to the threshold and act on the comparison as required.
Super easy LDR with Uno.
// super simple LDR - Uno sketch. Compare LDR value to threshold and light pin 13 LED
// on light and send serial message. Turn off LED in dard and send message
// by C. Goulding aka groundFungus
const byte ldrPin = A0;
const byte ledPin = 13;
unsigned int ldrTheshold = 500;
void setup()
{
Serial.begin(115200);
pinMode(ldrPin, INPUT_PULLUP); // turn on internal pullup to make
// required voltage divider for LDR
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW); //turn off LED
}
void loop()
{
static unsigned long ldrTimer = 0;
unsigned long ldrInterval = 500;
if (millis() - ldrTimer >= ldrInterval)
{
ldrTimer = millis();
unsigned int ldrValue = analogRead(ldrPin);
Serial.print("current light value = ");
Serial.print(ldrValue);
if (ldrValue < ldrTheshold)
{
digitalWrite(ledPin, HIGH); // turn on LED
Serial.println(" there is light");
}
else
{
digitalWrite(ledPin, LOW); // turn off LED
Serial.println(" it is dark");
}
}
}