Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Photodiode Data Logger
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Christian Schmiegelow
Photodiode Data Logger
Commits
12c343f8
Commit
12c343f8
authored
Aug 04, 2021
by
Christian Schmiegelow
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Initial Commit 2
parent
e3d583bc
Pipeline
#55
failed with stages
Changes
1
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
201 additions
and
0 deletions
+201
-0
continuousAcquisition.ino
continuousAcquisition/continuousAcquisition.ino
+201
-0
No files found.
continuousAcquisition/continuousAcquisition.ino
0 → 100644
View file @
12c343f8
#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
();
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment