     // take average of the last `readperiod` ms of readings and compare to current readings up until 1800, at which point average
 
 
-    if ((millis() % readperiod)==0) {
+    if ((millis()-lastread) > readperiod) {
         // if it is time to average the values, then average the values
         baseline=currentavg;
         currentavg=analogvalue;
+        lastread=millis();
     }
     else {
         // otherwise, update your average log


int r=1;

<<<<<<< Updated upstream
int lastrecord=0;
int recthreshold=5000; // record on sd card every 5 seconds

// if photon:
// SYSTEM_MODE(SEMI_AUTOMATIC);
=======
SYSTEM_MODE(SEMI_AUTOMATIC);
>>>>>>> Stashed changes


void setup() {


    int q=M*diff+B;

//    Serial commands for testing
//    Serial.print(M);
//    Serial.print("   ");
//    Serial.print(B);
//    Serial.print("   ");
//    Serial.print(diff);
//    Serial.print("   ");
//    Serial.print(q);
//    Serial.print("   ");
//    Serial.println(Q);
//*/

    lastdiff=diff;
