Hi,
The sketch below makes a LED produce a number of flashes according to how many loops the sketch has gone through. For example, if I set the period to 10 minutes, the led will produce one flash with a small delay during 10 minutes, after 10 minutes it would flash twice with a small delay every two flashes, during 10 minutes, etc.
unsigned long seconds = 1000L;
unsigned long minutes = seconds * 60;
unsigned long minutesInterval = (1*minutes);
unsigned long startTime;
unsigned long endTime;
int n = 0;
int pin = 13;
int c = 0;
void Pulse() {
digitalWrite(pin, HIGH);
delay(250);
digitalWrite(pin, LOW);
delay(250);
}
void setup() {
pinMode(pin, OUTPUT);
}
void loop() {
startTime = (millis());
endTime = startTime;
c++;
while ((endTime - startTime) <= minutesInterval) {
for (n = 0; n < c; n++) {
Pulse();
}
delay(500);
endTime = millis();
n = 0;
}
}
Now, I want to insert this piece of sketch into a larger sketch, that samples 4 sensors every 10 minutes. I want the LED to indicate how many sampling cycles the sketch has gone through, once the instrument is already running on the field. But when I combine the two codes, nothing happens. I added at the end, but please can someone tell me what I am missing?
#include "SD.h"
#include "Wire.h"
#include "OneWire.h"
#include "DallasTemperature.h"
#include "SPI.h"
#include "RTClib.h"
#include "Adafruit_ADS1015.h"
#define LOG_INTERVAL 1000
#define ONE_WIRE_BUS 2
Adafruit_ADS1115 ads1115 (0x48);
Adafruit_ADS1115 ads1116 (0x49);
DeviceAddress Probe01 = { 0x28, 0x2D, 0x4C, 0x92, 0x08, 0x00, 0x00, 0xE9 };
DeviceAddress Probe02 = { 0x28, 0x4A, 0xD8, 0x91, 0x08, 0x00, 0x00, 0x8F };
const float aref_voltage = 5;
int16_t light_1;
int16_t light_2;
int16_t temp_diff_1;
int16_t temp_diff_2;
const int chipSelect = 10;
String d;
String m;
String h;
String mi;
String stringOne;
unsigned long seconds = 1000L;
unsigned long minutes = seconds * 60;
int minutesInterval = (1 * minutes);
unsigned long startTime;
unsigned long endTime;
int n = 0;
int pin = 13;
int c = 0;
RTC_DS1307 RTC;
File logfile;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void printTemperature(DeviceAddress deviceAddress) {
float tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00) {
Serial.print("Error getting temperature ");
}
else {
Serial.print("C: ");
Serial.print(tempC);
}
}
void dataOutput() {
light_1 = ads1115.readADC_Differential_0_1();
delay(50);
light_2 = ads1115.readADC_Differential_2_3();
delay(50);
temp_diff_1 = ads1116.readADC_Differential_0_1();
delay(50);
temp_diff_2 = ads1116.readADC_Differential_2_3();
delay(50);
sensors.requestTemperatures();
delay(50);
}
void fileName() {
DateTime now;
now = RTC.now();
d = now.day();
m = now.month();
h = now.hour();
mi = now.minute();
stringOne = String(m + d + h + mi + ".csv");
}
void LEDflash() {
digitalWrite(pin, HIGH);
delay(250);
digitalWrite(pin, LOW);
delay(250);
}
void setup() {
Serial.begin(9600);
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT);
pinMode(pin, OUTPUT);
digitalWrite(10, HIGH);
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
else {
Serial.println("card initialized.");
}
Wire.begin(); ads1115.begin(); ads1116.begin(); sensors.begin();
RTC.adjust(DateTime(__DATE__, __TIME__));
if (!RTC.begin()) {
logfile.println("RTC failed");
Serial.println("RTC failed");
}
ads1115.setGain(GAIN_SIXTEEN );
ads1116.setGain(GAIN_SIXTEEN);
sensors.setResolution(Probe01, 10);
sensors.setResolution(Probe02, 10);
fileName();
logfile = SD.open(stringOne, FILE_WRITE);
logfile.println("day,month,year,hour,minute,second,light_1,light_2,temp_diff_1,temp_diff_2,room_temp_1,room_temp_2");
logfile.flush();
pinMode(13, OUTPUT);
}
void loop() {
DateTime now;
dataOutput();
now = RTC.now();
logfile.print(now.day(), DEC); logfile.print(',');
logfile.print(now.month(), DEC); logfile.print(',');
logfile.print(now.year(), DEC); logfile.print(',');
logfile.print(now.hour(), DEC); logfile.print(',');
logfile.print(now.minute(), DEC); logfile.print(',');
logfile.print(now.second(), DEC); logfile.print(',');
logfile.print(light_1); logfile.print(',');
logfile.print(light_2); logfile.print(',');
logfile.print(temp_diff_1); logfile.print(',');
logfile.print(temp_diff_2); logfile.print(',');
logfile.print( sensors.getTempC(Probe01) ); logfile.print(',');
logfile.print( sensors.getTempC(Probe02) ); logfile.print(',');
logfile.println();
logfile.flush();
startTime = (millis());
endTime = startTime;
c++;
while ((endTime - startTime) <= minutesInterval) {
for (n = 0; n < c; n++) {
LEDflash();
}
delay(500);
endTime = millis();
n = 0;
}
}