Ok, heres the code I am using currently.
I used the sample from above and added the LCD output. I had to change some pin values, as they conflicted with my LCD Interupt.
The lcd is showing the numbers and refreshing ok, just the numbers are all over the place. When I use the tone function to generate a square wave, it reads the freq and duty cycle perfectly
I can't take my PC to the furnace to test with serial output, but the output 'should' be the same as what im seeing on the LCD.
I have a call into the manufacturer of the furnace I am trying to read the value from, but I haven't heard back yet.
The LCD i'm using is the 6 button LCD Keypad Shield. (no brand name on it) Also, at this time I am not using any one the buttons, which is why there is no button reading code, not sure that I have a use for them anyways.
any help is appreciated.
-M-
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // select the pins used on the LCD panel
// Min about 40Hz Max about 6000Hz but this may be due to problems with tone() used to test it.
// Interrupt variables
unsigned long fall_Time; // Placeholder for microsecond time when last falling edge occured.
unsigned long rise_Time; // Placeholder for microsecond time when last rising edge occured.
volatile int frequency = 0; // Calculated frequency.
volatile byte dutyCycle = 0; // Duty Cycle %
volatile byte isr0Tick = 0; // Increments every ISR call (used to know if ISR fired since last read)
void PinChangeISR0(){ // Pin 2 (Interrupt 0) service routine
unsigned long total_Time;
unsigned long on_Time;
unsigned long lastRead = micros(); // Get current time
isr0Tick++; // Kick the tires to say we have been here
if (digitalRead(2) == LOW) {
// Falling edge
fall_Time = lastRead; // Just store falling edge and calculate on rising edge
}
else {
// Rising edge
total_Time = lastRead - rise_Time; // Get total cycle time
frequency = 1000000 / total_Time; // Calulate frequency and store
on_Time = fall_Time - rise_Time; // Get on time during this cycle
dutyCycle = 100 / ((float)total_Time / on_Time); // Convert to a percentage
rise_Time = lastRead; // Store rise time
}
}
void setup() { // Start setup
lcd.begin(16, 2); // start the library
lcd.setCursor(0,0);
lcd.print(" York YP9C Gen2 "); // Model designed for
lcd.setCursor(0,2);
lcd.print(" Mod Valve Disp "); // Part Designed to test
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" Belongs to "); // if lost or stolen
lcd.setCursor(0,2);
lcd.print("Michael Illingby"); // belongs to
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("michael.i@me.com"); // email
lcd.setCursor(0,2);
lcd.print(" Vernon, BC CA "); // where I live
delay(1000);
lcd.clear();
Serial.begin(115200);
pinMode(2,INPUT); // Interrupt Pin
Serial.println(F("ISR Pin 2 Configured For Input."));
attachInterrupt(0,PinChangeISR0,CHANGE); // Attach interrupt handler
Serial.println(F("Pin 2 ISR Function Attached."));
// For testing // Connect pin D2 to D11
// pinMode(11,OUTPUT); // Change 11 to Output
// analogWrite(11,192); // Establish square Wave
tone(11,52); // Generate 52hz tone at 50% duty cycle to test input
}
void loop() { // Start Loop
static byte oldisr0Tick = isr0Tick;
Serial.print(F("Frequency / Duty Cycle = "));
lcd.setCursor(0,0);
lcd.print(F("Fq / Duty"));
delay(1000);
if (oldisr0Tick != isr0Tick) { // ISR has fired so use it's values
Serial.print(frequency);
Serial.print(F(" / "));
Serial.print(dutyCycle);
lcd.clear();
lcd.setCursor(0,1);
lcd.print(frequency);
lcd.print(F(" / "));
lcd.print(dutyCycle);
oldisr0Tick = isr0Tick;
}
else { // No interrupt since last read so must be 0% or 100%
if (digitalRead(2) == LOW){
lcd.clear();
Serial.print(F("??? / 0x"));
lcd.setCursor(0,1);
lcd.print(F("??? / 0x"));
}
else {
lcd.clear();
Serial.print(F("??? / 100x"));
lcd.print(F(" "));
}
}
Serial.println();
}