ECE 110/Lab 5
Jump to navigation
Jump to search
This page contains supplemental information for Laboratory V: Light-Controlled Tone Generator for ECE 110.
PhotoresistorLightSensor
/*
* Adapted from Robotics with the BOE Shield - LeftLightSensor
*
* Measures RC decay time for photoresistor circuit
* Assumes 1 uF capacitor
*/
void setup() // Built-in initialization block
{
Serial.begin(9600); // Set data rate to 9600 bps
}
void loop() // Main loop auto-repeats
{
long decayTime = rcTime(8); // Uses rcTime function to calculate delay
Serial.print("Decay time = "); // Display time label
Serial.print(decayTime); // Display time value
Serial.print(" us "); // Display time units
Serial.print("RP = "); // Display resistor label
Serial.println(-decayTime/log(0.42)); // Note: uF capacitor and time in us cancel!
delay(10); // 10 ms delay
}
// rcTime function at pin
long rcTime(int pin) // ..returns decay time
{
pinMode(pin, OUTPUT); // Charge capacitor
digitalWrite(pin, HIGH); // ..by setting pin ouput-high
delay(10); // ..for 10 ms
pinMode(pin, INPUT); // Set pin to input
digitalWrite(pin, LOW); // ..with no pullup
long time = micros(); // Mark the time
while(digitalRead(pin)); // Wait for voltage < threshold
time = micros() - time; // Calculate decay time
pinMode(pin, OUTPUT); // Discharge capacitor
digitalWrite(pin, LOW); // ...by setting pin output-low
return time; // Return decay time
}