Difference between revisions of "ECE 110/Lab 5"
Jump to navigation
Jump to search
Line 7: | Line 7: | ||
* | * | ||
* Measures RC decay time for photoresistor circuit | * Measures RC decay time for photoresistor circuit | ||
− | |||
*/ | */ | ||
Line 21: | Line 20: | ||
Serial.print("Decay time = "); // Display time label | Serial.print("Decay time = "); // Display time label | ||
Serial.print(decayTime); // Display time value | Serial.print(decayTime); // Display time value | ||
− | Serial. | + | Serial.println(" us"); // Display time units |
− | |||
− | |||
delay(10); // 10 ms delay | delay(10); // 10 ms delay |
Revision as of 23:20, 10 February 2014
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
*/
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.println(" us"); // Display time units
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
}