Filter output

This code has input on A0 but there is no output pin.
Can someone help me to add PWM output of this filter ?

  http://www.arduino.cc/en/Tutorial/Smoothing
*/

// 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 us 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 readIndex = 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[readIndex];
  // read from the sensor:
  readings[readIndex] = analogRead(inputPin);
  // add the reading to the total:
  total = total + readings[readIndex];
  // advance to the next position in the array:
  readIndex = readIndex + 1;

  // if we're at the end of the array...
  if (readIndex >= numReadings) {
    // ...wrap around to the beginning:
    readIndex = 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
}

This code has input on A0 but there is no output pin.

What would be connected to the output pin? What IS connected to the input pin?

Can someone help me to add PWM output of this filter ?

Which pin do you want to do analogWrite() on? What do you expect to do if average is above 255?

Some real requirements are needed before you, or we, can write any code.

input 1V sine wave. I am using stm32f103, input A6, output A7.
The signal is noisy, maybe I can get a smooth sine ?

I am using stm32f103

I'm assuming that you know what that is. If you do, you are the only one. Well, you and google, but I'm not going to google it for you.

A couple of C structures that most experienced C programmers will use. Instead of:

1)

for (int thisReading = 0; thisReading < numReadings; thisReading++) {

  • readings[thisReading] = 0;*
  • }*

use

memset(readings, 0, sizeof(reading) / sizeof(reading[0]));

2)

  • total = total - readings[readIndex];*

use

  • total -= readings[readIndex];*

3)

  • total = total + readings[readIndex];*

use

  • total += readings[readIndex];*

4)

readIndex = readIndex + 1;

use

  • readIndex++;*

Truth be told, I doubt that any of these will change code size or speed, since the compiler's pretty smart and likely makes the changes for you. It's purely a style thing.

Ok I do that, but what about pins ?
Compiling original code I have an error " A0' was not declared in this scope"

Compiling original code I have an error " 0' was not declared in this scope"

Do NOT try to summarize error messages. Post the EXACT error message, which includes the line number that the compiler thinks the error is on. It is MUCH easier to find errors that way.

sketch_nov29a:9: error: 'A0' was not declared in this scope

const int pin=A0

^

sketch_nov29a:16: error: 'A0' was not declared in this scope

int inputPin = A0;

^

C:\Users\OWNER\Documents\Arduino\sketch_nov29a\sketch_nov29a.ino: In function 'void setup()':

sketch_nov29a:25: error: 'readings' was not declared in this scope

readings[thisReading] = 0;

^

sketch_nov29a:32: error: a function-definition is not allowed here before '{' token

void loop() {

^

sketch_nov29a:53: error: expected '}' at end of input

}

^

sketch_nov29a:53: error: expected '}' at end of input

exit status 1

You didn't post the whole sketch. There are errors from lines that don't appear there.

 const int pin=A0

Well, this line is missing a ;.

Sorry I was trying to add the pin, this is the original

/*
  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 Apr 2007
  by David A. Mellis  <dam@mellis.org>
  modified 9 Apr 2012
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Smoothing
*/

// 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 us 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 readIndex = 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[readIndex];
  // read from the sensor:
  readings[readIndex] = analogRead(inputPin);
  // add the reading to the total:
  total = total + readings[readIndex];
  // advance to the next position in the array:
  readIndex = readIndex + 1;

  // if we're at the end of the array...
  if (readIndex >= numReadings) {
    // ...wrap around to the beginning:
    readIndex = 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
}

Sorry I was trying to add the pin, this is the original

The original compiles, for me.

The error messages are from code you didn't post. We can NOT see code if you don't post it. We can not help with code we can not see.

const int pin=A0

After adding "const int pin=A0" I have this error, line #32

sketch_nov29a:26: error: 'A0' was not declared in this scope

const int pin=A0

^

sketch_nov29a:32: error: 'A0' was not declared in this scope

int inputPin = A0;

^

C:\Users\OWNER\Documents\Arduino\sketch_nov29a\sketch_nov29a.ino: In function 'void setup()':

