Using IF with smoothing code.

I have a question regarding the following smoothing code which I downloaded from this site and works fine:

Question: How can I use this code:

int userInput=Serial.read();
if (userInput=='1' )

to get this functionality:

  1. User types "1"
  2. Loop runs 10 times, averaging out the values at A0
  3. Serial prints the average of the 10 measured values
  4. Stops until user types in "1" again.

Many Thanks for any help!
Kind regards,
Hanss
zurich

/*

Smoothing

Reads repeatedly from an analog input, calculating a running average
and printing it to the computer. Keeps ten readings in an array and
continually averages them.

The circuit:

  • Analog sensor (potentiometer will do) attached to analog input 0

Created 22 April 2007
By David A. Mellis dam@mellis.org
modified 9 Apr 2012
by Tom Igoe

This example code is in the public domain.

*/

// Define the number of samples to keep track of. The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input. Using a constant rather than a normal variable lets
// use this value to determine the size of the readings array.
const int numReadings = 10;

int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average

int inputPin = A0;

void setup()
{
// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
}

void loop() {
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = analogRead(inputPin);
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;

// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;

// calculate the average:
average = total / numReadings;
// send it to the computer as ASCII digits
Serial.println(average);
delay(1); // delay in between reads for stability
}

Turn the smoothing code into a function that can be called when required then do this

initialise variables
setup input pin and serial port

start of loop
  if serial input is available
    read one byte from serial
    if byte matches '1'
      call the averaging_function
    end of if
  end of if
end of loop

void averaging_function
  code to read analog input 10 times and add to total
  calculate average of readings
  print average
end of function

User types "1"

This will be sent as a byte, or in this case a char, so "if (userInput=='1' )" will work.

Loop runs 10 times, averaging out the values at A0

Easy, use either a FOR loop or WHILE loop.

Serial prints the average of the 10 measured values

Extremely easy, unless your already using your 0 & 1 pins ( TX/RX), in which case you may need to use software serial for that other shield or whatever is using pins 0 & 1. Or get an Arduino Mega.

Stops until user types in "1" again.

A simple latch will do just fine. Look up how to use state machines.

Extremely easy, unless your already using your 0 & 1 pins ( TX/RX), in which case you may need to use software serial for that other shield or whatever is using pins 0 & 1. Or get an Arduino Mega.

Is it likely that one would want to average digital readings? There are analog pins 0 and 1, too. They seem far more likely to be what OP wants to average.

Especially given that OPs code called analogRead().

Thank you all for your help! Yes I ONLY want to average the A0 reading using smoothing and get the average reading AFTER the user presses the "1" key.

kind regards
hanss