Datalogger/Datalogger.ino

210 lines
5.7 KiB
C++

#include <SPI.h>
#include <SD.h>
#include <SparkFun_ADXL345.h>
#include <SoftwareSerial.h>
#include <MsTimer2.h>
#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
//----------------------------------------------------------------------------------
// CONSTANTES
//----------------------------------------------------------------------------------
#define PIN_BT_RX 14
#define PIN_BT_TX 15
#define PIN_SD_CS 10
#define MAX6675_CS 8
#define MAX6675_SO 6
#define MAX6675_SCK 9
#define START "Start"
#define STOP "Stop"
#define CONFIG "Config"
const char* ITEM_MENU[] = {"0. Paramètres", "1. Start" , "2. Stop"};
//----------------------------------------------------------------------------------
// VARIABLES
//----------------------------------------------------------------------------------
SoftwareSerial BT(PIN_BT_RX,PIN_BT_TX); // BLUETOOTH
ADXL345 adxl = ADXL345(); // Capteur ADXL
File dataFile; // Fichier sur carte SD
String dataString = ""; // String courante à écrire
unsigned long pm_flush = 0; // Précédent millis() pour flush
unsigned long cm = 0; // millis() courant
unsigned long t_zero = 0;
bool flag_run = false; // Flag acquisition en route
bool flag = false; // Flag mesure faite
int x,y,z; // Valeurs ADXL
tmElements_t tm;
String RTCString;
//----------------------------------------------------------------------------------
// PARAMETRES
//----------------------------------------------------------------------------------
bool Voie_ON[]= { // Voies actives
true, true, true, true, true, true};
unsigned long T = 500; // Période acquisition µs
unsigned long T_FLUSH = 2000; // Période écriture fichier ms
//----------------------------------------------------------------------------------
// ISR MESURE
//----------------------------------------------------------------------------------
void mesure(){
dataString = "";
unsigned long cm_mesure = millis();
// 0 = RTC
// 1 = temps passé
// 2,3,4 = X,Y,Z
// 5 = TC
if (Voie_ON[0]) {dataString += RTCString;}
if (Voie_ON[1]) {dataString += " " + String(cm_mesure-t_zero);}
if (Voie_ON[2] || Voie_ON[3] || Voie_ON[4]){
if (Voie_ON[2]) {dataString += " " + String(x);}
if (Voie_ON[3]) {dataString += " " + String(y);}
if (Voie_ON[4]) {dataString += " " + String(z);}
}
if (Voie_ON[5]) {dataString += " " + String(readTC());}
flag = true;
}
//----------------------------------------------------------------------------------
// CODE
//----------------------------------------------------------------------------------
void menu(){
for (byte i=0; i<3; i++){
Serial.println(ITEM_MENU[i]);
BT.println(ITEM_MENU[i]);
}
}
void setup() {
Serial.begin(9600);
BT.begin(9600);
Serial.print(F("Initializing SD card..."));
BT.print(F("Initializing SD card..."));
if (!SD.begin(PIN_SD_CS)) {
Serial.println("Card failed");
BT.println("Card failed");
while (1);
}
Serial.println(F("card initialized."));
BT.println(F("card initialized."));
pinMode(MAX6675_CS, OUTPUT);
pinMode(MAX6675_SO, INPUT);
pinMode(MAX6675_SCK, OUTPUT);
adxl.powerOn();
adxl.setRangeSetting(4);
dataFile = SD.open("datalog.txt", FILE_WRITE);
menu();
MsTimer2::set(T, mesure);
MsTimer2::stop();
}
double readTC() {
uint16_t v;
digitalWrite(MAX6675_CS, LOW);
delay(1);
v = shiftIn(MAX6675_SO, MAX6675_SCK, MSBFIRST);
v <<= 8;
v |= shiftIn(MAX6675_SO, MAX6675_SCK, MSBFIRST);
digitalWrite(MAX6675_CS, HIGH);
if (v & 0x4) // Bit 2 indicates if the TC is disconnected
{return NAN;}
v >>= 3; // The lower three bits (0,1,2) are discarded status bits
return v*0.25; // The remaining bits are the number of 0.25°C counts
}
void config(){
affich(CONFIG);
affich(String("Période acquisition [" + String(T) + "]"));
T=input(T);
affich(String("T = " + String(T)));
menu();
}
long input(long x){
long in = 0;
while((Serial.available()==0) && (BT.available()==0)){}
if (Serial.available()>0) {in=Serial.parseInt();}
if ( BT.available()>0) { in=BT.parseInt();}
if (in>0){return in;}else{return x;}
}
void affich(String x){
Serial.println(x);
BT.println(x);
}
void MAJmesure(){
if (Voie_ON[2] || Voie_ON[3] || Voie_ON[4]){adxl.readAccel(&x, &y, &z);}
if (Voie_ON[0]){
if (RTC.read(tm)){
RTCString = String(tm.Hour) + ":" ;
if (tm.Minute < 10) {RTCString += "0";}
RTCString += String(tm.Minute) + ":";
if (tm.Second < 10) {RTCString += "0";}
RTCString += String(tm.Second);
} else {
RTCString = "ERR";
}
}
}
void loop() {
cm = millis();
if (flag) {
dataFile.println(dataString);
Serial.println(dataString);
BT.println(dataString);
MAJmesure();
flag=false;
}
byte in = 0;
if (Serial.available()>0){in = Serial.read();}
if ( BT.available()>0){in = BT.read();}
if (!flag_run && in == 48) {
config();
}
if (!flag_run && in == 49) {
Serial.println(START);
BT.println(START);
flag_run = true;
MsTimer2::set(T-1, mesure);
MsTimer2::start();
t_zero = millis();
MAJmesure();
mesure();
}
if (flag_run && in == 50) {
Serial.println(STOP);
BT.println(STOP);
flag_run = false;
MsTimer2::stop();
dataFile.flush();
menu();
}
if (flag_run && ((cm-pm_flush)>T_FLUSH)) {
dataFile.flush();
pm_flush=cm;
}
}