sketch_nov29a:39: error: 'readings' was not declared in this scope

readings[thisReading] = 0;

^

C:\Users\OWNER\Documents\Arduino\sketch_nov29a\sketch_nov29a.ino: In function 'void loop()':

sketch_nov29a:45: error: 'readings' was not declared in this scope

total = total - readings[readIndex];

^

exit status 1
'A0' was not declared in this scope

When you posted your code in the code tags, the forum clearly distinguishes between the number 0 and the letter 0. When you don't post your code, or don't post the errors in code tags, we can NOT see whether you used the number 0 or the letter 0. We can't help you with what we can't see. Isn't that clear, yet?

that strange, compiling original I have this error

" sketch_nov29a:33: error: 'A0' was not declared in this scope

int inputPin = A0;

^

exit status 1
'A0' was not declared in this scope"

Here is a new code no errors until I try to add output on A7
A0 was original for for input I replaced it by PA6, compoling was ok , this error is appearing when I try to add output on PA7

Error
sketch_nov29b:15: error: 'A0' was not declared in this scope

int inputPin = A0;

^

exit status 1
'A0' was not declared in this scope

/*
  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 pin 15

  Created 22 April 2007
  By David A. Mellis  <dam@mellis.org>

  http://www.arduino.cc/en/Tutorial/Smoothing

  Ported to Maple 27 May 2010
  by Bryan Newbold
*/

// 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 index1 = 0;                  // the index1 of the current reading
int total = 0;                  // the running total
int average = 0;                // the average

int inputPin = PA6;          // analog input pin
int outputPin = PA7;           // output
void setup() {
    // Declare the input pin as INPUT_ANALOG:
    pinMode(inputPin, INPUT_ANALOG);
    Declare the output pin as OUTPUT _ANALOG;
    pinMode(inputPin, OUTPUT_ANALOG);
    
	 Serial.begin(9600); // Ignored by Maple. But needed by boards using hardware serial via a USB to Serial adaptor
	
    // 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[index1];
    // Read from the sensor:
    readings[index1] = analogRead(inputPin);
    // Add the reading to the total:
    total = total + readings[index1];
    // Advance to the next position in the array:
    index1 = index1 + 1;

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

    // Calculate the average:
    average = total / numReadings;
    // Send it to the computer (as ASCII digits)
    Serial.println(average, DEC);
}

Shouldn't this be a comment?

Declare the output pin as OUTPUT _ANALOG;
pinMode(inputPin, OUTPUT_ANALOG);

Why are you changing inputPin to output?

These are the errors I get from your code.

sketch_nov29b:31: error: 'PA6' was not declared in this scope

 int inputPin = PA6;          // analog input pin

                ^

sketch_nov29b:32: error: 'PA7' was not declared in this scope

 int outputPin = PA7;           // output

                 ^

C:\Users\RICHAR~1\AppData\Local\Temp\arduino_modified_sketch_381511\sketch_nov29b.ino: In function 'void setup()':

sketch_nov29b:35: error: 'INPUT_ANALOG' was not declared in this scope

     pinMode(inputPin, INPUT_ANALOG);

                       ^

sketch_nov29b:36: error: 'Declare' was not declared in this scope

     Declare the output pin as OUTPUT _ANALOG;

     ^

sketch_nov29b:37: error: 'OUTPUT_ANALOG' was not declared in this scope

     pinMode(inputPin, OUTPUT_ANALOG);

                       ^

exit status 1
'PA6' was not declared in this scope

the problem is somewhere here

    // Declare the input pin as INPUT_ANALOG:
    pinMode(inputPin, INPUT_ANALOG);
    Declare the output pin as OUTPUT _ANALOG;
    pinMode(inputPin, OUTPUT_ANALOG);
    
   Serial.begin(9600); // Ignored by Maple. But needed by boards using hardwar

I change to outputPin

sketch_nov29f:39: error: 'OUTPUT_ANALOG' was not declared in this scope

pinMode(outputPin, OUTPUT_ANALOG);

^

exit status 1
'OUTPUT_ANALOG' was not declared in this scope