Using IF with smoothing code.
i have question regarding following smoothing code downloaded site , works fine:
question: how can use code:
int userinput=serial.read();
if (userinput=='1' )
functionality:
1) user types "1"
2) loop runs 10 times, averaging out values @ a0
3) serial prints average of 10 measured values
4) stops until user types in "1" again.
many help!
kind regards,
hanss
zurich
/*
smoothing
reads repeatedly analog input, calculating running average
, printing computer. keeps ten readings in array ,
continually averages them.
circuit:
* analog sensor (potentiometer do) attached analog input 0
created 22 april 2007
david a. mellis <dam@mellis.org>
modified 9 apr 2012
tom igoe
http://www.arduino.cc/en/tutorial/smoothing
example code in public domain.
*/
// define number of samples keep track of. higher number,
// more readings smoothed, slower output will
// respond input. using constant rather normal variable lets
// use value determine size of readings array.
const int numreadings = 10;
int readings[numreadings]; // readings analog input
int index = 0; // index of current reading
int total = 0; // running total
int average = 0; // average
int inputpin = a0;
void setup()
{
// initialize serial communication computer:
serial.begin(9600);
// initialize readings 0:
(int thisreading = 0; thisreading < numreadings; thisreading++)
readings[thisreading] = 0;
}
void loop() {
// subtract last reading:
total= total - readings[index];
// read sensor:
readings[index] = analogread(inputpin);
// add reading total:
total= total + readings[index];
// advance next position in array:
index = index + 1;
// if we're @ end of array...
if (index >= numreadings)
// ...wrap around beginning:
index = 0;
// calculate average:
average = total / numreadings;
// send computer ascii digits
serial.println(average);
delay(1); // delay in between reads stability
}
question: how can use code:
int userinput=serial.read();
if (userinput=='1' )
functionality:
1) user types "1"
2) loop runs 10 times, averaging out values @ a0
3) serial prints average of 10 measured values
4) stops until user types in "1" again.
many help!
kind regards,
hanss
zurich
/*
smoothing
reads repeatedly analog input, calculating running average
, printing computer. keeps ten readings in array ,
continually averages them.
circuit:
* analog sensor (potentiometer do) attached analog input 0
created 22 april 2007
david a. mellis <dam@mellis.org>
modified 9 apr 2012
tom igoe
http://www.arduino.cc/en/tutorial/smoothing
example code in public domain.
*/
// define number of samples keep track of. higher number,
// more readings smoothed, slower output will
// respond input. using constant rather normal variable lets
// use value determine size of readings array.
const int numreadings = 10;
int readings[numreadings]; // readings analog input
int index = 0; // index of current reading
int total = 0; // running total
int average = 0; // average
int inputpin = a0;
void setup()
{
// initialize serial communication computer:
serial.begin(9600);
// initialize readings 0:
(int thisreading = 0; thisreading < numreadings; thisreading++)
readings[thisreading] = 0;
}
void loop() {
// subtract last reading:
total= total - readings[index];
// read sensor:
readings[index] = analogread(inputpin);
// add reading total:
total= total + readings[index];
// advance next position in array:
index = index + 1;
// if we're @ end of array...
if (index >= numreadings)
// ...wrap around beginning:
index = 0;
// calculate average:
average = total / numreadings;
// send computer ascii digits
serial.println(average);
delay(1); // delay in between reads stability
}
turn smoothing code function can called when required this
code: [select]
initialise variables
setup input pin , serial port
start of loop
if serial input available
read 1 byte serial
if byte matches '1'
call averaging_function
end of if
end of if
end of loop
void averaging_function
code read analog input 10 times , add total
calculate average of readings
print average
end of function
Arduino Forum > Using Arduino > Programming Questions > Using IF with smoothing code.
arduino
Comments
Post a Comment