Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. #define filterSamples 5
  2. #define POT_PIN 1
  3. #define MIC_PIN 0
  4. #define LED_PIN 5
  5.  
  6. unsigned int sample;
  7. int test = 0;
  8. int brightness = 0;
  9. int sensSmoothArray1 [filterSamples]; // array for holding raw sensor values for sensor1
  10. int smoothSample, smoothSample2;
  11. int micThreshold;
  12. void setup()
  13. {
  14. pinMode(LED_PIN, OUTPUT);
  15. delay(50);
  16. }
  17.  
  18. void loop(){
  19.  
  20. //campiono
  21. sample = analogRead(MIC_PIN);
  22. micThreshold = analogRead(POT_PIN);
  23. micThreshold = map(micThreshold, 0, 1023, 300, 600);
  24. smoothSample = digitalSmooth(sample, sensSmoothArray1);
  25.  
  26. if (smoothSample > micThreshold){
  27. att_plus(30);
  28. }
  29. else{
  30. att_minus(3);
  31. }
  32. }
  33.  
  34.  
  35.  
  36. void att_plus(int fadeAmount){
  37. brightness = brightness + fadeAmount;
  38. if(brightness >= 255){
  39. brightness = 255;
  40. }
  41. analogWrite(LED_PIN, brightness);
  42. //delay(5);
  43. }
  44.  
  45. void att_minus(int fadeAmount){
  46. brightness = brightness - fadeAmount;
  47. if (brightness <= 0){
  48. brightness = 0;
  49. }
  50. analogWrite(LED_PIN, brightness);
  51. //delay(5);
  52. }
  53.  
  54. int digitalSmooth(int rawIn, int *sensSmoothArray){
  55. int j, k, temp, top, bottom;
  56. long total;
  57. static int i;
  58. // static int raw[filterSamples];
  59. static int sorted[filterSamples];
  60. boolean done;
  61.  
  62. i = (i + 1) % filterSamples; // increment counter and roll over if necc. - % (modulo operator) rolls over variable
  63. sensSmoothArray[i] = rawIn; // input new data into the oldest slot
  64.  
  65. // Serial.print("raw = ");
  66.  
  67. for (j=0; j<filterSamples; j++){ // transfer data array into anther array for sorting and averaging
  68. sorted[j] = sensSmoothArray[j];
  69. }
  70.  
  71. done = 0; // flag to know when we're done sorting
  72. while(done != 1){ // simple swap sort, sorts numbers from lowest to highest
  73. done = 1;
  74. for (j = 0; j < (filterSamples - 1); j++){
  75. if (sorted[j] > sorted[j + 1]){ // numbers are out of order - swap
  76. temp = sorted[j + 1];
  77. sorted [j+1] = sorted[j] ;
  78. sorted [j] = temp;
  79. done = 0;
  80. }
  81. }
  82. }
  83.  
  84. // throw out top and bottom 15% of samples - limit to throw out at least one from top and bottom
  85. bottom = max(((filterSamples * 15) / 100), 1);
  86. top = min((((filterSamples * 85) / 100) + 1 ), (filterSamples - 1)); // the + 1 is to make up for asymmetry caused by integer rounding
  87. k = 0;
  88. total = 0;
  89. for ( j = bottom; j< top; j++){
  90. total += sorted[j]; // total remaining indices
  91. k++;
  92. // Serial.print(sorted[j]);
  93. // Serial.print(" ");
  94. }
  95. return total / k; // divide by number of samples
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement