Yingfan Chen
Published

Light Detector Test

Ever wonder how bright the room is? Can you really believe in your eyes? Light Detector Test, bringing truth to the hellish facade of life

BeginnerWork in progress29
Light Detector Test

Things used in this project

Hardware components

Jumper wires (generic)
Jumper wires (generic)
×13
LED (generic)
LED (generic)
×1
7 Segment LED Display, InfoVue
7 Segment LED Display, InfoVue
×1
Resistor 330 ohm
Resistor 330 ohm
×1
Resistor 10k ohm
Resistor 10k ohm
×1
Photo resistor
Photo resistor
×1
EK-TM4C123GXL TM4C Tiva LaunchPad
Texas Instruments EK-TM4C123GXL TM4C Tiva LaunchPad
×1
Breadboard (generic)
Breadboard (generic)
×1
tilt switch
×1

Software apps and online services

Energia
Texas Instruments Energia

Story

Read more

Schematics

Circuit 1

Circuit 2

Hook circuit 1 and the circuit 2 in parallel

Code

Light Detector Test

C/C++
1. Detects the intensity of light
2. Display the light intensity level on the 7-segment display (0 - darkest, 9 - brightest)
3. Lights up the LED (brighter if dark, darker if bright)
/*
 * The file reads the light intensity, lights up the LED according to the brightness of the room, and shows the level of light intensity from 0-9 on the 7-segment display
 */

#define LEVEL0 25
#define LEVEL1 50
#define LEVEL2 75
#define LEVEL3 100
#define LEVEL4 125
#define LEVEL5 150
#define LEVEL6 175
#define LEVEL7 200
#define LEVEL8 225
#define LEVEL9 255


// Define the LED digit patterns for 0 - 9 in a 2 dimensional array.
// The 2D array (an array of arrays or a matrix) has 10 arrays that each
// contain 7 values.
// Note that these patterns are for common cathode displays. For common
// anode displays, change the 1's to 0's and 0's to 1's
// 1 = LED on, 0 = LED off, in this order:
// Common Anode version
byte seven_segment_digits[10][7] = { { 0,0,0,0,0,0,1 }, // display '0'
                                     { 1,0,0,1,1,1,1 }, // display '1'
                                     { 0,0,1,0,0,1,0 }, // display '2'
                                     { 0,0,0,0,1,1,0 }, // display '3'
                                     { 1,0,0,1,1,0,0 }, // display '4'
                                     { 0,1,0,0,1,0,0 }, // display '5'
                                     { 0,1,0,0,0,0,0 }, // display '6'
                                     { 0,0,0,1,1,1,1 }, // display '7'
                                     { 0,0,0,0,0,0,0 }, // display '8'
                                     { 0,0,0,1,1,0,0 }  // display '9'
                                   };

/* 
 * Identify level of the light and display
 * the number
 */
void displayLight(int lightlevel) {
  if (lightlevel <= LEVEL0)
    displayNumber(0);
  else if (lightlevel <= LEVEL1)
    displayNumber(1);
  else if (lightlevel <= LEVEL2)
    displayNumber(2);
  else if (lightlevel <= LEVEL3)
    displayNumber(3);
  else if (lightlevel <= LEVEL4)
    displayNumber(4);
  else if (lightlevel <= LEVEL5)
    displayNumber(5);
  else if (lightlevel <= LEVEL6)
    displayNumber(6);
  else if (lightlevel <= LEVEL7)
    displayNumber(7);
  else if (lightlevel <= LEVEL8)
    displayNumber(8);
  else if (lightlevel <= LEVEL9)
    displayNumber(9);
}

/*
 * Displays the number on the 7-segment display
 */
void displayNumber(int num)
{
    int pin = 3;
    for (int segCount = 0; segCount < 7; ++segCount) {
      digitalWrite(pin, seven_segment_digits[num][segCount]);
      ++pin;
    }
    delay(1000); // this is the same as delay() but saves power
}


/*
 *In the setup function, we set our LED pins as OUTPUT.
 */
void setup() {
  Serial.begin(9600); // initialize serial communication
  pinMode(19, OUTPUT); // sets the LED on pin 19 to output
  pinMode(3, OUTPUT); // set segment A as output
  pinMode(4, OUTPUT); // set segment B as output
  pinMode(5, OUTPUT); // set segment C as output
  pinMode(6, OUTPUT); // set segment D as output
  pinMode(7, OUTPUT); // set segment E as output
  pinMode(8, OUTPUT); // set segment F as output
  pinMode(9, OUTPUT); // set segment G as output
  pinMode(10, OUTPUT); // set dot as output
  for(int i = 3; i < 10; i++) { // start with segments off
    digitalWrite(i, HIGH);
  }
  digitalWrite(10, HIGH);  // start with the dot off
}

void loop() {
  int lightLevel = analogRead(2); // Read the light level from analog pin 2
  delay(1); // delay for 1 millisecond for smoothness
  Serial.println(lightLevel); // Print the analog value to Serial
  // adjust the value 0 to max resolution to span 0 to 255
  lightLevel = map(lightLevel, 0, 4096, 0, 255);
  lightLevel = constrain(lightLevel, 0, 255); // constrain values between 0-255
  Serial.println(lightLevel); // Print the digital value to Serial
  analogWrite(19, lightLevel); // Write the PWM value to the LED
  displayLight(lightLevel); // Displays the number on 7-segment display according to the light level
  delay(1000); // this is the same as delay() but saves power
}

Credits

Yingfan Chen

Yingfan Chen

0 projects • 0 followers

Comments

Add projectSign up / Login