I am trying to merge 3 separate DataLogger codes in to one. they work fine individually; however I can get all 3 to work together

Viewed 16

I am trying to merge 3 separate DataLogger codes in to one. they work fine individually; however I can get all 3 to work together, can anyone help?

#include <SPI.h> 
#include <SD.h>  
#include "RTClib.h"

RTC_DS1307 rtc;

int sensor = A0;
int sensorInput;
double temp;

int led = 13;                
int sensorMotion = A1;             
int state = LOW;            
int val = 0;  

const char* filename = "DataLogger.csv";

File file;

void setup() {
  Serial.begin(9600);

  #ifndef ESP8266
  while (!Serial); 
#endif

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    abort();
  }

  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running, let's set the time!");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

  }

  pinMode(10, OUTPUT); 

  if (!SD.begin(10)) {
    Serial.println("Error : Push the reset button");
    for (;;); 
   }

  file = SD.open(filename, FILE_WRITE);

  if (file.size() == 0) {
    file.println("Brightness value per seconds");
    file.flush();
  }
}
  DateTime time = rtc.now();

  val = digitalRead(sensor);   // read sensor value
  if (val == HIGH) {           // check if the sensor is HIGH
    digitalWrite(led, HIGH);   // turn LED ON
    delay(500);                // delay 100 milliseconds 

    if (state == LOW) { 
      Serial.println(String("Motion detected 
")+","+time.timestamp(DateTime::TIMESTAMP_TIME)+","+time.timestamp(DateTime::TIMESTAMP_DATE));
      file.println(String("Motion detected 
")+","+time.timestamp(DateTime::TIMESTAMP_TIME)+","+time.timestamp(DateTime::TIMESTAMP_DATE));
      file.flush();
      state = HIGH;       // update variable state to HIGH
    }
  } 
  else {
      digitalWrite(led, LOW); // turn LED OFF
      delay(500);             // delay 200 milliseconds 
  
      if (state == HIGH){
        Serial.println("Motion stopped!");
        state = LOW;       // update variable state to LOW
    }
  }
}
void loop() { 
    measure();
    delay(1000);
 DateTime time = rtc.now();

  sensorInput = analogRead(A0);
  temp = (double)sensorInput / 1024;
  temp = temp * 5;
  temp = temp - 0.5;
  temp = temp * 100;
  delay(1000);

Serial.println(String(temp)+","+time.timestamp(DateTime::TIMESTAMP_TIME)+","+time.timestamp(DateTime::TIMESTAMP_DATE));

file.println(String(temp)+","+time.timestamp(DateTime::TIMESTAMP_TIME)+","+time.timestamp(DateTime::TIMESTAMP_DATE)); file.flush(); }

void measure() {
  DateTime time = rtc.now();
  int lightvalue = analogRead(A0); 

Serial.println(String(lightvalue)+","+time.timestamp(DateTime::TIMESTAMP_TIME)+","+time.timestamp(DateTime::TIMESTAMP_DATE));

file.println(String(lightvalue)+","+time.timestamp(DateTime::TIMESTAMP_TIME)+","+time.timestamp(DateTime::TIMESTAMP_DATE)); file.flush(); }

0 Answers
Related