Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.73 KB | None | 0 0
  1. // Choose to use filter or not
  2. // Moving average filter with changeable buffer size
  3. // LED toggle is inverted, in order to toggle LED on it has to be set LOW, or to turn it off set to HIGH
  4. // HUZZAH has two LEDs, red LED on pin 0, and blue LED on pin 2
  5.  
  6. /*
  7. How to plot values to matlab
  8. Run this project on the HUZZAH with serial monitor open
  9. in matlab run x = 0:1:999 (change the 999 to the amount of polls you make here)
  10. then paste y = [, and copy paste the results on mic A the Serial window, and close with ]
  11. then run plot(x,y)
  12. then run hold on
  13. then do the same but with mic B, y2 = [].....
  14. then run plot(x,y2)
  15. */
  16.  
  17.  
  18. int DA = A0; // Pin for Analog Output - AO
  19. const uint8_t ledRedPin = 0; // pin to the red LED
  20. const uint8_t ledBluePin = 2; // pin to the blue LED
  21. int led = ledBluePin;
  22. int threshold = 560; //Change this
  23. int adcValue = 0;
  24. int buzzerPin = 10;
  25.  
  26. const uint8_t adcFilterBufferSize = 5; // Change buffer size here
  27. uint16_t adcFilterBuffer[adcFilterBufferSize];
  28.  
  29. const uint16_t amountOfPolls = 4000; // The amount of times to poll
  30. uint16_t valuesA[amountOfPolls];
  31. uint16_t valuesB[amountOfPolls];
  32. uint16_t timeA = 0;
  33. uint16_t timeB = 0;
  34. uint16_t timeADone = 0;
  35. uint16_t timeBDone = 0;
  36. uint16_t timeAX = 0;
  37. uint16_t timeBX = 0;
  38.  
  39. uint32_t timeHigh = 2070;
  40. uint32_t timeLow = 2000;
  41.  
  42. uint32_t timeTestStart = 0;
  43. uint32_t timeTestEnd = 0;
  44.  
  45. uint32_t timeLoopStart = 0;
  46. uint32_t timeBuzzerBegin = 10000; // what time to start buzzer
  47. uint32_t timeBuzzerLength = 30000; // set length of buzzer in microseconds
  48.  
  49. //multiplex channel pins
  50. uint8_t channelA = 50;
  51. uint8_t channelB = 47;
  52. uint8_t channelC = 41;
  53.  
  54.  
  55. void getMicVal(uint16_t &pmicAData, uint16_t &pmicBData) {
  56.  
  57. //Get Mic B data from channel 0(pin 13 on mx)
  58. digitalWrite(channelA, LOW);
  59. digitalWrite(channelB, LOW);
  60. digitalWrite(channelC, LOW);
  61. delayMicroseconds(1); // Delay to give mx some time to switch channel
  62. PIO_Set(PIOB, PIO_PB27B_TIOB0);
  63. while ((adc_get_status(ADC) & ADC_ISR_DRDY) != ADC_ISR_DRDY)
  64. {}; //Wait for end of conversion
  65. PIO_Clear(PIOB, PIO_PB27B_TIOB0);
  66. pmicBData = adc_get_latest_value(ADC); // Read ADC
  67. //pmicBData = analogRead(DA);
  68.  
  69. delayMicroseconds(1);
  70.  
  71. //Get Mic A data from channel 3(pin 12 on mx)
  72. digitalWrite(channelA, HIGH);
  73. digitalWrite(channelB, HIGH);
  74. digitalWrite(channelC, LOW);
  75. delayMicroseconds(1); // Delay to give mx some time to switch channel
  76. PIO_Set(PIOB, PIO_PB27B_TIOB0);
  77. while ((adc_get_status(ADC) & ADC_ISR_DRDY) != ADC_ISR_DRDY)
  78. {}; //Wait for end of conversion
  79. PIO_Clear(PIOB, PIO_PB27B_TIOB0);
  80. pmicAData = adc_get_latest_value(ADC); // Read ADC
  81. //pmicAData = analogRead(DA);
  82. }
  83.  
  84. void setup() {
  85. Serial.begin(115200);
  86. //pinMode(led, OUTPUT);
  87. pinMode(buzzerPin, OUTPUT);
  88.  
  89. // init mx
  90. pinMode(channelA, OUTPUT);
  91. pinMode(channelB, OUTPUT);
  92. pinMode(channelC, OUTPUT);
  93. delay(500);
  94.  
  95. //ADC init
  96. pinMode(DA, INPUT);
  97. pmc_enable_periph_clk(ID_ADC); // To use peripheral, we must enable clock distributon to it
  98. adc_init(ADC, SystemCoreClock, ADC_FREQ_MAX, ADC_STARTUP_FAST); // initialize, set maximum posibble speed
  99. adc_disable_interrupt(ADC, 0xFFFFFFFF);
  100. adc_set_resolution(ADC, ADC_12_BITS);
  101. adc_configure_power_save(ADC, 0, 0); // Disable sleep
  102. adc_configure_timing(ADC, 0, ADC_SETTLING_TIME_3, 1); // Set timings - standard values
  103. adc_set_bias_current(ADC, 1); // Bias current - maximum performance over current consumption
  104. adc_stop_sequencer(ADC); // not using it
  105. adc_disable_tag(ADC); // it has to do with sequencer, not using it
  106. adc_disable_ts(ADC); // deisable temperature sensor
  107. adc_disable_channel_differential_input(ADC, ADC_CHANNEL_7);
  108. adc_configure_trigger(ADC, ADC_TRIG_SW, 1); // triggering from software, freerunning mode
  109. adc_disable_all_channel(ADC);
  110. adc_enable_channel(ADC, ADC_CHANNEL_7); // just one channel enabled
  111. adc_start(ADC);
  112.  
  113. getMicVal(valuesA[0], valuesB[0]); // init mx
  114. memset(adcFilterBuffer, 0, sizeof(adcFilterBuffer)); // Set all values in buffer to 0
  115. delay(500);
  116. Serial.println(" ");
  117. Serial.println(" ");
  118. Serial.println(" ");
  119. Serial.println("BOOTED DUAL MIC TESTSCRIPT.. GET READY....");
  120.  
  121.  
  122. //poll datax
  123. delay(50);
  124. timeLoopStart = micros();
  125. for (int i = 0; i < amountOfPolls; i++) {
  126. /////RAW WITHOUT FILTER
  127. //timeTestStart = micros();
  128. getMicVal(valuesA[i], valuesB[i]);
  129.  
  130. /////MOVING AVERAGE FILTER
  131. // for (uint8_t i = 0; i < adcFilterBufferSize - 1; i++) { // Remove the oldest value by moving all values one step closer to the start of the array
  132. // adcFilterBuffer[i] = adcFilterBuffer[i + 1];
  133. // }
  134. //
  135. // adcFilterBuffer[adcFilterBufferSize - 1] = analogRead(DA); // Save the latest ADC value at the back of the array.
  136. // uint16_t adcFilterBufferSum = 0;
  137. // for (uint8_t i = 0; i < adcFilterBufferSize; i++) { // Add up all the values
  138. // adcFilterBufferSum += adcFilterBuffer[i];
  139. // }
  140. // adcValue = adcFilterBufferSum / adcFilterBufferSize; // Get the average
  141. // values[i] = adcValue;
  142. ///MOVING AVERAGE FILTER
  143.  
  144. // if (adcValue >= threshold) { //Compare analog value with threshold
  145. // digitalWrite(led, LOW); //Turn ON Led(LOW=ON)
  146. // }
  147. // else {
  148. // digitalWrite(led, HIGH); //Turn OFF Led(HIGH=OFF)
  149. // }
  150.  
  151. uint32_t currenttime = micros() - timeLoopStart;
  152. // Get difference
  153. if (valuesA[i] > timeHigh || valuesA[i] < timeLow || !timeADone) {
  154. timeA = currenttime;
  155. timeADone = 1;
  156. timeAX = i;
  157. }
  158. if (valuesB[i] > timeHigh || valuesB[i] < timeLow || !timeBDone) {
  159. timeB = currenttime;
  160. timeBDone = 1;
  161. timeBX = i;
  162. }
  163.  
  164. //Buzzer
  165. if (currenttime > timeBuzzerBegin + timeBuzzerLength) {
  166. digitalWrite(buzzerPin, LOW);
  167. } else if (currenttime > timeBuzzerBegin) {
  168. digitalWrite(buzzerPin, HIGH);
  169. }
  170. //timeTestEnd = micros();
  171. //Serial.println(timeTestEnd - timeTestStart);
  172. //delayMicroseconds(1);
  173. }
  174.  
  175. //Print out
  176. delay(500);
  177. Serial.println("DONE SAMPLING... ");
  178.  
  179. Serial.print("TIME A peak: ");
  180. Serial.print(timeA);
  181. Serial.print("us X: ");
  182. Serial.println(timeAX);
  183.  
  184. Serial.print("TIME B peak:");
  185. Serial.print(timeB);
  186. Serial.print("us X: ");
  187. Serial.println(timeBX);
  188.  
  189. Serial.print("DIFFERENCE ");
  190. if (timeA > timeB)
  191. Serial.println(timeA - timeB);
  192. else
  193. Serial.println(timeB - timeA);
  194.  
  195.  
  196. Serial.println("PRINTING MIC A VALUES... ");
  197. Serial.println(" ");
  198. for (int i = 0; i < amountOfPolls; i++) {
  199. Serial.print(valuesA[i]);
  200. Serial.print(" ");
  201. }
  202. delay(500);
  203. Serial.println(" ");
  204. Serial.println(" ");
  205. Serial.println("PRINTING MIC B VALUES... ");
  206. Serial.println(" ");
  207. for (int i = 0; i < amountOfPolls; i++) {
  208. Serial.print(valuesB[i]);
  209. Serial.print(" ");
  210. }
  211. }
  212.  
  213. void loop() {
  214. //End program
  215. while (1) {
  216. delay(1000);
  217. }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement