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.
@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).
// 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.");
// 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;
/* 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;
}
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!
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.
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();
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;
}
/* 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;
}
}