Hello
My first project with Arduino is: Store data on micro SD card. I'm using MFRC RFID,RTC 1307 clock module,SD
card module on Ethernet shield and push buttons te start an event.My code is running ok when I have connected Mega 2650 via USB port to Arduino IDE and starting Serial monitor,but when is my hardwear connected to external 9 V supply only setup routine is done ,loop (reading push button,readingTAG,time and
storing DATA on SD card) is stoped. SD card is initalized properly than happend nothing. Any sugestions?
My code:
//OK!!!!
// Verzija MEGA 2650 z ethernet sildom
//Zapis tipke na sd s časom,
//Uporabljaj najbolj enostaven SD modul
// Za tipke uporablja črke
// A=1L,B=1,5L,C="L,D=0,5Kg skute;E=1kg skute,......
//###################################################################################
#include "OneButton.h"
#include <SPI.h>
#include <SD.h> // SD card libray
#include <Wire.h>// I2
#include <Time.h> // Time Manipulation
#include <DS1307RTC.h> // DS1307 RTC
#include <MFRC522.h>
char timedatebuf[65]; // Time and Date string buffer
int year4digit; // 4 digit year
#define RST_PIN 49 // RFID reset pin
#define SS_PIN 53 //RFID SDA pin
#define sdPin 4 // SD CS pin
File dataFile;
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
OneButton buttonA(3, true); //true=PULLUP
OneButton buttonB(5, true);
void setup() {
Serial.begin(9600);
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
buttonA.attachLongPressStart(longPressStartA);
buttonB.attachLongPressStart(longPressStartB);
buttonA.setPressTicks(400);
buttonB.setPressTicks(400);
SPI.begin(); // Init SPI bus
Serial.print("Initializing SD card...");
pinMode(4, OUTPUT);
if (!SD.begin(4)) { // check if card is installed
Serial.println("No SD Card present in module");
digitalWrite(7, HIGH); // turn the LED on for visual information)
delay(3000);
digitalWrite(7, LOW);
return;
}
Serial.println("SD Card Ready");
Serial.println("EVIDENCA PORABE MLEKA");
mfrc522.PCD_Init(); // Init MFRC
}
void loop() {
buttonA.tick();
buttonB.tick();
}//end
//.............................................................................................
void longPressStartA() {
tmElements_t tm;
if (RTC.read(tm)) { // Get Time/Date from RTC1307
year4digit = tm.Year + 1970; // 4 digit year variable
// Format Time & Date string in timedatebuf
sprintf(timedatebuf, "Dat: %02d/%02d/%02d U: %02d:%02d:%02d ", tm.Day, tm.Month, year4digit, tm.Hour, tm.Minute, tm.Second );
if ( ! mfrc522.PICC_IsNewCardPresent()) { // Look for new cards
return;
}
if ( ! mfrc522.PICC_ReadCardSerial()) { // Select one of the cards
return;
}
String UIDString = "";
for (byte i = 0; i < mfrc522.uid.size; i++) {
UIDString = UIDString + String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
UIDString = UIDString + String(mfrc522.uid.uidByte[i], HEX);
}
mfrc522.PCD_StopCrypto1();
delay(10);
String L1 = "A " ;
String dataString = "," + String(UIDString) + "," + String(L1);
File dataFile = SD.open("MLEKO.csv", FILE_WRITE); // Open or Create file
if (dataFile) { // Check if file exist on SD Card
dataFile.print(timedatebuf);
dataFile.println(dataString);
dataFile.close(); // Close file
Serial.println("...................");
Serial.print(timedatebuf);
Serial.println(dataString);
}
else {
Serial.println("error opening mleko1.csv"); // if file not on SD Card
}
}
}
//,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
void longPressStartB() {
tmElements_t tm;
if (RTC.read(tm)) { // Get Time/Date from RTC1307
year4digit = tm.Year + 1970; // 4 digit year variable
// Format Time & Date string in timedatebuf
sprintf(timedatebuf, "Dat: %02d/%02d/%02d U: %02d:%02d:%02d ", tm.Day, tm.Month, year4digit, tm.Hour, tm.Minute, tm.Second );
if ( ! mfrc522.PICC_IsNewCardPresent()) { // Look for new cards
return;
}
if ( ! mfrc522.PICC_ReadCardSerial()) { // Select one of the cards
return;
}
String UIDString = "";
for (byte i = 0; i < mfrc522.uid.size; i++) {
UIDString = UIDString + String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
UIDString = UIDString + String(mfrc522.uid.uidByte[i], HEX);
}
mfrc522.PCD_StopCrypto1();
delay(10);
String L1 = "B " ;
String dataString = "," + String(UIDString) + "," + String(L1) ;
File dataFile = SD.open("MLEKO.csv", FILE_WRITE); // Open or Create file
if (dataFile) { // Check if file exist on SD Card
dataFile.print(timedatebuf);
dataFile.println(dataString);
dataFile.close(); // Close file
Serial.println("...................");
Serial.print(timedatebuf);
Serial.println(dataString);
}
else {
Serial.println("error opening mleko1.csv"); // if file not on SD Card
}
}
}