G2A Many GEOs
SHARE
TWEET

exhast_servo

p4ul_allen Nov 20th, 2017 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // FHT, http://wiki.openmusiclabs.com/wiki/ArduinoFHT
  2. #define LOG_OUT 1 // use the log output function
  3. #define LIN_OUT8 1 // use the linear byte output function
  4. #define FHT_N 256 // set to 256 point fht
  5. #include <FHT.h> // include the library
  6.  
  7.  
  8. // pins
  9. #define MicPin A0 // used with analogRead mode only
  10.  
  11.  
  12. // consts
  13. #define AmpMax (1024 / 2)
  14. #define MicSamples (1024*2) // Three of these time-weightings have been internationally standardised, 'S' (1 s) originally called Slow, 'F' (125 ms) originally called Fast and 'I' (35 ms) originally called Impulse.
  15.  
  16.  
  17. // modes
  18. #define Use3.3 // use 3.3 voltage. the 5v voltage from usb is not regulated. this is much more stable.
  19. #define ADCReClock // switch to higher clock, not needed if we are ok with freq between 0 and 4Khz.
  20. #define ADCFlow // read data from adc with free-run (not interupt). much better data, dc low. hardcoded for A0.
  21.  
  22.  
  23. #define FreqLog // use log scale for FHT frequencies
  24. #ifdef FreqLog
  25. #define FreqOutData fht_log_out
  26. #define FreqGainFactorBits 0
  27. #else
  28. #define FreqOutData fht_lin_out8
  29. #define FreqGainFactorBits 3
  30. #endif
  31. #define FreqSerialBinary
  32.  
  33.  
  34. #define VolumeGainFactorBits 0
  35.  
  36.  
  37. // macros
  38. // http://yaab-arduino.blogspot.co.il/2015/02/fast-sampling-from-analog-input.html
  39. #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
  40. #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
  41.  
  42.  //#################################################################################################################################
  43. int enablePin = 11;
  44. int in1Pin = 10;
  45. int in2Pin = 9;
  46. int potPin = 1;
  47. int potVal = 0;
  48. int LoopCount = 0;
  49. boolean reverse = false;
  50. float RefDbValue;
  51. //int InitialDbValue = true;
  52.  
  53. void setup() {
  54.   // put your setup code here, to run once:
  55.    pinMode(in1Pin, OUTPUT);
  56.    pinMode(in2Pin, OUTPUT);
  57.    pinMode(enablePin, OUTPUT);
  58.  
  59.    int speed = 255;
  60.    setMotor(0,true);
  61.    delay(1000);
  62.  
  63.   while(LoopCount < 3){
  64.      switch (LoopCount){
  65.       case 0:
  66.         //reverse = true;
  67.         Serial.println("potPin Value = " + analogRead(potPin));
  68.         while(analogRead(potPin) < 1023){
  69.           setMotor(speed,false);
  70.         }
  71.         setMotor(0,true);
  72.         break;
  73.       case 1:
  74.         //reverse = false;
  75.         Serial.println("potPin Value = " + analogRead(potPin));
  76.         while(analogRead(potPin) > 0){
  77.           setMotor(speed,true);
  78.         }
  79.         setMotor(0,false);
  80.         break;
  81.       case 2:
  82.         //reverse = true;
  83.         Serial.println("potPin Value = " + analogRead(potPin));
  84.         while(analogRead(potPin) < 1023){
  85.           setMotor(speed,false);
  86.         }
  87.         setMotor(0,false);
  88.        break;
  89.      }
  90.      LoopCount++;
  91.   }
  92.  
  93. //#############################################################################################################
  94.   //pinMode(MicPin, INPUT); // relevant for digital pins. not relevant for analog. however, don't put into digital OUTPUT mode if going to read analog values.
  95.  
  96.  
  97. #ifdef ADCFlow
  98.   // set the adc to free running mode
  99.   // register explanation: http://maxembedded.com/2011/06/the-adc-of-the-avr/
  100.   // 5 => div 32. sample rate 38.4
  101.   // 7 => switch to divider=128, default 9.6khz sampling
  102.   ADCSRA = 0xe0+7; // "ADC Enable", "ADC Start Conversion", "ADC Auto Trigger Enable" and divider.
  103.   ADMUX = 0x0; // use adc0 (hardcoded, doesn't use MicPin). Use ARef pin for analog reference (same as analogReference(EXTERNAL)).
  104. #ifndef Use3.3
  105.   ADMUX |= 0x40; // Use Vcc for analog reference.
  106. #endif
  107.   DIDR0 = 0x01; // turn off the digital input for adc0
  108. #else
  109. #ifdef Use3.3
  110.   analogReference(EXTERNAL); // 3.3V to AREF
  111. #endif
  112. #endif
  113.  
  114.  
  115. #ifdef ADCReClock // change ADC freq divider. default is div 128 9.6khz (bits 111)
  116.   // http://yaab-arduino.blogspot.co.il/2015/02/fast-sampling-from-analog-input.html
  117.   // 1 0 0 = mode 4 = divider 16 = 76.8khz
  118.   //sbi(ADCSRA, ADPS2);
  119.   //cbi(ADCSRA, ADPS1);
  120.   //cbi(ADCSRA, ADPS0);
  121.   // 1 0 1 = mode 5 = divider 32 = 38.4Khz
  122.   sbi(ADCSRA, ADPS2);
  123.   cbi(ADCSRA, ADPS1);
  124.   sbi(ADCSRA, ADPS0);
  125. #endif
  126.  
  127.  
  128.   // serial
  129.   Serial.begin(9600);
  130.   while (!Serial); // Wait untilSerial is ready - Leonardo
  131.   Serial.println("Starting mic demo");
  132. //#########################################################################################################################
  133. MeasureVolume(true,0);
  134. MeasureVolume(true,0);
  135. MeasureVolume(true,0);
  136. MeasureVolume(true,0);
  137. MeasureVolume(true,0);
  138. MeasureVolume(true,0);
  139. MeasureVolume(true,0);
  140. }
  141.  
  142.  
  143. void loop() {
  144.   // put your main code here, to run repeatedly:
  145.   // int speed = 255;
  146.   // setMotor(speed,reverse);
  147.  
  148. //#########################################################################################################################
  149.   potVal = analogRead(potPin);
  150.   Serial.println("PotPin Value = " + potVal);
  151.   MeasureVolume(false,potVal);
  152. //#########################################################################################################################
  153. }
  154.  
  155. void setMotor(int speed, boolean reverse) {
  156.    analogWrite(enablePin, speed);
  157.    digitalWrite(in1Pin, ! reverse);
  158.    digitalWrite(in2Pin, reverse);
  159. }
  160.  
  161. // calculate volume level of the signal and print to serial and LCD
  162. void MeasureVolume(boolean InitialDbValue, int ServoPos)
  163. {
  164.   long soundVolAvg = 0, soundVolMax = 0, soundVolRMS = 0, t0 = millis();
  165.   //cli();  // UDRE interrupt slows this way down on arduino1.0
  166.   for (int i = 0; i < MicSamples; i++)
  167.   {
  168. #ifdef ADCFlow
  169.     while (!(ADCSRA & /*0x10*/_BV(ADIF))); // wait for adc to be ready (ADIF)
  170.     sbi(ADCSRA, ADIF); // restart adc
  171.     byte m = ADCL; // fetch adc data
  172.     byte j = ADCH;
  173.     int k = ((int)j << 8) | m; // form into an int
  174. #else
  175.     int k = analogRead(MicPin);
  176. #endif
  177.     int amp = abs(k - AmpMax);
  178.     amp <<= VolumeGainFactorBits;
  179.     soundVolMax = max(soundVolMax, amp);
  180.     soundVolAvg += amp;
  181.     soundVolRMS += ((long)amp*amp);
  182.   }
  183.   soundVolAvg /= MicSamples;
  184.   soundVolRMS /= MicSamples;
  185.   float soundVolRMSflt = sqrt(soundVolRMS);
  186.   //sei();
  187.  
  188.  
  189.   float dB = 20.0*log10(soundVolRMSflt/AmpMax);
  190.  
  191.  
  192.   // convert from 0 to 100
  193.   soundVolAvg = 100 * soundVolAvg / AmpMax;  
  194.   soundVolMax = 100 * soundVolMax / AmpMax;  
  195.   soundVolRMSflt = 100 * soundVolRMSflt / AmpMax;
  196.   soundVolRMS = 10 * soundVolRMSflt / 7; // RMS to estimate peak (RMS is 0.7 of the peak in sin)
  197.  
  198.  
  199.   // print
  200.   Serial.print("Time: " + String(millis() - t0));
  201.   Serial.print(" Amp: Max: " + String(soundVolMax));
  202.   Serial.print("% Avg: " + String(soundVolAvg));
  203.   Serial.print("% RMS: " + String(soundVolRMS));
  204.   Serial.println("% dB: " + String(dB,3));
  205.   //Serial.println("% dB: " + String(dB));
  206. //Serial.println("InitialDbValue: " + String(InitialDbValue));
  207.   if(InitialDbValue == true) {
  208.     RefDbValue = dB;
  209.     Serial.println("RefDbValue = " + String(RefDbValue,3));
  210.   }
  211.   else {
  212.     Serial.println("RefDbValue = " + String(RefDbValue,3));
  213.     Serial.println("abs = " + String(abs(RefDbValue - dB)));
  214.     if(abs(RefDbValue - dB) < 10.00 ){
  215.       //potVal = analogRead(potPin);
  216.       if (ServoPos > 0){
  217.          setMotor(255,true);
  218.       }
  219.     }
  220.     else {
  221.  
  222.        if(abs(RefDbValue - dB) >= 10.00) {
  223.           //potVal = analogRead(potPin);
  224.           if(ServoPos < 1023){
  225.              setMotor(255,false);
  226.           }
  227.        }
  228.     }
  229.   }
  230. }
RAW Paste Data
Ledger Nano X - The secure hardware wallet
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top