Difference between revisions of "ECE 110/Lab 5"

From PrattWiki
Jump to navigation Jump to search
 
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
This page contains supplemental information for Laboratory V: Light-Controlled Tone Generator for ECE 110.
+
This page contains supplemental information for [[ECE 110]] Laboratory V: Light-Controlled Tone Generator.
  
 
== PhotoresistorLightSensor ==
 
== PhotoresistorLightSensor ==
Line 7: Line 7:
 
  *  
 
  *  
 
  * Measures RC decay time for photoresistor circuit
 
  * Measures RC decay time for photoresistor circuit
* Assumes 1 uF capacitor
 
 
  */
 
  */
  
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.print(" us   ");                    // Display time units
+
   Serial.println(" 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
 
   delay(10);                                // 10 ms delay

Latest revision as of 17:54, 20 February 2014

This page contains supplemental information for ECE 110 Laboratory V: Light-Controlled Tone Generator.

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
}