Pages: [1]   Go Down
Author Topic: Cant get VU meter and sin generator to work together.  (Read 236 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, I'm trying to combine a sin wave generator and a VU meter. The problem is that the wave crested is distorted.

Code:
void loop() {
  long sumOfSquares = 0;
 
  for (int i=0; i<numberOfSamples; i++) { //take many readings and average them
   sample = analogRead(1);             //take a reading
    signal1 = (sample - middleValue);      //work out its offset from the center
    signal2 = signal1 * signal1;                     //square it to make all values positive
   sumOfSquares += signal2;               //add to the total
  }
  averageReading = sumOfSquares/numberOfSamples;     //calculate running average
  runningAverage=(((averagedOver-1)*runningAverage)+averageReading)/averagedOver;

 /* if (runningAverage>threshold){         //is average more than the threshold ?
    digitalWrite(ledPin, HIGH);          //if it is turn on the LED
  }else{
    digitalWrite(ledPin, LOW);           //if it isn't turn the LED off
  } */
  Serial.println(runningAverage);        //print the value so you can check it
  //delay (10);
 
  if (runningAverage > 0)
  {
    digitalWrite(led[1], HIGH);   
  }
  else { digitalWrite(led[1], LOW);}
 

Even when I disconnect all the LED's and the electret microphone I'm having this problem. There is something wrong with the code itself. Any help is greatly appreciated.

* Phas_Freq_VU.ino (3.69 KB - downloaded 6 times.)

* photo.JPG (2104.36 KB, 3264x2448 - viewed 17 times.)
* Waveforms.h (86.7 KB - downloaded 6 times.)
« Last Edit: March 12, 2014, 12:08:04 pm by shareefm » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Bump
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 505
Posts: 31343
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Can't see any distortion.

Do you mean that double pulse at the end?
That is caused by a small delay between the cycles, probbly caused by the timer interrupt service routine.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

anyway to remove it?
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 505
Posts: 31343
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

anyway to remove it?

Well it is an artifact of your scope, there is no distortion just a time delay.

As you have not posted all of your code it is difficult to know if you are using the millis() timer. You could try disabling the interrupts.
Logged

Pages: [1]   Go Up
Jump to: