# Reading problem after triangle wave input

After hitting triangle wave input with oscilloscope
Use array to average
simply put Is that I want to take the average of the triangle wave

Serial.println(X); The reason for X is that I’m not sure which direction the average value is

``````#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <SPI.h>
#include <Ticker.h>

int val;

byte serialA;
int A=0;
int B=0;

//-----------------------------------------------------------//
int inputpin0 = A0;
const int n1 = 4;
int data1[n1];           // all array data

const int n2 = 256;
int data2[n2];           // all array data
//-----------------------------------------------------------//
void setup()
{

for (int i = 0; i < n1 ;i++) //0~4
{
data1[i] = 0;
}
for (int i = 0; i < n2 ;i++) //256
{
data2[i] = 0;
}

Serial.begin(115200);
pinMode(A0,INPUT);
delay(10);

timer1_attachInterrupt(timerIsr);
timer1_enable(TIM_DIV16, TIM_EDGE, TIM_SINGLE);
timer1_write(4166.66);
}

void loop() {

Serial.println(X);
delay(50);
}

void timerIsr()
{
{
}

byte Data[2];
B=B+1;
if (A==1){
if (B>=4){
for(int j=0;j<2;j++)
Serial.write(Data[j]);

B=0;
}
}
{
}

timer1_write(4166.66);
}
``````

simply put Is that I want to take the average of the triangle wave

An += (Sn - An-1) * K

where An is the current average, Sn the current sample and K, a value < 1 which is the time constant for averaging.

gcjr:
An += (Sn - An-1) * K

where An is the current average, Sn the current sample and K, a value < 1 which is the time constant for averaging.

I can understand mathematically
But into code
I may need to take a moment to understand

asas80185:
simply put Is that I want to take the average of the triangle wave

that would be zero for triangle wave, with range from -V to -V and period T.

Are we right to assume that your triangular wave is always >0 ?

that would be zero for triangle wave, with range from -V to -V and period T.

Are we right to assume that your triangular wave is always >0 ?

The values of my triangle wave input >0

asas80185:
The values of my triangle wave input >0

assuming it is a symmetric triangular wave then, the average value for the triangle wave is Vp-p/2.

so in code, you could simply continuously capture samples discarding the ‘previous’ value if the ‘new’ value is greater.
then as soon as you get a value where ‘new’ < ‘previous’ assume that ‘previous’ was the ‘+peak’ value.
no do the converse to get ‘-peak’
Then divide ‘+peak’ - ‘-peak’ by 2 to get the ‘average’

repeating that you could average a series of ‘+peak’ and ‘-peak’ values to get a more accurate average IMHO.

hope that helps.