Arduino with adafruit data logger shield

I have a simple project to make. The job is to time record 5 event data from various discrete site into micro sd card of data logger shield. Whenever any of the below event happens, the event should be recorded in sd card.

  1. door(A) opening,
  2. door (A) Closing,
  3. Door (B) opening ,
  4. Door (B) closing,
  5. Push button pressed

If any expert guides me it will be of much help

@sunilrathaur, your topic has been moved to a more suitable location on the forum.

Have a look at the state change detection example that comes with the IDE for 1/2 and 3/4. For 5, does that mean "button is pressed" or "button becomes pressed"?

Further have a look at the examples that come with the SD or SdFat library (whichever one you use) to understand how you can write data to a SD card.

If the time that you want to record is relative to the bootup time of the Arduino, you can use millis() as a reference. If you need the time to be absolute, you will need an accurate source (RTC, NTP server).

What hardware do you have, and have you made any attempts at sensing the events you’re interested in ?

@sterretje has raised some starting ideas for you to consider.

Oh great! Thanks.
now looking forward to use the State Change Detection (Edge Detection) for pushbuttons
thanks

/*
State change detection (edge detection)
*/

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

File myFile;

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

// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to

// Variables will change:
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
const int chipSelect = 10;

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

Serial.print("Initializing SD card...");

if (!SD.begin(10)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");

// open a new file and immediately close it:

// Check to see if the file exists:

}

void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);

// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
Serial.println("on");
myFile = SD.open("logtest.txt", FILE_WRITE);
myFile.println("buttonstate high");
myFile.close();

     } else {
 
}
// Delay a little bit to avoid bouncing
delay(500);

}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;

}

codes made with one input and its recording in sd card under testlog file.

Now trying to raise from one input logging to 5 input logging


prototype ready with one push button logging

OK now you need to go and read the instructions, then go back and edit post #5 (we seem to have the numbers working again - yay! :pleading_face:) to fix your code.

/*   State change detection (edge detection) */

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

File myFile;

// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to

// Variables will change:
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
const int chipSelect = 10;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.print("Initializing SD card...");
  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");
}

void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      Serial.println("on");
      myFile = SD.open("logtest.txt", FILE_WRITE);
      myFile.println("buttonstate high");
      myFile.close();
    } else {
    }
    // Delay a little bit to avoid bouncing
    delay(500);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;
}

auto-formatting is done and repeating codes removed.

This prototype is working fine with one input of pushbutton and also logging it with rtc time stamp in sd card.
Now only portion left is to increase inputs from 1 to 5. I require guidance here!

Thanks for the help.

After 2 , 3 days the rtc data starts logging absurds dates, time
33-38-2032 31:33:00 instead of 4-18-2022 13:39:56
What to do now?

Well since the sketch you posted in #10 3 weeks ago has no rtc-related code in it (afaics) start by catching us up with that code. Also a diagram showing the connection, and say what kind of rtc it is.

/* State change detection and print_time() in format mm-dd-yyyy hh:mm:ss
*/

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

RTC_DS1307 rtc;
const int chipSelect = 10;
File myFile;
const int buttonPin = 2;
const int LedPin1 = 3;
const int LedPin2 = 4;
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int Led_builtin = 0; // fault indication via Led13

String print_time(DateTime timestamp) {
char message[120];
int Year = timestamp.year();
int Month = timestamp.month();
int Day = timestamp.day();
int Hour = timestamp.hour();
int Minute = timestamp.minute();
int Second = timestamp.second();

sprintf(message, "%d-%d-%d %02d:%02d:%02d", Month, Day, Year, Hour, Minute, Second);
return message;
}

void setup() {
Serial.begin(9600);
pinMode(Led_builtin, 0);
pinMode(10, OUTPUT);
pinMode(chipSelect, OUTPUT);
if (!SD.begin(chipSelect)) {
Serial.println("Error: SD card would not initiate.");
digitalWrite(LedPin1, HIGH);
}
rtc.begin();
if (!rtc.isrunning()) {
Serial.println("Clock is not running");
}
myFile = SD.open("logtest.csv", FILE_WRITE);
if (!myFile) {
Serial.println("Could not open file.");
}
}

void loop() {
DateTime now = rtc.now();
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
myFile = SD.open("logtest.csv", FILE_WRITE);
myFile.println(print_time(now));
myFile.print(" Hearth Fwd SV ON ");
Serial.println(print_time(now));
Serial.print(" Hearth Fwd SV ON ");
myFile.close();
digitalWrite(LedPin2, HIGH);
} else {
}
// Delay a little bit to avoid bouncing
delay(500);
digitalWrite(LedPin2, LOW);
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
}

}

Hmmmm, you need to read the instructions! :face_with_raised_eyebrow:

/* State change detection and print_time() in format mm-dd-yyyy hh:mm:ss
*/
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"


RTC_DS1307 rtc;
const int chipSelect = 10;
File myFile;
const int  buttonPin = 2;
const int  LedPin1 = 3;
const int  LedPin2 = 4;
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
int Led_builtin = 0;         // fault indication  via Led13


String print_time(DateTime timestamp) {
  char message[120];
  int Year = timestamp.year();
  int Month = timestamp.month();
  int Day = timestamp.day();
  int Hour = timestamp.hour();
  int Minute = timestamp.minute();
  int Second = timestamp.second();

  sprintf(message, "%d-%d-%d %02d:%02d:%02d", Month, Day, Year, Hour, Minute, Second);
  return message;
}

void setup() {
  Serial.begin(9600);
  pinMode(Led_builtin, 0);
  pinMode(10, OUTPUT);
  pinMode(chipSelect, OUTPUT);
  if (!SD.begin(chipSelect)) {
    Serial.println("Error: SD card would not initiate.");
    digitalWrite(LedPin1, HIGH);
  }
  Wire.begin();
  rtc.begin();
  if (!rtc.isrunning()) {
    Serial.println("Clock is not running");
  }
  myFile = SD.open("logtest.csv", FILE_WRITE);
  if (!myFile) {
    Serial.println("Could not open file.");
  }
}

void loop() {
  DateTime now = rtc.now();
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      myFile = SD.open("logtest.csv", FILE_WRITE);
      myFile.println(print_time(now));
      myFile.print(" Hearth Fwd SV ON    ");
      Serial.println(print_time(now));
      Serial.print(" Hearth Fwd SV ON    ");
      myFile.close();
      digitalWrite(LedPin2, HIGH);
    } else {
    }
    // Delay a little bit to avoid bouncing
    delay(500);
    digitalWrite(LedPin2, LOW);
    // save the current state as the last state, for next time through the loop
    lastButtonState = buttonState;
  }

}

Sir,
Maybe I forgot to enter Wire.h ?

thanks
Sunil

hi trying new circuit with opamp

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.