Created
September 3, 2015 21:56
-
-
Save jrleeman/2783be23e956036ddf2e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // include the library code: | |
| #include <LiquidCrystal.h> | |
| // initialize the library with the numbers of the interface pins | |
| LiquidCrystal lcd(12, 11, 7, 6, 5, 4); | |
| volatile unsigned long firstPulseTime; | |
| volatile unsigned long lastPulseTime; | |
| volatile unsigned long numPulses; | |
| void isr() | |
| { | |
| unsigned long now = micros(); | |
| if (numPulses == 1) | |
| { | |
| firstPulseTime = now; | |
| } | |
| else | |
| { | |
| lastPulseTime = now; | |
| } | |
| ++numPulses; | |
| } | |
| void setup() { | |
| Serial.begin(9600); | |
| // set up the LCD's number of columns and rows: | |
| lcd.begin(16, 2); | |
| // Print a message to the LCD. | |
| lcd.setCursor(0, 0); | |
| lcd.print("Freq [Hz]:"); | |
| lcd.setCursor(0, 1); | |
| lcd.print("Vel [m/s]:"); | |
| //pinMode(3, OUTPUT); // put a PWM signal on pin 3, then we can connect pin 3 to pin 2 to test the counter | |
| //analogWrite(3, 128); | |
| } | |
| // Measure the frequency over the specified sample time in milliseconds, returning the frequency in Hz | |
| float readFrequency(unsigned int sampleTime) | |
| { | |
| numPulses = 0; // prime the system to start a new reading | |
| attachInterrupt(0, isr, RISING); // enable the interrupt | |
| delay(sampleTime); | |
| detachInterrupt(0); | |
| return (numPulses < 3) ? 0 : (1000000.0 * (float)(numPulses - 2))/(float)(lastPulseTime - firstPulseTime); | |
| } | |
| void loop() { | |
| float freq = readFrequency(1000); | |
| int rssi = analogRead(3); | |
| //float freq = 5; | |
| float vel = 0.01424 * freq; | |
| // set the cursor to column 0, line 1 | |
| // (note: line 1 is the second row, since counting begins with 0): | |
| lcd.setCursor(11, 1); | |
| // print the number of seconds since reset: | |
| lcd.print(vel); | |
| lcd.setCursor(11, 0); | |
| // print the number of seconds since reset: | |
| lcd.print(freq); | |
| Serial.print(millis()); | |
| Serial.print(","); | |
| Serial.print(freq); | |
| Serial.print(","); | |
| Serial.print(vel); | |
| Serial.print(","); | |
| Serial.println(rssi); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment