Hi
In program #1 = DC Voltmeter with smoothing function is working ok.
I have another program where this lines are related to voltage reading on LCD. program #2
How to change this lines to implement a smoothing function ?
program #1
////////////////////////////////////////////////////////
#define NUM_SAMPLES 10
int sum = 0; // sum of samples taken
int analogInput = PB0;
unsigned char sample_count = 0; // current sample number
float vol = 0.0; // calculated voltage
//////////////////////////////////////////////////////////
#include <LiquidCrystal.h>
LiquidCrystal lcd(PA0, PA1, PA2, PA3, PA4, PA5);
void setup() {
pinMode(PB0, INPUT_ANALOG);
lcd.begin(16, 2);
}
void loop()
{
// take a number of analog samples and add them up
while (sample_count < NUM_SAMPLES)
{
sum += analogRead(analogInput);
sample_count++;
}
{
float vin = analogRead(PB0);
vin = (vin * 3.3) / 4095.0;
lcd.setCursor(0, 0);
lcd.print(vin);
}
}
program # 2, in void loop
{
// read the value at analog input
value = analogRead(analogInput);
vout = (value * 3.3) / 4190.0; // 4200
vin = vout;
}
I declared this part of the first program on the top of the program # 2
define NUM_SAMPLES 10
int sum = 0; // sum of samples taken
int analogInput = PB0;
unsigned char sample_count = 0; // current sample number
float vol = 0.0; // calculated voltage
ted:
In program #1 = DC Voltmeter with smoothing function is working ok.
No, it does not. That is a few lines of nonsense code.
Which Arduino board do you use ?
You can take the average of a few samples. It can be placed in a function to make the sketch better looking.
The average of a few samples for 10-bit ADC of AVR boards can be like this:
const unsigned int n = 100;
unsigned long total = 0;
for( unsigned int i=0; i<n; i++)
{
total += analogRead( A0);
}
total += n / 2; // half bit correction
float voltage = (float) total / (float) n; // average as float, to get extra resolution
voltage = voltage / 1024.0 * 5.0; // convert to voltage
For the Arduino Zero or Arduino Due, it should be "voltage / 4096.0 * 3.3".
You should not change the "4096.0", because that is the number of steps of a 12-bit ADC.
You can change the 3.3 if you know the actual voltage of the 3.3 Volt.
Is this for a ESP8266 ? Then you might need an extra factor for the resistors (if there is a voltage divider at the analog input).
This is the origin of the DC voltmeter to which I added smoothing function, the numbers on LCD are jumping.
int volt;
int i;
//int volt, avolt[50];
//int LED = PB14;
////////////////////////////
const int ResetPin = PA7; // resets Tmin and Tmax both to current temperature T
//int volt = 0; #include <LiquidCrystal.h>
LiquidCrystal lcd(PA0, PA1, PA2, PA3, PA4, PA5);
void setup() {
pinMode(PB0, INPUT_ANALOG);
pinMode(ResetPin, INPUT);
// pinMode(PA7, INPUT_ANALOG);
// pinMode(PB14, OUTPUT);
lcd.begin(16, 2);
}
void loop() {
float volt = analogRead(PB0);
volt = (volt * 3.3) / 4095.0;
lcd.setCursor(0, 0);
lcd.print(volt);
delay(100);
/*
for (int i = 0; i < 50; i++)
//if (volt >= 1000 && volt <= 1300)
//if (volt <= && volt >= )
if(volt <= 1.20 && volt >= 1.00)
{
digitalWrite(PB14, HIGH);
LED = 1;
There's no smoothing in that code either, not even the junk in the comment.
Smoothing is simple arithmetic - sum the last n readings and divide by n.
This is Jackson Pollack programming.
Go away and work through some programming examples, you're wasting our time.
If you want someone to write you code, post in Gigs.