Assignment 6 – James Kyle

Daylight Level Indicator:

For this assignment, I have made a theoretical daylight level sensor. The inspiration for this comes from my lab being in the basement of Ansys. We have no windows down there so I can easily get sucked into work and stay there for hours without realizing what time of day it is and/or whether it is time to pack up and call it a day. This daylight level indicator is meant to solve this problem by sensing the amount of daylight coming from outside and physically turning a dial to show the time of day. Theoretically, this dial would take the shape of a circle with night and day images on either side. The servo would turn to the appropriate position to indicate how close the sun is to setting or rising depending on the level of light outside and the time of day.

#include <Servo.h>


int hours = 0;

const int measurementNumber = 20;
int measurementCount = 0;
int lightMeasurements[measurementNumber];
const int lightLevel = A0; //Light level pin (photoresistor pin)

int lightBottomBound = 0;
int lightTopBound = 0;


Servo myServo;
int SERVOPIN = 9;



void setup() {

  pinMode(lightLevel, INPUT);

  myServo.attach(9);

}

void loop() {

  //Update measurement counter
  if (measurementCount < measurementNumber) {
    measurementCount += 1;
  } else {
    measurementCount = 0;
  }


  //Update light level average
  int lightLevelSum = 0;
  for (int i = 1; i < measurementNumber; i++) {
    lightLevelSum += lightMeasurements[i];
  }

  int lightLevelAverage = lightLevelSum / measurementNumber;



  //Update light level measurements
  int currentLightLevel = analogRead(lightLevel);
  lightMeasurements[measurementCount] = currentLightLevel;




  //Change bounds if light level is outside of bounds
  if (lightLevelAverage < lightBottomBound) {
    lightBottomBound = lightLevelAverage;
  }

  if (lightLevelAverage > lightTopBound) {
    lightTopBound = lightLevelAverage;
  }


  //Write to servo
  int servoAngle = map(lightLevelAverage, lightBottomBound, lightTopBound, 0, 180);
  myServo.write(servoAngle);


  delay(1000);

}

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.