Advertisement
Guest User

Untitled

a guest
Mar 13th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.39 KB | None | 0 0
  1. // Below: pin number for FOUT
  2. #define PIN_NUMBER 4
  3. // Below: number of samples for averaging
  4. #define AVERAGE 4
  5. // Below: define to use serial output with python script
  6. //#define PYTHON_OUTPUT
  7. unsigned int doppler_div = 19;
  8. unsigned int samples[AVERAGE];
  9. unsigned int x;
  10.  
  11. void setup() {
  12.   Serial.begin(115200);
  13.   pinMode(PIN_NUMBER, INPUT);
  14. }
  15.  
  16. void loop() {
  17.   noInterrupts();
  18.   pulseIn(PIN_NUMBER, HIGH);
  19.   unsigned int pulse_length = 0;
  20.   for (x = 0; x < AVERAGE; x++)
  21.   {
  22.     pulse_length = pulseIn(PIN_NUMBER, HIGH);
  23.     pulse_length += pulseIn(PIN_NUMBER, LOW);    
  24.     samples[x] =  pulse_length;
  25.   }
  26.   interrupts();
  27.  
  28.   // Check for consistency
  29.   bool samples_ok = true;
  30.   unsigned int nbPulsesTime = samples[0];
  31.   for (x = 1; x < AVERAGE; x++)
  32.   {
  33.     nbPulsesTime += samples[x];
  34.     if ((samples[x] > samples[0] * 2) || (samples[x] < samples[0] / 2))
  35.     {
  36.       samples_ok = false;
  37.     }
  38.   }
  39.  
  40.   if (samples_ok)
  41.   {
  42.     unsigned int Ttime = nbPulsesTime / AVERAGE;
  43.     unsigned int Freq = 1000000 / Ttime;
  44.  
  45.     #ifdef PYTHON_OUTPUT
  46.       Serial.write(Freq/doppler_div);
  47.     #else
  48.       //Serial.print(Ttime);
  49.       Serial.print("\r\n");
  50.       Serial.print(Freq);
  51.       Serial.print("Hz : ");
  52.       Serial.print(Freq/doppler_div);
  53.       Serial.print("km/h\r\n");
  54.     #endif
  55.   }
  56.   else
  57.   {
  58.     #ifndef PYTHON_OUTPUT
  59.       Serial.print(".");
  60.     #endif
  61.   }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement