Reading values, Storing them and divide them

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.

Add another line to recalculate average after you add readings to sum.

You should fix that line, too, to explicitly set average to 0, rather than implicitly setting it to 0.

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");
}

Good news that you got it working.

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 project contains multiple stages,

first i got the mq-7 sensor working, after that i made a baseline.

the next step is to make a separate counter which counts to a certain amount. If the measured values are to far off the baseline.

if the counter hits its max it will make a new baseline.

When i got that working i will link it to a sms module which will send me a sms at a certain value.

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?

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

What does "a new baseline" mean to you? Does that mean just that 20 values have been collected? What should happen with the 21st value?

does anyone have a treat or video where is explained

Got leftover Halloween candy in a bowl on my desk. Help yourself.

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

In which case you will need need to eliminate all delay()s from the code and use millis() for timing

I will look into that thanks for noticing!

ArduinoBot2000:
I will look into that thanks for noticing!

Have a look at Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

Im trying to make a separate counter now and i have a question about that.

I want to make the timer in a way that it goes up when its 3 higher of lower then the created baseline.

Is is possible to say that in a if statement like:

int counter = 0;

if ("measured value" is higher or lower then 3 of the baseline) {

counter = counter + 1;

}

Is is possible to say that in a if statement like:

Yes. You just need to turn express it with the correct syntax then turn it into programming code

if ((measured value < baseline - 3)  or (measured value > baseline + 3))

or neater

if (abs(measured value - baseline) > 3)

thanks making good progress now i came up with this:

int counter = 0;

  if (sensorValue < average - 3 && sensorValue > average + 3) {
    
    counter + 1;
    delay(1000);
    
  } else if (counter < 1){
    
    counter = 0;
  } else {

    counter - 1;
    delay(1000);
  }
    Serial.println(counter);
}

the Serial.println only gives back 0.

even if its +3 or -3 from the baseline.

what did i do wrong here?

what did i do wrong here?

You did not post a complete program but when you do
int counter = 0;sets the counter back to zero

    counter - 1;This too is rubbish.

Please post a complete program

int sensor = 0;
int sensorValuesArray[20];
int average;

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]);

int counter = 0;

  if (sensorValue < average - 3 && sensorValue > average + 3) {
    
    counter + 1;
    delay(1000);
    
  } else if (counter < 1){
    
    counter = 0;
  } else {

    counter - 1;
    delay(1000);
  }
    Serial.println(counter);
}
 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

Note my comments

At what values of sensorValue and average will sensorValue be less than average - 3 and at the same time greater than average + 3?

you are right i've changed it with: ||

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.