Creating an Average Value

Hello,

I am working on a project at the minute. And i want to use an electric generator a value. I attached an electric motor (witch i am using as a generator) to the A1 pin. I want to get an average voltage or number over a time width of around 10 seconds. and give me an average.

My code is here below i am using a button with my project but that is not really necessary to explaine.

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
            int buttonState = digitalRead(pushButton);  //create buttonstate and read
              Serial.println("Button off state set"); //display text
                  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
                delay(2000);  //delay 500ms               
  /////////////////////////////////////////////////////////////////////////////////////////////////////Buttonstatesset.
  
  int TemporaryButtonState = digitalRead(pushButton); //add button state temp
    Serial.println("turn on water to max");// display text
      Serial.println("Press the Button to continue");
          while (buttonState == TemporaryButtonState) {
              buttonState = digitalRead(pushButton); // refresh buttonstate 
                delay(500); // delay 1000ms
          }
            int MaxRPM = analogRead(CurrentRPM);
            int TempRPM = analogRead(CurrentRPM);
delay(500);
            for (int i=0; i <= 500; i++){
            TempRPM = analogRead(CurrentRPM);
            Serial.println(MaxRPM);
            Serial.println(TempRPM);
                if (MaxRPM < TempRPM) {
                  MaxRPM = analogRead(CurrentRPM);
                  MaxRPM = TempRPM;
                  delay(100);
               }
           delay(250);
            }     
  Serial.println("Setup Finished");  // display end text
  Serial.println(MaxRPM);
}
void loop() {
}

Rather than us trying to guess if your program does what you want it to, why not tell us what you need help with?

I can’t see any attempt to average anything in there but perhaps I just missed it.

Steve

You want to do something like this (I have not tried compiling or running this):

const byte pinCurrentRPM = A1;
float currentRPM;
unsigned long sum = 0;
const int noOfReadings = 100;
for (int i = 0; i < noOfReadings ; i++) {
  sum += analogRead(pinCurrentRPM);
}
currentRPM = (float)sum / noOfReadings;

Take multiple readings and add them all up. Then divide by the number of readings. I don't know what kind of numbers you'll be using/needing - the above will return a float rather than an integer).

In the example I renamed your CurrentRPM variable to pinCurrentRPM since otherwise it looks like it holds the current RPM instead of a pin number.

            for (int i=0; i <= 500; i++){
            TempRPM = analogRead(CurrentRPM);
            Serial.println(MaxRPM);
            Serial.println(TempRPM);
                if (MaxRPM < TempRPM) {
                  MaxRPM = analogRead(CurrentRPM);
                  MaxRPM = TempRPM;
                  delay(100);
               }

What are you trying to do here? It looks odd, especially doing another analogRead after checking a condition against the previous analogRead (which may well retuirn a different value)