Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define filterSamples 5
- #define POT_PIN 1
- #define MIC_PIN 0
- #define LED_PIN 5
- unsigned int sample;
- int test = 0;
- int brightness = 0;
- int sensSmoothArray1 [filterSamples]; // array for holding raw sensor values for sensor1
- int smoothSample, smoothSample2;
- int micThreshold;
- void setup()
- {
- pinMode(LED_PIN, OUTPUT);
- delay(50);
- }
- void loop(){
- //campiono
- sample = analogRead(MIC_PIN);
- micThreshold = analogRead(POT_PIN);
- micThreshold = map(micThreshold, 0, 1023, 300, 600);
- smoothSample = digitalSmooth(sample, sensSmoothArray1);
- if (smoothSample > micThreshold){
- att_plus(30);
- }
- else{
- att_minus(3);
- }
- }
- void att_plus(int fadeAmount){
- brightness = brightness + fadeAmount;
- if(brightness >= 255){
- brightness = 255;
- }
- analogWrite(LED_PIN, brightness);
- //delay(5);
- }
- void att_minus(int fadeAmount){
- brightness = brightness - fadeAmount;
- if (brightness <= 0){
- brightness = 0;
- }
- analogWrite(LED_PIN, brightness);
- //delay(5);
- }
- int digitalSmooth(int rawIn, int *sensSmoothArray){
- int j, k, temp, top, bottom;
- long total;
- static int i;
- // static int raw[filterSamples];
- static int sorted[filterSamples];
- boolean done;
- i = (i + 1) % filterSamples; // increment counter and roll over if necc. - % (modulo operator) rolls over variable
- sensSmoothArray[i] = rawIn; // input new data into the oldest slot
- // Serial.print("raw = ");
- for (j=0; j<filterSamples; j++){ // transfer data array into anther array for sorting and averaging
- sorted[j] = sensSmoothArray[j];
- }
- done = 0; // flag to know when we're done sorting
- while(done != 1){ // simple swap sort, sorts numbers from lowest to highest
- done = 1;
- for (j = 0; j < (filterSamples - 1); j++){
- if (sorted[j] > sorted[j + 1]){ // numbers are out of order - swap
- temp = sorted[j + 1];
- sorted [j+1] = sorted[j] ;
- sorted [j] = temp;
- done = 0;
- }
- }
- }
- // throw out top and bottom 15% of samples - limit to throw out at least one from top and bottom
- bottom = max(((filterSamples * 15) / 100), 1);
- top = min((((filterSamples * 85) / 100) + 1 ), (filterSamples - 1)); // the + 1 is to make up for asymmetry caused by integer rounding
- k = 0;
- total = 0;
- for ( j = bottom; j< top; j++){
- total += sorted[j]; // total remaining indices
- k++;
- // Serial.print(sorted[j]);
- // Serial.print(" ");
- }
- return total / k; // divide by number of samples
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement