Commit 12c343f8 authored by Christian Schmiegelow's avatar Christian Schmiegelow

Initial Commit 2

parent e3d583bc
Pipeline #55 failed with stages
#include <Wire.h>
#include <Adafruit_ADS1015.h>
#include <SD.h>
#include <RTClib.h> //needed becuase we have ready-made functions of this librray
#include <avr/wdt.h>
RTC_DS3231 rtc; //the object rtc is created from the class RTC_DS3231
#define deviceAddress 0x68
Adafruit_ADS1115 ads; // Use this for the 16-bit version
//Adafruit_ADS1015 ads; // Use this for the 12-bit version
const int alertPin = 2;
volatile bool continuousConversionReady = false;
int contador = 0;
unsigned long lastmillis = 0;
unsigned long now = 0;
//const char filename[] = "s201206.txt"; //no usar nombres con palabras despues del numero
File txtFile;
String buffer;
unsigned long lastMillis = 0;
const int analogInPin = A7; // Analog input pin that the potentiometer is attached to
int lastHour;
void error(char *str)
{
Serial.print("error: ");
Serial.println(str);
// red LED indicates error
digitalWrite(13, HIGH);
while (1);
}
void openNewFile(){
char filename[] = "LOGGER00.CSV";
for (uint8_t i = 0; i < 100; i++) {
filename[6] = i / 10 + '0';
filename[7] = i % 10 + '0';
//Serial.println(filename);
if (! SD.exists(filename)) {
// only open a new file if it doesn't exist
txtFile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}
if (! txtFile) {
error("couldnt create file");
//Serial.println("could not create file");
}
DateTime nowTime = rtc.now();
txtFile.print("# Start time:");
char buf1[] = "YYYY.MM.DD hh:mm";
txtFile.println(nowTime.toString(buf1));
Serial.print("Logging to: ");
Serial.println(filename);
}
void setup(void)
{
delay(100);
wdt_enable(WDTO_8S); // start watchdog timer
pinMode(alertPin, INPUT);
Serial.begin(9600);
Serial.println("Hello!");
// The ADC input range (or gain) can be changed via the following
// functions, but be careful never to exceed VDD +0.3V max, or to
// exceed the upper and lower limits if you adjust the input range!
// Setting these values incorrectly may destroy your ADC!
// ADS1015 ADS1115
// ------- -------
//ads.setGain(GAIN_TWOTHIRDS); // 2/3x gain +/- 6.144V 1 bit = 3mV 0.1875mV (default)
ads.setGain(GAIN_ONE); // 1x gain +/- 4.096V 1 bit = 2mV 0.125mV
// ads.setGain(GAIN_TWO); // 2x gain +/- 2.048V 1 bit = 1mV 0.0625mV
// ads.setGain(GAIN_FOUR); // 4x gain +/- 1.024V 1 bit = 0.5mV 0.03125mV
// ads.setGain(GAIN_EIGHT); // 8x gain +/- 0.512V 1 bit = 0.25mV 0.015625mV
// ads.setGain(GAIN_SIXTEEN); // 16x gain +/- 0.256V 1 bit = 0.125mV 0.0078125mV
//ads.begin(1,0); // for ESP8266 SDA, SCL can be specified
ads.begin();
rtc.begin();
DateTime nowTime = rtc.now();
Serial.print("Current Date-time: ");
char buf1[] = "YYYY.MM.DD hh:mm";
Serial.println(nowTime.toString(buf1));
// save current hour for file cropping every hour.
lastHour=nowTime.hour();
Serial.println("Starting continous mode on A0 at 250 SPS");
ads.setSPS(ADS1115_DR_250SPS);
ads.startContinuous_SingleEnded(0);
//ads.startContinuous_SingleEnded(1);
//ads.startContinuous_SingleEnded(2);
//ads.startContinuous_SingleEnded(3);
//ads.startContinuous_Differential_0_1();
//ads.startContinuous_Differential_0_3();
//ads.startContinuous_Differential_1_3();
//ads.startContinuous_Differential_2_3();
attachInterrupt(digitalPinToInterrupt(alertPin), continuousAlert, FALLING);
// init the SD card
if (!SD.begin()) {
error("couldnt initiate SD communication");
}
Serial.println("Card initialized.");
// create a new file
openNewFile();
}
void continuousAlert() {
// Do not call getLastConversionResults from ISR because it uses I2C library that needs interrupts
// to make it work, interrupts would need to be re-enabled in the ISR, which is not a very good practice.
now = micros();
continuousConversionReady = true;
}
void loop(void)
{
float result;
long resultBITS;
if (continuousConversionReady) {
continuousConversionReady = false;
resultBITS = ads.getLastConversionResults();
//result= ((float) resultBITS) * ads.voltsPerBit();
//unsigned long now = millis();
DateTime nowTime = rtc.now();
float temp = rtc.getTemperature();
//float sensorValue = analogRead(analogInPin) * 50 / 1023;
buffer += nowTime.unixtime();
buffer += " ";
buffer += now;
buffer += " ";
buffer += String(temp, 1);
buffer += " ";
//buffer += String(sensorValue, 2);
//buffer += " ";
buffer += resultBITS;
buffer += "\r\n";
//Serial.print(buffer);
//Serial.print ("In interrupt routine. Reading is ");
//Serial.print (resultBITS,7);
//Serial.print (" at millisecond ");
//Serial.println(millis());
//Serial.println(now);
contador = contador + 1;
}
if (!continuousConversionReady) {
unsigned int chunkSize = txtFile.availableForWrite();
if (chunkSize && buffer.length() >= chunkSize) {
// write to file and blink LED
digitalWrite(LED_BUILTIN, HIGH);
txtFile.write(buffer.c_str(), chunkSize);
digitalWrite(LED_BUILTIN, LOW);
// remove written data from buffer
buffer.remove(0, chunkSize);
wdt_reset(); // Reset Watchdog timer
}
// Close file and open new one on condition.
DateTime nowTime = rtc.now();
if(nowTime.hour()>lastHour){
lastHour=nowTime.hour();
//write remaining buffer
txtFile.write(buffer.c_str(),buffer.length());
// remove written data from buffer
buffer.remove(0,buffer.length());
txtFile.close();
openNewFile();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment