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

Hello,

I’m working on a project involving reading a photoresistor and everything is great, except for one thing I want to add:

This project has a baseline reading, and then preforms an action if this reading drops to a certain level. However, the lighting in my room can be different from time to time, so if I have a set baseline value in my code, it would be useless later. So, I thought that in void setup() I could read the value 7 times and find the average, and use that value as the baseline. Then, at a different time of day, I can reset the Arduino if I want a newer baseline. Here is my code, minus the baseline thing -

``````#include <Servo.h>
Servo blocker;

int entranceSensor = A0;

int baselineVal;

void setup() {

baselineVal = 0; // read Sensor 7 times and find average

Serial.begin(9600);
Serial.print("************Baseline Value:");
Serial.print(baselineVal);
Serial.println("************");

blocker.attach(5);

}

void loop() {

if (sensorVal <= baselineVal - 100) {

blocker.write(160);

}

else {

blocker.write(0);

}

Serial.println(sensorVal);

}
``````

Any guidance on how to do this would be appreciated. Thanks!

Marco

I assume you know how to take an average? Take 7 readings, add 'em up and divide by 7.

In case you don't know this, here's something you can do in programming that doesn't make sense in a math equation:

Initialize Sum to zero before you start and then make a loop (probably a for-loop) that loops 7 times.

FYI - In programming lingo, that's not a constant. A constant is a value that's set at compile-time and your program can't change it.

A constant is a value that's set at compile-time

.... unless it's a const class member, in which case it is initialised at run time by the constructor.

DVDdoug: I assume you know how to take an average? Take 7 readings, add 'em up and divide by 7.

In case you don't know this, here's something you can do in programming that doesn't make sense in a math equation:

Initialize Sum to zero before you start and then make a loop (probably a for-loop) that loops 7 times.

Yes, I know how to find an avg. That makes sense.

DVDdoug: FYI - In programming lingo, that's not a constant. A constant is a value that's set at compile-time and your program can't change it.

Ah okay, oops.

When I try this:

``````#include <Servo.h>
Servo blocker;

int entranceSensor = A0;

int baselineVal;

void setup() {

for (int 1=0; i <= 7; i++) {

}

baselineVal = baselineVal / 7;

Serial.begin(9600);
Serial.print("************Baseline Value:");
Serial.print(baselineVal);
Serial.println("************");

blocker.attach(5);

}

void loop() {

if (sensorVal <= baselineVal - 100) {

blocker.write(160);

}

else {

blocker.write(0);

}

Serial.println(sensorVal);

}
``````

I get this:

``````Arduino: 1.6.8 (Mac OS X), Board: "Arduino/Genuino Uno"

Build options changed, rebuilding all
/var/folders/4w/b3f5dh255ys1f54r9f6089zr0000gq/T/arduino_modified_sketch_221464/sketch_jun28c.ino: In function 'void setup()':
sketch_jun28c:10: error: expected unqualified-id before numeric constant
for (int 1=0; i <= 7; i++) {
^
sketch_jun28c:10: error: expected ';' before numeric constant
sketch_jun28c:10: error: lvalue required as left operand of assignment
for (int 1=0; i <= 7; i++) {
^
sketch_jun28c:10: error: 'i' was not declared in this scope
for (int 1=0; i <= 7; i++) {
^
sketch_jun28c:10: error: expected ')' before ';' token
for (int 1=0; i <= 7; i++) {
^
sketch_jun28c:10: error: 'i' was not declared in this scope
for (int 1=0; i <= 7; i++) {
^
sketch_jun28c:10: error: expected ';' before ')' token
for (int 1=0; i <= 7; i++) {
^
exit status 1
expected unqualified-id before numeric constant

"Show verbose output during compilation"
option enabled in File -> Preferences.
``````
``````for (int 1=0
``````

You can't assign zero to one.

well that makes sense. that should have been an ‘i’

I have a similar situation where I want to detect a sudden noise level if it goes much above an ambient level in a room. But the ambient level may change, say if truck goes by, or a refrigerator turns on.

So at program startup, I take twenty readings at 0.5 second intervals, getting an ambient noise level averaged over ten seconds. But when the code goes into the loop mode, I keep taking noise level readings, and use a moving window average (i.e., the last ten seconds of values). This works very nicely in my application to provide me with a slowly varying ambient level, and I only trigger a noise alert if I get an instantaneous reading 10% (in terms of mic voltage) above the ambient.

You might be able to adapt this to your situation by experimenting with length of average, excursion needed to trigger alert, etc.