Skip to content

Instantly share code, notes, and snippets.

@dpr0
Created February 20, 2018 09:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpr0/e978cf46d8ad896a132c4ede20509d2d to your computer and use it in GitHub Desktop.
Save dpr0/e978cf46d8ad896a132c4ede20509d2d to your computer and use it in GitHub Desktop.
pinMode(A5, 'analog');
var co2_values = [], index = 0, co2_value = 0, co2_min = 1000, co2_max = 0;
const VALUES_SIZE = 150;
const RZERO = 83.54781175;
const PARA = 116.6020682;
const PARB = 2.769034857;
const CORA = 0.00035;
const CORB = 0.02718;
const CORC = 1.39538;
const CORD = 0.0018;
const ATMOCO2 = 397.13;
function getRZero() {
return getResistance() * Math.pow((ATMOCO2/PARA), (1.0/PARB));
}
function getResistance() {
return (1.515151515151515 / analogRead(A5)) - 1;
}
function getPPM() {
return Math.round(PARA * Math.pow((getResistance() / RZERO), - PARB));
}
function getCorrectedResistance(t, h) {
return getResistance()/getCorrectionFactor(t, h);
}
function getCorrectionFactor(t, h) {
return CORA * t * t - CORB * t + CORC - (h - 33.0) * CORD;
}
function getCorrectedPPM(t, h) {
return PARA * Math.pow((getCorrectedResistance(t, h)/RZERO), - PARB);
}
setInterval(() => {
let current_co2 = getPPM();
co2_values[index] = current_co2;
if(current_co2 < co2_min) co2_min = current_co2;
if(current_co2 > co2_max) co2_max = current_co2;
co2_value = (co2_values.reduce((a, b) => { return a+b; }, 0) / co2_values.length).toFixed();
if(index >= VALUES_SIZE-1) index = 0;
else index++;
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment