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
int readIndex1 = 0; // the index1 of the current reading
const int n2 = 256;
int data2[n2]; // all array data
int readIndex2 = 0; // the index1 of the current reading
//-----------------------------------------------------------//
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()
{
data1[readIndex1]= analogRead(inputpin0);
data2[readIndex2]=(data1[0]+data1[1]+data1[2]+data1[3])/4;
readIndex2 = readIndex2 + 1;
if(readIndex2 >= n2 )
{
readIndex2 = 0;
}
byte Data[2];
B=B+1;
Data[0]=data2[readIndex2]/256;
Data[1]=data2[readIndex2]%256;
if (A==1){
if (B>=4){
for(int j=0;j<2;j++)
Serial.write(Data[j]);
B=0;
}
}
readIndex1 = readIndex1 + 1;
if(readIndex1 >= n1 )
{
readIndex1 = 0;
}
timer1_write(4166.66);
}