Hello all, I'm back.
This is in the following my sketch which is managing the measurements steps.
If someone have some time to take a look and help me to improve it...
I'm not a programmer, and I think some improvements are possible...
The idea is to build a weather station. I know it's a frequent subject, but I don't want a simple data logger, because a significant period of recording is one hour, but during this delay, some parameters like wind, or sun, can change a lot...
So, In this sketch, I'm just manage a writing event for each minute with a calculation of average value, minimum & maximum.
The measurement is just a CTN sensor put on the Analog pin A0, without translation.
For exemple, I'm using volatile value, is it necessary?
I'm wondering if it's not possible to create a sub function to calculate Average/Min/Max...??
Best regards
#include <Wire.h>
#define DS1307_I2C_ADDRESS 0x68
#define THERM_PIN 0
volatile int count;
volatile int therm;
volatile int MN_therm;
volatile int MX_therm;
volatile int INT_therm;
volatile int AV_therm;
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val) //--------------------------------------------------------------------------------------------
{
return ( (val/16*10) + (val%16) );
}
// Gets the date and time from the ds1307
void getDateDs1307(byte *second,byte *minute,byte *hour,byte *dayOfWeek,byte *dayOfMonth,byte *month,byte *year) //---
{
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
// A few of these need masks because certain bits are control bits
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
void setup() //-------------------------------------------------------------------------------------------------------
{
Wire.begin();
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0x07);
Wire.write(0x10);
Wire.endTransmission();
attachInterrupt(0, compteur , RISING);
Serial.begin(9600);
Serial.print("Ana Max Min Moy __:__:__ __/__/__");
}
void compteur() //----------------------------------------------------------------------------------------------------
{
count = count + 1;
if (count == 60)
{
count = 0;
}
}
void loop() //--------------------------------------------------------------------------------------------------------
{
int c = count;
while (c == count);
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
therm = analogRead(THERM_PIN);
INT_therm = INT_therm + therm;
AV_therm = INT_therm / 60;
if (therm > MX_therm)
{
MX_therm = therm;
}
else if (therm < MN_therm)
{
MN_therm = therm;
}
else
{
therm = therm;
}
if (second == 0)
{
Serial.print(therm);
Serial.print(" ");
Serial.print(MX_therm);
Serial.print(" ");
Serial.print(MN_therm);
Serial.print(" ");
Serial.print(AV_therm);
Serial.print(" ");
Serial.print(hour);
Serial.print(":");
Serial.print(minute);
Serial.print(":");
Serial.print(second);
Serial.print(" ");
Serial.print(month);
Serial.print("/");
Serial.print(dayOfMonth);
Serial.print("/");
Serial.println(year);
INT_therm = therm;
AV_therm = therm;
MN_therm = therm;
MX_therm = therm;
}
}