Creating a average with analogread

Hello,

I am working a on a project. I am trying to get a generator to spin for a certain amount of time. I want to use that data to make a average that i can use. but i am not sure on how i am suppost to code that.

Short Question: How can i create a average for an analogread.

Here is my code

int pushButton = A0;  // Button Pin
int CurrentRPM = A1;  //create max rpm reading from pin A0 while analog reading
void setup() {
  Serial.begin(9600); //serial monitor setup
    Serial.println("Please do not touch the device.");  //dislay text
      delay(2000);  //delay 2000ms
        pinMode(pushButton, INPUT); //pushbutton pinmode set to input
          int buttonoff = digitalRead(pushButton);  // create button off state and read
            Serial.println("Button off state set"); //display text
              int buttonState = digitalRead(pushButton);  //create buttonstate and read
                Serial.println("Current buttonstate set"); // display text
                  Serial.println("Change the state of the button"); // display text

  
                  
  while(buttonoff == buttonState){ //if buttonoff state is same as buttonstate then wait for change
      buttonState = digitalRead(pushButton); // refresh buttonstate 
  }
  
  int ButtonOn = digitalRead(pushButton); // create button on state and read
    Serial.println("Current Button Status"); //display text
      Serial.println(ButtonOn); //display text
        Serial.println("On State");  //display text 
          Serial.println(ButtonOn); //display text
            Serial.println("Off State");  //display text
              Serial.println(buttonoff);  //display text
  
  Serial.println("turn on water to max");// display text
      int TempButtonState = digitalRead(pushButton); //add button state temp
        Serial.println("Press the Button to continue");
        
          while (buttonState == TempButtonState) {
              buttonState = digitalRead(pushButton); // refresh buttonstate 
                delay(1000); // delay 1000ms
          }
            int MaxRPM = analogRead(CurrentRPM);
            int TempRPM = analogRead(CurrentRPM);
delay(250);
          
            for (int i=0; i <= 5; i++){
            TempRPM = analogRead(CurrentRPM);
            Serial.println(MaxRPM);
            Serial.println(TempRPM);
                if (MaxRPM < TempRPM) {
                  MaxRPM = analogRead(CurrentRPM);
                  MaxRPM = TempRPM;
               }
           delay(250);
            }

            
  Serial.println("Setup Finished");  // display end text
  Serial.println(MaxRPM);
}
void loop() {

}
int CurrentRPM = A1;  //create max rpm reading from pin A0 while analog reading

That comment has nothing to do with that code.

Why does your code look like it was typed by a drunken monkey? Use Tools + Auto Format to sober that monkey up.

You average the analogRead() values just like any other set of numbers. Add them up, and divide by the number of values.

Your indentation is weird. Use the auto-format in the IDE. It is very good. If it scrambles your code then the code was wrong to begin with.

How do you make a regular average? You add up the readings and divide by the number of readings. Do that in code.

You do have to be careful that the intermediate sum does not exceed the size of the variables. A regulat int cannot store values larger than 32767. If it does, then use long int.

Well done on using the code tags, by the way.

MorganS: You do have to be careful that the intermediate sum does not exceed the size of the variables. A regulat int cannot store values larger than 32767. If it does, then use long int.

You can double that value by using an unsigned int or uint16_t. Those go to 65535. Those can store at least 64 readings.