Datalogger/Datalogger.ino

181 lines
5.1 KiB
C++

//#include <TimerOne.h>
#include <SPI.h>
#include <SD.h>
#include <SparkFun_ADXL345.h>
#include <SoftwareSerial.h>
#include <MsTimer2.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; // Précédent millis() pour flush
unsigned long cm; // millis() courant
bool flag_run = false; // Flag acquisition en route
bool flag = false; // Flag mesure faite
int x,y,z; // Valeurs ADXL
//----------------------------------------------------------------------------------
// 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 = "";
// 0 = millis
// 1 = RTC
// 2,3,4 = X,Y,Z
// 5 = TC
if (Voie_ON[0]) {dataString += String(millis());}
if (Voie_ON[1]) {/*RTC!!*/}
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); // 500ms period
MsTimer2::stop();
}
double readTC() {
uint16_t v;
digitalWrite(MAX6675_CS, LOW);
delay(1);
// Read in 16 bits,
// 15 = 0 always
// 14..2 = 0.25 degree counts MSB First
// 2 = 1 if thermocouple is open circuit
// 1..0 = uninteresting status
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 thermocouple 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 degree (C) counts
}
void config(){
affich(CONFIG);
affich(String("Période acquisition [" + String(T) + "]"));
}
void affich(String x){
Serial.println(x);
BT.println(x);
}
void loop() {
cm = millis();
if (flag) {
dataFile.println(dataString);
Serial.println(dataString);
BT.println(dataString);
if (Voie_ON[2] || Voie_ON[3] || Voie_ON[4]){adxl.readAccel(&x, &y, &z);}
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, mesure);
MsTimer2::start();
}
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;
}
}