LED indicating number of loops that has gone through (with sketches in boxes)

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;
  }
}

You could have simply edited your earlier post to add the code tags, rather than repeating the post.

That code is just way too confusing. One letter global variable names, many of which should be local, suck. Use meaningful names.

dataOutput() is a dumb name for a function that does not output any data.

There are NO comments to help decipher that code.

But when I combine the two codes, nothing happens.

Nonsense. Something happens. You are either not observant enough, or you consider that actually happens as trivial. We can NOT see what you see. You need to tell us EVERYTHING that you observe, no matter how trivial.

If necessary, you need to add more Serial.print() statements to obtain more observations.

I like to do these problems as classes. I'd write a class with a loop whose job is "flash the LED n times". A trick I like to use is that internally, an even numner means the led is off an an odd number means the led is on. So, to flash 3 tmes I turn the led on and set the flash count to 7.

struct Flasher {
  byte pin = 6; // whatever
  uint32_t ct;
  uint32_t flashLen = 250;
  uint32_t flashStart;

  void flashMe(int nFlashes) {
    digitalWrite(pin, HIGH);
    flashStart = millis();
    ct = nFlashes * 2 - 1;
  }

  void loop() {
    if(ct > 0 && millis() - flashStart >= flashLen) {
      ct --;
      digitalWrite(pin, ct %2 == 0 ? LOW : HIGH);
      flashStart = millis();
    }
  }
} myFlasher;


void loop() {
  myFlasher.loop();

  // other code goes here
}

And now, at ant time in your other code you just go myFlasher.flashMe(3); to get three flashes. You can do a similar thing and create an object HourCounter to tell the flasher to flash when the hour rolls over. The hour counter doesn't have to worry about how to time flashing - the Flasher does that.

class HourCounter {
  boolean amCounting = false;
  int nHours;
  uint_t32 hourMark;

  void startCounting() {
    amCounting = true;
    hourMark = millis();
    nHours = 0;
  }

  void stopCounting() {
    amCounting = false;
  }

  void loop() {
    if(amCounting && millis() - hourMark >= 60L * 60L * 1000L) {
      nHours ++;
      flasher.flashMe(nHours);
      hourMark = millis();
    }
  }
} hourCounter;

void loop() {
  myFlasher.loop();
  hourCounter.loop();

  // other code goes here
}

This is called encapsulation. Use classes (structs) to break your code into sections, each with a well-defined function.[/code]