Reading a pin Multiple times and storing the average value to a constant


hello,

i'm working on project involving reading photoresistor , great, except 1 thing want add:

this project has baseline reading, , preforms action if reading drops level. however, lighting in room can different time time, if have set baseline value in code, useless later. so, thought in void setup() read value 7 times , find average, , use value baseline. then, @ different time of day, can reset arduino if want newer baseline. here code, minus baseline thing -

code: [select]
#include <servo.h>
servo blocker;

int entrancesensor = a0;

int baselineval;

void setup() {

  baselineval = 0; // read sensor 7 times , find average
 
  serial.begin(9600);
  serial.print("************baseline value:");
  serial.print(baselineval);
  serial.println("************");

  blocker.attach(5);
 
}

void loop() {

  int sensorval = analogread(entrancesensor);

  if (sensorval <= baselineval - 100) {

    blocker.write(160);
   
  }

  else {

    blocker.write(0);
   
  }

  serial.println(sensorval);
 
}


any guidance on how appreciated. thanks!

marco

i assume know how take average?    take 7 readings, add 'em , divide 7.



in case don't know this, here's can in programming doesn't make sense in math equation:

sum = sum + analog.read(entrancesensor);

initialize sum 0 before start , make loop (probably for-loop) loops 7 times.




fyi - in programming lingo, that's not constant.     a constant value that's set @ compile-time , program can't change it.   


Arduino Forum > Using Arduino > Project Guidance > Reading a pin Multiple times and storing the average value to a constant


arduino

Comments

Popular posts from this blog

Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode - Raspberry Pi Forums

class MPU6050 has no member named begin

missing filename after '-o'