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() {
}