Assignment 1

Circuit: The circuit in two modes i.e. photoresistor and thermistor mode. The wires can be interchanged or the pins can be changed in the code to alter the input sensor. After calibration, I use a library(https://playground.arduino.cc/Main/QuickStats) to find the median which assigns the value to the output pin via analogWrite. The library also gives access to other stats.

CODE: https://drive.google.com/open?id=1tP_MSYRrJdPCSbaLjyjgEVYBWCxyLu0I

/* basic stats with analogRead()
*
* intputs
* A0 photoresitor / A3 thermistor
*
* output
* D13 LED (Debugging)
* A9 LED (Output)
* Serial stats output
*
* Auto caliberates in the first 5 seconds (press/cover the sensor)
*
*/
// using library from https://playground.arduino.cc/Main/QuickStats
#include "QuickStats.h"
int NUMSAMPLES=10; // number of samples to take for data smoothing
float measurements[10];
const int analogInPin = A0; // Analog input A0 for photoresistor and A1 for thermistor
const int analogOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)

QuickStats stats; //initialize an instance of this class

// Min and Max sensor values

int sensorMin = 1023; // minimum sensor value
int sensorMax = 0; // maximum sensor value
float sensorMinF = 0.1;
float sensorMaxF = 0.0;
float smoothed = 0.0;

void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
caliberate();

}

// concept borrowed from https://www.arduino.cc/en/Tutorial/Calibration
void caliberate()
{
Serial.println("Caliberating ");
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)

while (millis() < 5000) { sensorValue = analogRead(analogInPin); // record the maximum sensor value if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}

// record the minimum sensor value
if (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
}

sensorMin-= 10;
sensorMax+=10;

// volts * 10 for the thermistor to work

sensorMinF = (50.0*(float)sensorMin/1023.0);
sensorMaxF = (50.0*(float)sensorMax/1023.0);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW

}

void printStats()
{
Serial.println("Descriptive Statistics");
Serial.print("Mean : ");
Serial.println(stats.average(measurements,NUMSAMPLES));
Serial.print("Minimum: ");
Serial.println(stats.minimum(measurements,NUMSAMPLES));
Serial.print("Maximum: ");
Serial.println(stats.maximum(measurements,NUMSAMPLES));
Serial.print("Standard Deviation: ");
Serial.println(stats.stdev(measurements,NUMSAMPLES));
Serial.print("Standard Error: ");
Serial.println(stats.stderror(measurements,NUMSAMPLES));
Serial.print("Coefficient of Variation (%): ");
Serial.println(stats.CV(measurements,NUMSAMPLES));
Serial.print("Median: ");
Serial.println(stats.median(measurements,NUMSAMPLES));
Serial.print("Mode: ");
Serial.println(stats.mode(measurements,NUMSAMPLES,0.00001));
}

void loop() {

for(int i=0;i<NUMSAMPLES;i++){
sensorValue = analogRead(analogInPin);
measurements[i]=(5.0*(float)sensorValue/1023.0); // convert to volts
delay(5); // Change (or remove) this delay value to alter the sampling time span.
}
smoothed=stats.median(measurements,NUMSAMPLES); // Median filter (choose which filter to use)

// caliberating for thermistor (low values)
outputValue = map(smoothed*10, sensorMinF, sensorMaxF, 0, 255);
outputValue = constrain(outputValue, 0, 255);

Serial.println(smoothed,3); // Print smoothed value to serial monitor
Serial.print("min: ");
Serial.print(sensorMinF); // Print min value to serial monitor
Serial.print(", max: ");
Serial.print(sensorMaxF); // Print max value to serial monitor
Serial.print(", ");
Serial.println(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);

analogWrite(analogOutPin, outputValue);
printStats();
delay(250); // Change (or remove) this delay to alter the time between readings.
}

Link to picture: https://docs.google.com/document/d/1W2unaggIEvyaNUqzIg3yzXc4pTtWhLolyvhvZdAdMLc/edit

Circuit diagram borrowed from: https://learn.adafruit.com/experimenters-guide-for-metro/circ09-wiring 

Leave a Reply

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