over the past few weeks while my Internets been flaky I've been working on a multipurpose little tool, though i have come across something i cant quite pin down,
while feeding in an external input driver directly off a buffer (though not quite de-bounced) I'm seeing i need to add some 14.6 milliseconds to the lcd loop time for above 2Khz signals to get the correct reading, the required delay appears to be correct all the way up to 70Khz, and i cannot quite explain it. now i have removed all the code but this one function for simplicities sake and the problem remains?
I am hoping its something glaring in my face that i am missing, and at the same time possible insight into ways to improve this little snippet, (possible directly writing to the LCD as opposed to the library?)
As for why i am using an interrupt, its as another function measures pulse time, and hope to reuse the same pin.
#include <LiquidCrystal.h>
// Working Values
unsigned long duration = 0; // Time of last valid action for frequency patterns
unsigned long temp = 0; // Temporariy variable for holding count for frequency measurement,
unsigned long last = 0; // Used variable for holding count for frequency measurement,
// Interrupt Values
volatile unsigned long count = 0; // Volitile variable for counting pulses for frequency measurement,
// Pin Definitions
#define COUNT 2 // Pin 2 - Pulse counting input
// Task Definitions
LiquidCrystal lcd(A5, A4, A3, A2, A1, A0); // defines LCD pin locations
/* Setup */
/*IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII*/
void setup() {
// Set Device I/O Pinmodes
pinMode(COUNT, INPUT);
// Initialises LCD
lcd.begin(16, 2); // LCD Setup
lcd.noCursor();
}
/*-------------------------------------------------------------------------------------------------------*/
void loop() {
hz();
}
// interupt routine for pulse counting
void pulse() {
count++; // increment count for every pulse
}
// for counting frequency
void hz() {
count = 0; // Reset count for current loop
duration = micros();
attachInterrupt(0, pulse, RISING); // Set up interrupt for counting on input 2
lcd.clear();
lcd.setCursor(4,0); // set cursor to begin
while(1) {
if (micros() - duration >=1000000 ) { // 1 second gate time increased to correct for lcd loop
duration = micros(); // sets duration to current time, resetting the if statement
temp = count; // sets temp to current count to avoid lcd delay variation
lcd.setCursor(4,0); // sets cursor back to start position
lcd.print((count - last)); // prints frequency, (yet to work out why its doubled)
lcd.print(" Hz "); // prints Hz after variable and blanking spaces to avoid a full clear
last = temp; // updates last to current count
}
}
detachInterrupt(0); // removes the interrupt as its no longer needed
}
thanks for any help in advance,
Ryan,