Have you assumed that this calculates the average for you?
unsigned int sum = 0;
unsigned int average = sum/20;
Folks starting programming sometimes believe that the calculation of average shown means that whatever value sum acquires as the program runs, average will be a twentieth of that. This is not true.
average will be a twentieth of what sum equals when that line executes, sum is zero and so average will be too, as you see.
Add another line to recalculate average after you add readings to sum.
i've fixed it! it works now thanks for all the reactions helped me a lot!
just for people who are interested, i've changed the code like this:
int sensor = 0;
int sensorValuesArray[20];
int counter;
void setup() {
Serial.begin(9600);
}
void loop() {
for (int i = 0; i < 20; i++) {
int sensorValue = analogRead(sensor);
delay(1000);
sensorValuesArray[i] = sensorValue;
Serial.print("sensor value ");
Serial.print(i);
Serial.print(": ");
Serial.println(sensorValuesArray[i]);
}
unsigned int sum = 0;
for (int i = 0; i < 20; i++) {
sum += sensorValuesArray[i];
}
unsigned int average = sum / 20;
Serial.print("Baseline:");
Serial.println(average, DEC);
delay(10000);
Serial.println("End of for loop");
}
You indicated earlier that this was a test for a larger project. In the larger project is the Arduino required to do anything else whilst reading the input a number of times ? If so, then the use of delay() will trip you up.
The strategy of testing parts/stages of the project is a good one but you do need to think ahead.
As currently written there will be a period of 20 seconds whilst the Arduino does nothing else but read the analogue input. Is that acceptable in the context of the program as a whole ?
once on the Arduino will always have power, so once the first baseline is generated there will always be measurements.
I need to make the next code in a way that when its calculating a new baseline the old baseline will still be active till there is a new one generated
does anyone have a treat or video where is explained how to create a counter that goes up/down depending on if the measurements are +/- a x number from the baseline?
int counter = 0; //sets the counter back to zero each time through loop()
if (sensorValue < average - 3 && sensorValue > average + 3)
{
counter + 1; //does nothing to the value of counter
My baseline is created and now im trying again to make the second counter. The second counter counts up if the measured value is +3 or -3 from the baseline.
this is the code where I came up with:
int sensor = 0;
int sensorValuesArray[20];
int average;
int counter = 0;
void setup() {
Serial.begin(9600);
calculateBaseline();
}
void calculateBaseline() {
for (int i = 0; i < 20; i++) {
int sensorValue = analogRead(sensor);
delay(1000);
sensorValuesArray[i] = sensorValue;
Serial.print("sensor value ");
Serial.print(i);
Serial.print(": ");
Serial.println(sensorValuesArray[i]);
}
unsigned int sum = 0;
for (int i = 0; i < 20; i++) {
sum += sensorValuesArray[i];
}
unsigned int average = sum / 20;
Serial.print("Baseline:");
Serial.println(average, DEC);
Serial.println("End of for loop");
delay(5000);
}
void loop() {
int sensorValue = analogRead(sensor);
delay(1000);
sensorValuesArray[20] = sensorValue;
Serial.print("Begin na baseline:");
Serial.println(sensorValuesArray[20]);
if (sensorValue < average - 3 || sensorValue > average + 3) {
counter++;
delay(1000);
} else {
counter--;
delay(1000);
}
Serial.println(counter);
}
the Serialprint gives back the first 20 measured values and creates the baseline. After that new measurements are being gathered. The problem is the counter does not listen to the rule:
if (sensorValue < average - 3 || sensorValue > average + 3)
at least that is what i think. Im having problems solving this issue.
this is what the Serialprint gives back:
sensor value 0: 121
sensor value 1: 121
sensor value 2: 121
sensor value 3: 120
sensor value 4: 120
sensor value 5: 120
sensor value 6: 120
sensor value 7: 120
sensor value 8: 120
sensor value 9: 120
sensor value 10: 120
sensor value 11: 120
sensor value 12: 119
sensor value 13: 119
sensor value 14: 120
sensor value 15: 120
sensor value 16: 120
sensor value 17: 121
sensor value 18: 121
sensor value 19: 121
Baseline:120
End of for loop
Begin na baseline:120
1
Begin na baseline:119
2
Begin na baseline:120
3
Begin na baseline:120
4
Begin na baseline:119
5
Begin na baseline:119
6
Begin na baseline:119
7
Begin na baseline:354
8
Begin na baseline:119
9
Begin na baseline:117
10
etc.
anyone sees what the problem is? and sorry if the answer is already been given then i must have misinterpreted it.