# Function to sense every 30 seconds

Hello everybody. I’m not a native speaker sorry if I write something confuse.My problem is this: I m working with a project that turn on a group of UV lamps in a interval of time (5,10,15,20 min ) ,sense the intensity with 4 uvm30a sensors, and then send this data to a micro SD.
(The arduino receive a code by bluetooth and depending of the code turn on the reles that turn on the lamps). For example:

Arduino receives the code 1205 (that means turn on 12 lamps for 5 minutes).

Enter to the conditional if and turn on the pin 5 for five minutes with millis() function.

My intention is that during this five minutes the sensors sense every 30 seconds the values received and print in the micro SD. (10 values sensed)

The code is working without the intervals of sensing . I tried to create a function named tarjeta() that print in the SD the data, but i dont know how can I execute this function every 30 seconds during the intervals of time (5,10,15 20 minutes).
I hope that you can hellp me.Very thanks, I will be aware.This is the code:

``````unsigned long milliForRel4;
unsigned long milliFor2Rel4;
unsigned long milliFor3Rel4;
unsigned long milliFor4Rel4;

unsigned long milliForRel5;
unsigned long milliFor2Rel5;
unsigned long milliFor3Rel5;
unsigned long milliFor4Rel5;

unsigned long milliForRel6;
unsigned long milliFor2Rel6;
unsigned long milliFor3Rel6;
unsigned long milliFor4Rel6;

unsigned long tiempo1 = 0;
const long intervalo = 30000;

#include<SoftwareSerial.h>
#include<SD.h>
#include<DS1302.h>
SoftwareSerial BT(10, 11);

File myFile;
DS1302 rtc(2, 3, 7);
Time t;
#define KEEPONFOR 300000L
#define KEEPONFOR2 600000L     //intervals of time
#define KEEPONFOR3 900000L
#define KEEPONFOR4 1200000L

void setup() {

Serial.begin(9600);
Serial.print("Iniciando SD ...");
if (!SD.begin(53)) {
Serial.println("No se pudo inicializar");     //Micro SD initialization
return;
}
Serial.println("inicializacion exitosa");

BT.begin(9600);
pinMode(4, OUTPUT);
digitalWrite(4, LOW);
pinMode(5, OUTPUT);
digitalWrite(5, LOW);
pinMode(6, OUTPUT);
digitalWrite(6, LOW);

pinMode(3, INPUT);

milliForRel4 = 0L;
milliFor2Rel4 = 0L;
milliFor3Rel4 = 0L;
milliFor4Rel4 = 0L;     //variables for millis() function

milliForRel5 = 0L;
milliFor2Rel5 = 0L;
milliFor3Rel5 = 0L;
milliFor4Rel5 = 0L;

milliForRel6 = 0L;
milliFor2Rel6 = 0L;
milliFor3Rel6 = 0L;
milliFor4Rel6 = 0L;

}
int valor;

void loop() {

if (BT.available()) {

while (BT.available() > 0) {                              //Receive the code

delay(5);
}

if (cadena[0] == '1' && cadena[1] == '2') {          //condition to turn on the pin 5
Serial.println("prende relevador 2 por 5 min");
digitalWrite(5, HIGH);

milliForRel5 = millis();
tarjeta();                                                  // function to print in the micro SD (with this code it works only one time)

}

Serial.println("prende relevador 2 por 10 min"); // other condition
digitalWrite(5, HIGH);

milliFor2Rel5 = millis();
tarjeta();

}
Serial.println("prende relevador 2 por 15 min");
digitalWrite(5, HIGH);
milliFor3Rel5 = millis();
}
Serial.println("prende relevador 2 por 20 min");
digitalWrite(5, HIGH);
milliFor4Rel5 = millis();
}
}

Serial.println("prende relevador 2 y 3por 5 min");
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);

