-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLDR.h
33 lines (28 loc) · 891 Bytes
/
LDR.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// photo resitor setup
// #define LDR_PIN 32
void ldrsetup() {
pinMode(LDR_PIN, INPUT);
delay(100); // Small delay to stabilize readings
int testValue = analogRead(LDR_PIN);
if (testValue == -1) { // -1 means an error in most cases
Serial.println("❌ LDR sensor error: No response!");
} else {
Serial.println("✅ LDR sensor is working!");
}
}
// Get raw light value
float getLightLevel() {
float lightLevel = analogRead(LDR_PIN);
Serial.print("💡 Light Level: ");
Serial.println(lightLevel);
return lightLevel;
}
// Get light percentage (0-100%)
float getLightPercentage() {
float rawValue = analogRead(LDR_PIN);
float percentage =100 -( (rawValue / 4095.0) * 100); // Convert to percentage
Serial.print("🔆 Light Percentage: ");
Serial.print(percentage);
Serial.println("%");
return percentage;
}