Wednesday, October 22, 2014

Process for Project: Finding Code

Does Not Work:

http://www.danielandrade.net/2011/04/09/arduino-sound-sensor/

/*
This is the code to make a LED blink with the music.
You have to set the threshold so it' sensible enough to make the led blink.
You connect an LED to PIN13 and the Sound Sensor to Analog Pin 0
 */
int led = 13;
int threshold = 500; //Change This
int volume;
void setup() {                
  Serial.begin(9600); // For debugging
  pinMode(led, OUTPUT);     
}
void loop() {
  
  volume = analogRead(A0); // Reads the value from the Analog PIN A0
  /*
    //Debug mode
    Serial.println(volume);
    delay(100);
  */
  
  if(volume>=threshold){
    digitalWrite(led, HIGH); //Turn ON Led
  }  
  else{
    digitalWrite(led, LOW); // Turn OFF Led
  }
}





Works, but doesn’t change when threshold is adjusted

https://www.inkling.com/read/arduino-cookbook-michael-margolis-2nd/chapter-6/recipe-6-7

/*
microphone sketch

SparkFun breakout board for Electret Microphone is connected to analog pin 0
*/

const int ledPin = 13;            //the code will flash the LED in pin 13
const int middleValue = 512;      //the middle of the range of analog values
const int numberOfSamples = 128;  //how many readings will be taken each time

int sample;                       //the value read from microphone each time
long signal;                      //the reading once you have removed DC offset
long averageReading;              //the average of that loop of readings

long runningAverage=0;          //the running average of calculated values
const int averagedOver= 16;     //how quickly new values affect running average
                                //bigger numbers mean slower

const int threshold=400;        //at what level the light turns on

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  long sumOfSquares = 0;
  for (int i=0; i<numberOfSamples; i++) { //take many readings and average them
    sample = analogRead(0);               //take a reading
    signal = (sample - middleValue);      //work out its offset from the center
    signal *= signal;                     //square it to make all values positive
    sumOfSquares += signal;               //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
}







THIS ONE SEEMS TO WORK!!!!!!!

http://arduino-info.wikispaces.com/Brick-SoundSensor 

/* YourDuino Electronic Brick Test
   Sound Sensor
   terry@yourduino.com */

/*-----( Declare Constants / Pin Numbers )-----*/
#define  SOUND_PIN  A2
#define  LED_PIN    13

/*-----( Declare Variables )-----*/

int  SoundValue  =0;

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  pinMode(LED_PIN, OUTPUT);  // declare the ledPin as an OUTPUT
  Serial.begin(9600);        // initialize serial communication with computer
}/*--(end setup )---*/


void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{
  SoundValue = analogRead(SOUND_PIN);    // read the value from the sensor
  Serial.println(SoundValue, DEC);          // send it to the computer (as ASCII digits)
  
  if (SoundValue > 550)
  {  
    digitalWrite(LED_PIN, HIGH);  // turn the ledPin on
    delay(SoundValue*5);                  // stop the program for some time
  }
  
  digitalWrite(LED_PIN, LOW);   // turn the ledPin off
  delay(200);                  // stop the program for some time

}/* --(end main loop )-- */

/* ( THE END ) */

No comments:

Post a Comment