milliForRel5 = millis();
milliForRel6 = millis();
}
Serial.println("prende relevador 2 y 3 por 10 min");
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
milliFor2Rel5 = millis();
milliFor2Rel6 = millis();
}
Serial.println("prende relevador 2 y 3  por 15 min");
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
milliFor3Rel5 = millis();
milliFor3Rel6 = millis();
}
Serial.println("prende relevador 2 y 3por 20 min");
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);

milliFor4Rel5 = millis();
milliFor4Rel6 = millis();
}
}

Serial.println("prende relevador 1,2,3 por 5 min");
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
milliForRel4 = millis();
milliForRel5 = millis();
milliForRel6 = millis();

}
Serial.println("prende relevador 1,2,3 por 10 min");
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
milliFor2Rel4 = millis();
milliFor2Rel5 = millis();
milliFor2Rel6 = millis();
}
Serial.println("prende relevador 1,2,3 por 15 min");
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
milliFor3Rel4 = millis();
milliFor3Rel5 = millis();
milliFor3Rel6 = millis();
}
Serial.println("prende relevador 1,2,3 por 20 min");
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
milliFor4Rel4 = millis();
milliFor4Rel5 = millis();
milliFor4Rel6 = millis();
}
}
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);

}

}
if (milliForRel4 != 0) {
if (millis() - milliForRel4 >= KEEPONFOR) {
digitalWrite(4, LOW);
milliForRel4 = 0L;
}
}

if (milliFor2Rel4 != 0) {
if (millis() - milliFor2Rel4 >= KEEPONFOR2) {
digitalWrite(4, LOW);
milliFor2Rel4 = 0L;
}
}

if (milliFor3Rel4 != 0) {
if (millis() - milliFor3Rel4 >= KEEPONFOR3) {
digitalWrite(4, LOW);
milliFor3Rel4 = 0L;
}
}

if (milliFor4Rel4 != 0) {
if (millis() - milliFor4Rel4 >= KEEPONFOR4) {
digitalWrite(4, LOW);
milliFor4Rel4 = 0L;
}
}
////////////////////////////////////////////////

if (milliForRel5 != 0) {
if (millis() - milliForRel5 >= KEEPONFOR) {
digitalWrite(5, LOW);
milliForRel5 = 0L;
}
}

if (milliFor2Rel5 != 0) {
if (millis() - milliFor2Rel5 >= KEEPONFOR2) {
digitalWrite(5, LOW);
milliFor2Rel5 = 0L;
}
}

}

