Hey guys! For this month, I want to do a small project using the Arduino. I have little experience with using it, and the alternative would be using the BASIC Stamp, but I chose to use the Arduino.
I have a few question and will separate it into three steps for simplicity reasons, if you could help me with a one or two of those, or even all, that would be great!!!
1., I am connecting a electret microphone to the Arduino.
2., I am connecting a piezo vibration sensor, connect it in parallel with a 10M resistor (or a 2-3M resistor to make it less sensitive) to the Arduino.
3. I want to combine the two on a 2-line LCD that I have purchased and want to output the sound and vibration level on each line.
1.
For the sound detector portion, I use the following code from "Arduino Cookbook"
/*
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
}
the code works good, however, how do I loop it so that it would output only, let's say, 5 values (the average of the 128 values that it gives me) per second?
Also, how do output that number into dB?
2.
For the vibration detector portion, I am using the following code found in one of the Arduino tutorials, and modified the code just a little bit:
const int ledPin = 13; // led connected to digital pin 13
const int knockSensor = A0; // the piezo is connected to analog pin 0
const int threshold = 3; // threshold value to decide when the detected sound is a knock or not
// these variables will change:
int sensorReading = 0; // variable to store the value read from the sensor pin
int ledState = LOW; // variable used to store the last LED status, to toggle the light
void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
Serial.begin(9600); // use the serial port
}
void loop() {
// read the sensor and store it in the variable sensorReading:
sensorReading = analogRead(knockSensor);
// if the sensor reading is greater than the threshold:
if (sensorReading >= threshold) {
// toggle the status of the ledPin:
ledState = !ledState;
// update the LED pin itself:
digitalWrite(ledPin, ledState);
// send the string "Knock!" back to the computer, followed by newline
Serial.println(sensorReading);
}
delay(50); // delay to avoid overloading the serial port buffer
}
This code also works well, was just also wondering how to loop this one. Every time I touch or flick the sensor, it will output around 3-10 numbers, not sure if averaging them would make sense, because I can't tell if I should omit a few of those numbers (noise) or if all those numbers are important. So I am not sure on this one yet, however I would still want only one or two values per seconds, if that is plausible.
And I would like to convert this to the unit 'G'.
3. I want to connect it to an LCD, and have to combine these two codes, but I am not sure, how and where to look for the code to put this on the Arduino.
I will continue experimenting around. Any help would be appreciated!!!
Thanks a bunch!
I have a few question and will separate it into three steps for simplicity reasons, if you could help me with a one or two of those, or even all, that would be great!!!
1., I am connecting a electret microphone to the Arduino.
2., I am connecting a piezo vibration sensor, connect it in parallel with a 10M resistor (or a 2-3M resistor to make it less sensitive) to the Arduino.
3. I want to combine the two on a 2-line LCD that I have purchased and want to output the sound and vibration level on each line.
1.
For the sound detector portion, I use the following code from "Arduino Cookbook"
/*
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
}
the code works good, however, how do I loop it so that it would output only, let's say, 5 values (the average of the 128 values that it gives me) per second?
Also, how do output that number into dB?
2.
For the vibration detector portion, I am using the following code found in one of the Arduino tutorials, and modified the code just a little bit:
const int ledPin = 13; // led connected to digital pin 13
const int knockSensor = A0; // the piezo is connected to analog pin 0
const int threshold = 3; // threshold value to decide when the detected sound is a knock or not
// these variables will change:
int sensorReading = 0; // variable to store the value read from the sensor pin
int ledState = LOW; // variable used to store the last LED status, to toggle the light
void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
Serial.begin(9600); // use the serial port
}
void loop() {
// read the sensor and store it in the variable sensorReading:
sensorReading = analogRead(knockSensor);
// if the sensor reading is greater than the threshold:
if (sensorReading >= threshold) {
// toggle the status of the ledPin:
ledState = !ledState;
// update the LED pin itself:
digitalWrite(ledPin, ledState);
// send the string "Knock!" back to the computer, followed by newline
Serial.println(sensorReading);
}
delay(50); // delay to avoid overloading the serial port buffer
}
This code also works well, was just also wondering how to loop this one. Every time I touch or flick the sensor, it will output around 3-10 numbers, not sure if averaging them would make sense, because I can't tell if I should omit a few of those numbers (noise) or if all those numbers are important. So I am not sure on this one yet, however I would still want only one or two values per seconds, if that is plausible.
And I would like to convert this to the unit 'G'.
3. I want to connect it to an LCD, and have to combine these two codes, but I am not sure, how and where to look for the code to put this on the Arduino.
I will continue experimenting around. Any help would be appreciated!!!
Thanks a bunch!