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
}
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.
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.
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
}
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?
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);
}
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
// 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