I am trying to average the values I am reading from an LDR! The values are in-consistent, and range from 60 +/- 20. I have a 10K on the data pin (with my limited knowledge I am assuming it is acting as a pull-up resistor). I want to read the value of the pin ten times, and then average it (divide by ten). I am applying quite a bit of force to the top to ensure it is completely covered and it is still all over the place. Below I have pasted my code. Thank you in advance for any shared knowledge!
int ldr_Pin = 8; // Pin in which the LDR is connected to.
int ldr_Value = 0; // Variable that will be used to store the value of the LDR.
void setup() {
Serial.begin(9600);
}
void loop() {
ldr_Value = analogRead(ldr_Pin); // Reads the value of the LDR connected to pin 8.
Serial.println(ldr_Value); // Begins printing the value stored in the variable, ldr_Value, to the serial monitor.
delay(50);
}
I want to read the value of the pin ten times, and then average it (divide by ten).
Please feel free to read the value in a for loop counting from 0 to 9, adding the reading to the total each time you read it, then when the for loop ends divide the total by 10
Here's a "rolling average" LDR sketch, I used a 68k divider resistor on pin A0, reads 0 in total darkness and around 1008 in full daylight:
uint32_t tStart, // timer start
tEnd = 2000; // update time in mS
int total, // sum of samples
current,
val;
const byte sampleBin = 8, // number of samples for smoothing
aInPin = A0;
void setup()
{
Serial.begin(9600);
analogRead(aInPin);
for(int i = 0;i < sampleBin;i++) // for smoothing, fill total
total += analogRead(aInPin); // with sampleBin * current
// reading
}
void loop()
{
if(millis() - tStart > tEnd)
{
tStart = millis(); // reset timer
total -= (total / sampleBin); // make room for new reading
current = analogRead(aInPin);
Serial.print(current); Serial.print("\t");
total += current; // add new reading
val = total / sampleBin;
Serial.println(val);
}
}
Am I to understand that I should use the for control structure element? I did some research but am having trouble understanding how to move forward? Here is my updated code:
/*
* This is a simple program that will read the value of an LDR
* ten times, and then it will average the ten values.
*
* Configuration is as follows:
*
* LDR connected to pin 8 with a 10K resistor on the
* data pin. Obviously, the other lead on the LDR is
* connected to GND.
*/
void setup() {
int ldr_Pin = 8; // Pin in which the LDR is connected to.
int ldr_Value = 0; // Variable that will be used to store the value of the LDR.
int ldraverage_Calculation; //The variable that will be used to perform the averaging.
Serial.begin(9600); // Begins serial connection at 9600 baud.
pinMode(8, INPUT); // Sets pin 8 to be an input pin. Forgot to do this the first time around.
}
void loop() {
ldr_Value = analogRead(ldr_Pin); // Reads the value of the LDR connected to pin 8.
Serial.println(ldr_Value); // Begins printing the value stored in the variable, ldr_Value, to the serial monitor.
delay(50); // Delays the program 50 milli-seconds to allow the operator time to read the values (otherwise, it would be difficult to read because it would be printing values more quickly than could be read).
for (int ldr_Value; ldraverage_Calculation == 0; increment) {
//statement(s);
}
}