/* Ivan Wong * 6/1/23 * Don Heer * 24 Logging Final (Light Sensor) */ const int numReadings = 600; // Amount of readings needed for 10 minutes const int sensorPin = 32; const float luxConversionFactor = 0.2; // Conversion factor from analog value to lux int readings[numReadings]; int readIndex = 0; unsigned int total = 0; int average = 0; int counter = 0; // Tracks how many readings happened unsigned long previousMillis = 0; const unsigned long second = 1000; // 1 second has 1000 milliseconds void setup() { Serial.begin(115200); pinMode(sensorPin, INPUT); // This allows for 600 readings, then resets the array back to 0 for(int i = 0; i < numReadings; i++) { readings[i] = 0; } } void loop() { // Current time unsigned long currentMillis = millis(); // previousMillis gets a 1000 added each second if((currentMillis - previousMillis) >= second) { previousMillis = currentMillis; unsigned int sensorVal = analogRead(sensorPin); // readings array gets the sensor value readings[readIndex] = sensorVal; // Calculates the total of the values total = total + readings[readIndex]; // readings arrray gets incremented by 1 each reading and the % prevents it from going over 600 readIndex = (readIndex + 1) % numReadings; // Increments counter by 1 for each reading counter++; // Once there is 600 readings, we get the raw average if(counter >= numReadings) { average = total / numReadings; float luxAvg = average * luxConversionFactor; Serial.println(luxAvg); // Resets everything back to 0 so we can read the next interval counter = 0; total = 0; } } }