// These constants won't change. They're used to give names // to the pins used: const int analogInPin = 1; // Analog input pin that the potentiometer is attached to const int analogOutPin = 13; // Analog output pin tha2b357d16t the LED is attached to int sensorValue = 0; // value read from the pot int outputValue = 0; // value output to the PWM (analog out) int i = 0; int sample = 30; float rawArray[30]; int raw; float averageRaw; float sumRaw; //replace with AREF voltage? float referenceVoltage; #define CHAR_ESC '\x1B' void setup() { Serial.begin(57600); //analogReference(EXTERNAL); referenceVoltage = 5; //referenceVoltage = 4.79; } void loop() { // read the analog in value: sensorValue = analogRead(analogInPin); // map it to the range of the analog out: outputValue = map(sensorValue, 0, 1023, 0, 255); // change the analog out value: analogWrite(analogOutPin, outputValue); float raw = analogRead(analogInPin); //Calculation 1 = Average over 30 loops rawArray[i] = raw; for (int i = 0; i < 30; i++) sumRaw = sumRaw + rawArray[i]; averageRaw = sumRaw/sample; //Arduino analogReads in a value 0-1024 float voltDivisor = 1024/referenceVoltage; float volts = averageRaw/voltDivisor; float kelvin = (volts * 1000)/10; float celcius = kelvin - 273.15; Serial.print( CHAR_ESC ) ; Serial.print( 'E' ) ; // clear screen Serial.print("averaged="); Serial.print(celcius); Serial.print(" C"); // voltage divisor = 1024/5v = 204.8 volts = raw/voltDivisor; kelvin = (volts * 1000)/10; // convert to kelvin: [°C] = [K] − 273.15 celcius = kelvin - 273.15; Serial.print(" raw="); Serial.print(celcius); Serial.print(" C"); Serial.println(""); Serial.print("kelvin="); Serial.print(kelvin); Serial.print(" K"); Serial.println(""); Serial.print("voltageDivisor="); Serial.print(voltDivisor); Serial.print(" volts="); Serial.print(volts); Serial.println(""); Serial.print("referenceVoltage="); Serial.print(referenceVoltage); i++; if (i >= sample) { i = 0; } sumRaw = 0; // wait n milliseconds before the next loop // for the analog-to-digital converter to settle // after the last reading: int n = 200; delay(n); }