void tarjeta() {

unsigned long ahora = millis();

if (ahora - tiempo1 >= intervalo) {

tiempo1 = ahora;

myFile = SD.open("datalog.txt", FILE_WRITE);//abrimos  el archivo
t = rtc.getTime();
if (myFile) {
Serial.print("Escribiendo SD: ");

myFile.print(rtc.getDateStr());
myFile.print("---");
myFile.print(rtc.getTimeStr());
myFile.print("---");
myFile.print(sensor1);
myFile.println("");

myFile.print(rtc.getDateStr());
myFile.print("---");
myFile.print(rtc.getTimeStr());
myFile.print("---");
myFile.print(sensor2);
myFile.println("");

myFile.print(rtc.getDateStr());
myFile.print("---");
myFile.print(rtc.getTimeStr());
myFile.print("---");
myFile.print(sensor3);
myFile.println("");

myFile.print(rtc.getDateStr());
myFile.print("---");
myFile.print(rtc.getTimeStr());
myFile.print("---");

myFile.println(sensor4);

myFile.close(); //cerramos el archivo

} else {
Serial.println("Error al abrir el archivo");
}

}
}
``````
``````  if (!SD.begin(53)) {
``````

It seems you're using a Mega2560, so why are you using that horrible SoftwareSerial if you have 4 hardware serial interfaces:

``````SoftwareSerial BT(10, 11);
``````

You implemented the complete code based on timings with the millis() function. So you can add another such timed action for the sensing. Just add that to your loop.

You should learn to use arrays. Would make your code much smaller and better readable.

You never set the variable ‘tiempol’ so your function won’t work. You need to set it to the beginning time, just like the relay values. You also need to check it every time through the loop in order to get it to execute more than once.
untested:

``````unsigned long milliForRel4;
unsigned long milliFor2Rel4;
unsigned long milliFor3Rel4;
unsigned long milliFor4Rel4;

unsigned long milliForRel5;
unsigned long milliFor2Rel5;
unsigned long milliFor3Rel5;
unsigned long milliFor4Rel5;

unsigned long milliForRel6;
unsigned long milliFor2Rel6;
unsigned long milliFor3Rel6;
unsigned long milliFor4Rel6;

unsigned long tiempo1 = 0;
const long intervalo = 30000;

#include<SoftwareSerial.h>
#include<SD.h>
#include<DS1302.h>
SoftwareSerial BT(10, 11);

File myFile;
DS1302 rtc(2, 3, 7);
Time t;
#define KEEPONFOR 300000L
#define KEEPONFOR2 600000L     //intervals of time
#define KEEPONFOR3 900000L
#define KEEPONFOR4 1200000L

void setup() {

Serial.begin(9600);
Serial.print("Iniciando SD ...");
if (!SD.begin(53)) {
Serial.println("No se pudo inicializar");     //Micro SD initialization
return;
}
Serial.println("inicializacion exitosa");

BT.begin(9600);
pinMode(4, OUTPUT);
digitalWrite(4, LOW);
pinMode(5, OUTPUT);
digitalWrite(5, LOW);
pinMode(6, OUTPUT);
digitalWrite(6, LOW);

pinMode(3, INPUT);

milliForRel4 = 0L;
milliFor2Rel4 = 0L;
milliFor3Rel4 = 0L;
milliFor4Rel4 = 0L;     //variables for millis() function

milliForRel5 = 0L;
milliFor2Rel5 = 0L;
milliFor3Rel5 = 0L;
milliFor4Rel5 = 0L;

milliForRel6 = 0L;
milliFor2Rel6 = 0L;
milliFor3Rel6 = 0L;
milliFor4Rel6 = 0L;

}
int valor;

void loop() {

if ( tiempol > 0 ) {
tarjeta();
}

if (BT.available()) {

while (BT.available() > 0) {                              //Receive the code

delay(5);
}

if (cadena[0] == '1' && cadena[1] == '2') {          //condition to turn on the pin 5
Serial.println("prende relevador 2 por 5 min");
digitalWrite(5, HIGH);

milliForRel5 = millis();
tiempo1 = millis();
//tarjeta();                                                  // function to print in the micro SD (with this code it works only one time)
}

Serial.println("prende relevador 2 por 10 min"); // other condition
digitalWrite(5, HIGH);

milliFor2Rel5 = millis();
tiempo1 = millis();
}
Serial.println("prende relevador 2 por 15 min");
digitalWrite(5, HIGH);
milliFor3Rel5 = millis();
tiempo1 = millis();
}
Serial.println("prende relevador 2 por 20 min");
digitalWrite(5, HIGH);
milliFor4Rel5 = millis();
tiempo1 = millis();
}
}

Serial.println("prende relevador 2 y 3por 5 min");
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);

milliForRel5 = millis();
milliForRel6 = millis();
tiempo1 = millis();
}
Serial.println("prende relevador 2 y 3 por 10 min");
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
milliFor2Rel5 = millis();
milliFor2Rel6 = millis();
tiempo1 = millis();
}
Serial.println("prende relevador 2 y 3  por 15 min");
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
milliFor3Rel5 = millis();
milliFor3Rel6 = millis();
tiempo1 = millis();
}
Serial.println("prende relevador 2 y 3por 20 min");
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);

milliFor4Rel5 = millis();
milliFor4Rel6 = millis();
tiempo1 = millis();
}
}

