Thanks @sherzaad , @Grumpy_Mike !
I was finally able to get the interrupts and read the pulse/duty cycle.
I have a working sketch, but i am having an issue with the interrupt incorporation.
This is a cut down version of my sketch, so its a little messy, but its a decent example of what i am trying to do. Basically i am reading pulse/duty cycle from a circuit and using a switch to start and stop datalogging to an SD card. Each one of these work separately but not together.
The pulse reading with the intrupt is working perfect, but when i press the push button to start the data log process the prg. gets hung up. Could you help point me in a direction to take this?
// FILE: freqCounter06.ino
// AUTHOR: Rob Tillaart
// DATE: 2013-04-13
// PURPOSE: freq counter
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#define DISPLAY_INTERVAL 1000000L
#define IRQPIN 3
//LCD Address
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x3F for a 16 chars and 2 line display
volatile unsigned int count = 0;
volatile unsigned int ovf = 0;
const int PWMOutPin=6; //Used to verify circuit with 5V into octo
unsigned long frequency = 0;
unsigned long highCount = 0;
unsigned long totalCount = 0;
float dutyCycle = 0;
unsigned long lastDisplay = 0;
// Data Logging setup
#define LOG_INTERVAL 200 // mills between entries (reduce to take more/faster data)
#define SYNC_INTERVAL 1000 // mills between calls to flush() - to write data to the card
#define ECHO_TO_SERIAL 1 // echo data to serial port
#define WAIT_TO_START 0 // Wait for serial input in setup()
uint32_t syncTime = 0; // time of last sync()
unsigned long DataCurrent, DataPrevious, TimeOutPrevious=0;
int DataLEDPin= 7;
const int chipSelect = 10;
RTC_DS1307 RTC; // define the Real Time Clock object
File logfile;
void error(char *str){
Serial.print("error: ");
Serial.println(str);
while(1);
}
//Datalog Start Screen
void DataLogStart(){
lcd.clear();
lcd.setCursor(1,0);
lcd.print("DATALOGGING ON");
delay(300);
lcd.clear();
}
//Datalog Stop Screen
void DataLogStop(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("DATALOGGING OFF");
lcd.clear();
}
//No SD Screen
void NoSD(){
lcd.clear();
lcd.setCursor(3,0);
lcd.print("NO SD CARD");
lcd.setCursor(0,1);
lcd.print("DATALOGGING N/A");
delay(500);
lcd.clear();
}
//Datalog Available Screen
void DataOK(){
lcd.clear();
lcd.setCursor(3,0);
lcd.print("DATALOGGING");
lcd.setCursor(4,1);
lcd.print("AVAILABLE");
delay(500);
lcd.clear();
}
// DataLog File Create
void FileCreate(){
char filename[] = "LOGGER00.CSV";
for (uint8_t i = 0; i < 100; i++) {
filename[6] = i/10 + '0';
filename[7] = i%10 + '0';
if (! SD.exists(filename)) {
// only open a new file if it doesn't exist
logfile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}}
if (! logfile) {
error("couldnt create file");
}
Serial.print("Logging to: ");
Serial.println(filename);
delay(1000);
//connect to RTC
Wire.begin();
if (!RTC.begin()) {
logfile.println("RTC failed");
#if ECHO_TO_SERIAL
Serial.println("RTC failed");
#endif //ECHO_TO_SERIAL
}
logfile.println("millis,stamp,PSI,DC");
#if ECHO_TO_SERIAL
Serial.println("millis,stamp,PSI,DC");
#endif //ECHO_TO_SERIAL;
}
void pulse(){
if (PIND & 0x04)
{
TCNT1 = 0x0000;
ovf = 0;
}
else
{
highCount += TCNT1;
highCount += ovf*65536L;
}
count++;
}
// Push Button & Datalog_Start/Stop
int ButtonIn=4, ButtonNew, ButtonOld=1, ButtonSta=0;
unsigned long ButtonPrevious, ButtonCurrent;
// Variables will change:
//
int buttonState = 1,lastButtonState = 1,startPressed = 0,endPressed = 0,holdTime = 0,idleTime = 0,DataPause=3000, DataState;
void setup()
{
lcd.init(); //LCD initiate
lcd.clear(); //Clear Screen
lcd.backlight(); // Make sure backlight is on
Serial.begin(9600);
Serial.println("* Frequency Counter 0.6 *");
Serial.println(" ");
Serial.println("Connect pulse TTL to Arduino pin 2");
Serial.println("Connect pulse GND to Arduino GND");
pinMode(IRQPIN, INPUT);
attachInterrupt(0, pulse, CHANGE);
pinMode(PWMOutPin,OUTPUT);
pinMode(ButtonIn, INPUT_PULLUP); //Setup button (ground-Digital input using internal pull-up)
TIMSK1 = (1 << TOIE1); // timer overflow interrupt enabled
TCCR1A = 0x00; //
TCCR1B = 1; // prescaler == 1 => timer runs at clock speed: 16 MHz
pinMode(10, OUTPUT);
}
void loop()
{
analogWrite(PWMOutPin,150); //Used to verify circuit with 5V into octo
if(digitalRead(ButtonIn)==0){
FileCreate();
lcd.clear();
Serial.println("DataLog ON");
DataLogStart();
DateTime now;
unsigned long DataCurrent= millis();
if (DataState==1 && DataCurrent - DataPrevious > 250){
// fetch the time
now = RTC.now();
// log time
logfile.print(now.unixtime()); // seconds since 2000
logfile.print(", ");
logfile.print(now.year(), DEC);
logfile.print("/");
logfile.print(now.month(), DEC);
logfile.print("/");
logfile.print(now.day(), DEC);
logfile.print(" ");
logfile.print(now.hour(), DEC);
logfile.print(":");
logfile.print(now.minute(), DEC);
logfile.print(":");
logfile.print(now.second(), DEC);
#if ECHO_TO_SERIAL
Serial.print(now.unixtime()); // seconds since 2000
Serial.print(", ");
Serial.print(now.year(), DEC);
Serial.print("/");
Serial.print(now.month(), DEC);
Serial.print("/");
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(":");
Serial.print(now.minute(), DEC);
Serial.print(":");
Serial.print(now.second(), DEC);
#endif //ECHO_TO_SERIAL
logfile.println(dutyCycle);
#if ECHO_TO_SERIAL
Serial.println(dutyCycle);
#endif //ECHO_TO_SERIAL
DataPrevious=DataCurrent;
}
// Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
// which uses a bunch of power and takes time
if ((millis() - syncTime) < SYNC_INTERVAL) return;
syncTime = millis();
logfile.flush();
}
if(digitalRead(ButtonIn)==1){
lcd.clear();
Serial.println("DataLog Off");
DataLogStop();
}
uint32_t now = micros();
if (now - lastDisplay > DISPLAY_INTERVAL)
{
lastDisplay = now;
cli();
{
frequency = count;
count = 0;
totalCount = highCount;
highCount = 0;
}
sei();
frequency /= 2;
dutyCycle = totalCount/160000.0;
Serial.print(frequency);
Serial.print(" Hz. - ");
Serial.print(dutyCycle, 1);
Serial.println(" %");
}
}
ISR(TIMER1_OVF_vect)
{
ovf++;
}