Difference between revisions of "ECE 110/Lab 5"

From PrattWiki
Jump to navigation Jump to search
 
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 ==

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
}