Hello,
This is my first post so I hope it's the right place and I'm not bothering.
I am working on a project for my University.
I am reading temperatures and I try to count pulses generated by water-meters. I have a loop for writing the temperatures in a txt file on SD Card, time-synced via an RTC. Temperatures are on the same line, separated by commas but I also want to write the number of interrupts.
The problem : if an interrupt is happening in less than a second (on which my loop is set), I miss the count. How can I count the interrupts separately and just write the no. of counts at each loop(each second)?
I want to count interrupts for each water meter in order to show the number of counts at each loop.
LE: the thing is that I might have high frequency interrupts (1 impulse per 50 milliseconds). And I want this to be counted and see how many were counted between loops.
//Arduino UNO
#include <SD.h>
#include <SPI.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <DS3231.h>
// Firul de date este atribuit pinului 2
#define ONE_WIRE_BUS 2
//setează o singură instanță de comunicare cu orice OneWire
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// Addresses of 5 DS18B20s
uint8_t sensor1[8] = { 0x28, 0x60, 0xCF, 0x79, 0xA2, 0x01, 0x03, 0x34 };
uint8_t sensor2[8] = { 0x28, 0xEC, 0x87, 0x79, 0xA2, 0x01, 0x03, 0x74 };
uint8_t sensor3[8] = { 0x28, 0x41, 0xA6, 0x79, 0xA2, 0x01, 0x03, 0x57 };
uint8_t sensor4[8] = { 0x28, 0xD1, 0x6C, 0x79, 0xA2, 0x01, 0x03, 0x79 };
uint8_t sensor5[8] = { 0x28, 0x9D, 0x3E, 0x79, 0xA2, 0x01, 0x03, 0x67 };
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
const int buttonPin = 9; //pentru impulsuri - firul galben de la IWMPL3
File myFile;
DS3231 rtc(SDA, SCL);
int pinCS = 10; // Pin 10 on Arduino Uno
void setup() {
Serial.begin(9600);
pinMode(pinCS, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
// SD Card Initialization
if (SD.begin())
{
Serial.println("Card SD initializat.");
} else
{
Serial.println("Initialiuzare card SD esuata!");
return;
}
sensors.begin();
rtc.begin();
}
void loop(void)
{
if (SD.begin())
lastButtonState = buttonState;
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == LOW) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
}
// if the file didn't open, print an error:
else {
Serial.println("error opening test.txt");
}
}
sensors.requestTemperatures();
myFile = SD.open("test.txt", FILE_WRITE);
if (myFile) {
sensors.requestTemperatures();
myFile.print(rtc.getDateStr());
myFile.print(",");
myFile.print(rtc.getTimeStr());
myFile.print(",");
//myFile.print(" senzor exterior: ");
printTemperature(sensor1);
myFile.print(",");
printTemperature(sensor2);
myFile.print(",");
printTemperature(sensor3);
myFile.print(",");
printTemperature(sensor4);
myFile.print(",");
printTemperature(sensor5);
myFile.print(",");
myFile.println(buttonPushCounter);
myFile.close(); // close the file
}
delayMicroseconds (5000);
// Delay a little bit to avoid bouncing
}
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
Serial.print(tempC);
myFile.print(tempC);
// Serial.print("C | ");
// Serial.print(DallasTemperature::toFahrenheit(tempC));
}