Audio shield - Noise while using photoresistor with audio shield to generate sine

Status
Not open for further replies.

Manasi

New member
I am trying to build a theremin where i'm controlling the amplitude of a sine wave based on input from a photoresistor (and frequency based on ultrasonic distance sensor).
I am getting really bad noise when the photoresistor input is in the low to middle range. Input from other sensor (ultrasonic distance) does not cause this problem. Any guidance will be appreciated.

code:
Code:
[CODE]#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
//Ultrasonic sensor HC-S04 stuff
#define trigPin 0
#define echoPin 1
#define photoResistorPin 8
#define led 13
long distance, duration;


// GUItool: begin automatically generated code
AudioSynthWaveform   sine1;          //xy=245,312
AudioOutputUSB           usb1;           //xy=841,291
AudioOutputAnalog        dac1;
AudioOutputI2S           i2s1;           //xy=873,209
AudioConnection          patchCord1(sine1, 0, i2s1, 0);
AudioConnection          patchCord2(sine1, 0, i2s1, 1);
AudioConnection          patchCord3(sine1, 0, usb1, 0);
AudioConnection          patchCord4(sine1, 0, usb1, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=469.00000762939453,382.00000762939453
// GUItool: end automatically generated code


float noteFrequency;
float noteAmplitude;
double amplitudeVal; 
long sensorVal;
long sensor1Val;
int key;



void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT);
  sine1.begin(WAVEFORM_SINE);
  AudioMemory(20);
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.85);
  
}
//>>>>>>>>>>>>>> Reading the ultra sonic sensor<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
long sensorReading()
  {
    digitalWrite(trigPin, LOW); 
    delayMicroseconds(2); 
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10); 
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    distance = duration/58.2; //for cm
    Serial.println("Distance ");
    Serial.println(distance);
    return distance;
  }
//>>>>>>>>>>>>>>>>>>>>> Key Mapping <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Translating sensor reading to frequency>>>>>>>>>>>>>>>>>>>>>>
float frequencyCalculator()
  {    
    if((distance>50))
    {
      noteFrequency = 0;
    }
    else 
     {
        if((distance<=5))
         {
          noteFrequency = 261;    //middle C
         } 
        else if((distance>5)&&(distance<=10))
            {
             noteFrequency = 293;   //D
            }
              else if((distance>10)&&(distance<=15))
               {
                noteFrequency = 329;    //E
               } 
               else if((distance>15)&&(distance<=20))
                  {
                   noteFrequency = 349;   //F
                  }
                   else if((distance>20)&&(distance<=25))
                     {
                      noteFrequency = 391;    //G
                     }
                     else if ((distance>25)&&(distance<=30))
                        {
                          noteFrequency = 440;    //A
                        }
                        else if ((distance>30))
                           {
                             noteFrequency = 494;    //B
                           }
    }
    return noteFrequency;     
   }
// >>>>>>>>>>>>>>>>>>>>>> Envelope altering >>>>>>>>>>>>>>>>>>>>>>>>>

// >>>>>>>>>>>>>>>>>>>>>> Amplitude maping >>>>>>>>>>>>>>>>>>>>>>>>>
float amplitudeMapping()
    {  
       
       double z;     
       z = analogRead(photoResistorPin);
       
       amplitudeVal = z/1000;
       Serial.println("aplitude is ");
       Serial.println(amplitudeVal);       
      return amplitudeVal;
    }  
  //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> generating the sound<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
void soundGen()
  {sensorReading();
    noteFrequency = frequencyCalculator();
    
    noteAmplitude = amplitudeMapping();
    
    
    
     
          sine1.frequency(noteFrequency);
          sine1.amplitude(noteAmplitude);
          
          
    
       
     if(noteFrequency == 0)
      {
       //Serial.print("\nDoing nothing \n"); 
        
        digitalWrite(led,LOW);
      }
  }

// >>>>>>>>>>>>>>>>>>>>>>>>>>> Main Loop <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


void loop()
{
 
 soundGen();
  
    Serial.print("\n Key: ");
    Serial.print(key); 
    Serial.print("  amplitudeVal: ");
    Serial.print(amplitudeVal);
    Serial.print("  Frequency: ");
    Serial.print(noteFrequency);
    
 }
[/CODE]
 
If it's with a photoresistor, you should not call it a Theremin. As the original Theremin patent states, its primary characteristic is generating sound with continuous pitch through heterodyning two radio frequencies, one fixed and one variable.

What you are doing is a sensor controlled oscillator. And your code does not output continuous pitch but discrete tones, and that in a very limited range... ;)

The photoresistor risks to pick up ambient 50 or 100Hz noise from AC driven light bulbs, which is then aliased with your sampling rate. All these products will modulate the audio signal... Thus, such a circuit can only work with advanced filtering.
 
Maybe the ResponsiveAnalogRead library can help you get a cleaner measurement of the photosensor signal?

Might also be worth trying a 10 nF capacitor connected as close as reasonably possible between the analog input pin and AGND.
 
Status
Not open for further replies.
Back
Top