Serial.println("prende relevador 1,2,3 por 5 min");
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
milliForRel4 = millis();
milliForRel5 = millis();
milliForRel6 = millis();
tiempo1 = millis();
}
Serial.println("prende relevador 1,2,3 por 10 min");
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
milliFor2Rel4 = millis();
milliFor2Rel5 = millis();
milliFor2Rel6 = millis();
tiempo1 = millis();
}
Serial.println("prende relevador 1,2,3 por 15 min");
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
milliFor3Rel4 = millis();
milliFor3Rel5 = millis();
milliFor3Rel6 = millis();
tiempo1 = millis();
}
Serial.println("prende relevador 1,2,3 por 20 min");
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
milliFor4Rel4 = millis();
milliFor4Rel5 = millis();
milliFor4Rel6 = millis();
tiempo1 = millis();
}
}
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
tiempo1 = 0;
}
}
if (milliForRel4 != 0) {
if (millis() - milliForRel4 >= KEEPONFOR) {
digitalWrite(4, LOW);
milliForRel4 = 0L;
tiempo1 = 0;
}
}

if (milliFor2Rel4 != 0) {
if (millis() - milliFor2Rel4 >= KEEPONFOR2) {
digitalWrite(4, LOW);
milliFor2Rel4 = 0L;
tiempo1 = 0;
}
}

if (milliFor3Rel4 != 0) {
if (millis() - milliFor3Rel4 >= KEEPONFOR3) {
digitalWrite(4, LOW);
milliFor3Rel4 = 0L;
tiempo1 = 0;
}
}

if (milliFor4Rel4 != 0) {
if (millis() - milliFor4Rel4 >= KEEPONFOR4) {
digitalWrite(4, LOW);
milliFor4Rel4 = 0L;
tiempo1 = 0;
}
}
////////////////////////////////////////////////

if (milliForRel5 != 0) {
if (millis() - milliForRel5 >= KEEPONFOR) {
digitalWrite(5, LOW);
milliForRel5 = 0L;
tiempo1 = 0;
}
}

if (milliFor2Rel5 != 0) {
if (millis() - milliFor2Rel5 >= KEEPONFOR2) {
digitalWrite(5, LOW);
milliFor2Rel5 = 0L;
tiempo1 = 0;
}
}
}

void tarjeta() {

unsigned long ahora = millis();

if (ahora - tiempo1 >= intervalo) {
tiempo1 = ahora;

myFile = SD.open("datalog.txt", FILE_WRITE);//abrimos  el archivo
t = rtc.getTime();
if (myFile) {
Serial.print("Escribiendo SD: ");

myFile.print(rtc.getDateStr());
myFile.print("---");
myFile.print(rtc.getTimeStr());
myFile.print("---");
myFile.print(sensor1);
myFile.println("");

myFile.print(rtc.getDateStr());
myFile.print("---");
myFile.print(rtc.getTimeStr());
myFile.print("---");
myFile.print(sensor2);
myFile.println("");

myFile.print(rtc.getDateStr());
myFile.print("---");
myFile.print(rtc.getTimeStr());
myFile.print("---");
myFile.print(sensor3);
myFile.println("");

myFile.print(rtc.getDateStr());
myFile.print("---");
myFile.print(rtc.getTimeStr());
myFile.print("---");

myFile.println(sensor4);

myFile.close(); //cerramos el archivo
} else {
Serial.println("Error al abrir el archivo");
}
}
}
``````

Have a look at the demo Several Things at a Time and how it blinks an LED. You can use the same technique (with suitable timing values) to manage your sensor readings.

The technique is much the same as what you are already using.

...R

@Pylon You' re right I m using Arduino Mega 2560.Im going to change to the communication ports, previously was working with Arduino uno and i havent see that mistake. Im a begineer in Arduino I dont know how to use arrays in this case but I promise study more about it. Thanks for your recomendations. :)

@blh64 I tried with your code and finally it works I m really gracefull with you, i couldnt see that important mistakes with the variable tiempo1. Thank you :) :)