Duvida - Analisador de BODE Arduino Mega

Olá.

Eu estou tentando fazer um analisador de frequência com o um arduino mega 2560.

Estou com um problema no código, onde a leitura analógica não está funcionando corretamente, dando apenas o valor 0.0000.

#include "logtendistribution.h"

unsigned int m, range, gateinterval;
int *lgf, *Vs;
int npt, nppd;
int i = 0, j = 0, k, lx;
unsigned int serialWR, WR;

class Sinal
{
    float currentTime, previousTime;
    double range, T, frequency;
    int gateinterval = 1000, qc, cdelay = 0, count = 0, analogPin = 0, varauxwrite;
    int sum = 0, sumv = 0, V = 0;
  public:
    void Gerar(int x, unsigned int serialWR)
    {
      unsigned int flagsinal = 0;
      if (serialWR == 1)
      {
        /*Serial.print("Valores de M loop criacao do sinal: ");
          Serial.print(x,DEC);
          Serial.print("\n");*/
        qc = 0;      //qc é a quantidade de counts
        T = 1 / lgf[x] * 1000000; //period in microseconds
        cdelay = gateinterval * 1000 / T; //gate interval em milisegundos, multiplica por 1000 ai fica em micro, tempo para cada medida, cdelay é a quantidade de vezes que eu quero para uma mesma frequência
        while (qc <= cdelay)
        {
          currentTime = micros();
          if ((flagsinal == 1) && (currentTime - previousTime >= T / 2))
          {
            PORTB = B00000000;  // Turn it off
            previousTime = currentTime;  // Remember the time
            flagsinal = 0; // Update the actual LED
            qc++;
          }
          else if ((flagsinal == 0) && (currentTime - previousTime >= T / 2))
          {
            PORTB = B00000001;  // turn it on
            previousTime = currentTime;   // Remember the time
            flagsinal = 1; // Update the actual LED
          }
        }
        if (x == npt - 1)      //npt number of points
        {
          count = 0;
          PORTB = 0x00;
        }
      }
    }
    void Ler(int x, unsigned int serialWR)
    {
      if (serialWR == 1) {
        /*Serial.print("Valores de M loop Leitura resposta: ");
          Serial.print(x,DEC);
          Serial.print("\n");*/
        while (count < 6) {
          sumv = sumv + analogRead(analogPin);
          count = count + 1;
        }
        Vs[x] = sumv / count;
        sumv = 0;
        count = 0;
      }
    }
    void Escrever(int x, unsigned int serialWR)
    {
      if (WR == 1)
      {
        Serial.println("loop enviando leitura para serial - START: ");
        delay(100);
        Serial.println(x, DEC);
        delay(100);
        Serial.print("\n");
        delay(100);
        for (k = 0; k <= x - 1; k++)
        {
          //Tentar botar os vetores diretos
          Serial.print("loop enviando leitura para serial - Enviando...");
          delay(100);
          Serial.print(k, DEC);
          delay(100);
          Serial.print("\n");
          delay(100);
          varauxwrite = Vs[k];
          Serial.print(varauxwrite, DEC);
          delay(100);
        }
      }
    }
};

void setup()
{
  DDRB = B00000001; // *evil laugh* why not change the data direction register manually, too!
  // this sets all pins on port b as outputs. (this includes pin 13 ~ that has an LED connected on the programmer)
  range = 100;                // range  da frequência
  nppd = 30;                  // number of points per decade
  npt = nppd * log10(range);  //npt number of points
  lgf = ltdistribution(range, nppd);
  pinMode(0, INPUT);
  analogReference(INTERNAL2V56);
  FreqMeasure.begin();
  FreqCount.begin(gateinterval);
  Vs = (int*)malloc(npt * sizeof(int));
  Serial.begin(57600);
}

Sinal S1;

void loop() {
  // put your main code here, to run repeatedly:
  WR = 1;
  for (lx = 0; lx <= npt - 1; lx++) {
    S1.Gerar(lx, WR);
    S1.Ler(lx, WR);
  }
  S1.Escrever(lx, WR);
}

A função S1.Gerar geraria o meu sinal que seria ondas quadráticas variando a frequência de acordo com uma distribuição log10. Que vem do ponteiro lgf.

A arquivo header logtendistribution quem cria isso.

#ifndef LOGTENDISTRIBUTION_H_
#define LOGTENDISTRIBUTION_H_
#endif 
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int *ltdistribution(int range,int nppd)	// nppd number of points per decade
{
	int *p;
	p = (int*)malloc(nppd * log10(range) * sizeof(int));
	printf("\t PONTEIRO CRIADO \n");
	if (p == NULL)
	{
		exit(0);
		printf("\t PONTEIRO NULO \n");
	}
	for (int m= 1; m <= nppd*log10(range)+1; m++)
	{
		p[m-1] = pow(10,float (m - 1) / nppd);
	
	}
	return p;
}

Para a leitura da resposta do meu circuito estou fazendo 6 leituras seguidas e tirando uma média.
Bom pessoal, não sei por que não está funcionando, já tentei de tudo. Quem puder me ajudar, ficaria muito grato. Por favor, qualquer dúvidas eu tentarei responder o mais rápido possível.

Obrigado.

Isto

T = 1 / lgf[x] * 1000000; //period in microseconds

não é bonito... lgf é um apontador... para onde é que está a apontar??

Aqui vai uma pergunta estúpida... onde é que chamas a função analogRead? Em lado algum vejo essa função ou uma média de 6 valores.