Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2015
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. // include the library code:
  2. #include <LiquidCrystal.h>
  3.  
  4. // initialize the library with the numbers of the interface pins
  5. LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
  6.  
  7. volatile unsigned long firstPulseTime;
  8. volatile unsigned long lastPulseTime;
  9. volatile unsigned long numPulses;
  10.  
  11. void isr()
  12. {
  13. unsigned long now = micros();
  14. if (numPulses == 1)
  15. {
  16. firstPulseTime = now;
  17. }
  18. else
  19. {
  20. lastPulseTime = now;
  21. }
  22. ++numPulses;
  23. }
  24.  
  25.  
  26. void setup() {
  27. Serial.begin(9600);
  28. // set up the LCD's number of columns and rows:
  29. lcd.begin(16, 2);
  30. // Print a message to the LCD.
  31. lcd.setCursor(0, 0);
  32. lcd.print("Freq [Hz]:");
  33. lcd.setCursor(0, 1);
  34. lcd.print("Vel [m/s]:");
  35. //pinMode(3, OUTPUT); // put a PWM signal on pin 3, then we can connect pin 3 to pin 2 to test the counter
  36. //analogWrite(3, 128);
  37.  
  38. }
  39.  
  40. // Measure the frequency over the specified sample time in milliseconds, returning the frequency in Hz
  41. float readFrequency(unsigned int sampleTime)
  42. {
  43. numPulses = 0; // prime the system to start a new reading
  44. attachInterrupt(0, isr, RISING); // enable the interrupt
  45. delay(sampleTime);
  46. detachInterrupt(0);
  47. return (numPulses < 3) ? 0 : (1000000.0 * (float)(numPulses - 2))/(float)(lastPulseTime - firstPulseTime);
  48. }
  49.  
  50. void loop() {
  51. float freq = readFrequency(1000);
  52. int rssi = analogRead(3);
  53. //float freq = 5;
  54. float vel = 0.01424 * freq;
  55. // set the cursor to column 0, line 1
  56. // (note: line 1 is the second row, since counting begins with 0):
  57. lcd.setCursor(11, 1);
  58. // print the number of seconds since reset:
  59. lcd.print(vel);
  60. lcd.setCursor(11, 0);
  61. // print the number of seconds since reset:
  62. lcd.print(freq);
  63.  
  64. Serial.print(millis());
  65. Serial.print(",");
  66. Serial.print(freq);
  67. Serial.print(",");
  68. Serial.print(vel);
  69. Serial.print(",");
  70. Serial.println(rssi);
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement