Good morning,
It’s a good day to learn new stuff. I am currently working on an SD card that is used as datalogger. The idea is as follows. You have a button, 2 leds, a switch and of course an SD card reader.
If the button is pressed, a counter will keep the number of pressures. The time is also saved when the button is pushed. If the data is written, a led will burn.
There is a condition for writing down the data. That is, the switch is ON. The idea behind this is that if the SD card is ready to write and the LED is out, you can switch the switch and safely take the SD card.
This section works completely.
My problem / question is the following. The idea is that you can stop the SD card without having to reset the system. This is not a problem now. I really want to write a part what checked if the switch is ON or the SD card is present and works.
if (digitalRead(SDswich) == 1 && SDtest == 0) {
SD.begin(53);
if (!SD.begin(53)) {
Serial.println("initialization failed!");
return;
}
else {
Serial.println("initialization completed!");
SDtest = 1;
}
}
This last part doesn’t work. When you print it, it will print "initialization failed!".
This is the total code:
/*
Connect the 5V pin to the 5V pin on the Arduino
Connect the GND pin to the GND pin on the Arduino
Connect CLK to pin 52 (SCK)
Connect DO to pin 50 (MISO)
Connect DI to pin 51 (MOSI)
Connect CS to pin 53 (/SS)
*/
#include <SPI.h>
#include <SD.h>
#include <Controllino.h>
File myFile;
#define SDswich CONTROLLINO_A8
#define SDled CONTROLLINO_D7
#define button CONTROLLINO_A7
#define SDcheck CONTROLLINO_D9
int SDstage = 0; //to controle the SD
int SDtest = 0;
int val = 0;
int currentState = 0;
int previousState = 0;
long counter = 0;
int dag;
int maand;
int jaar;
int uur;
int minuut;
int seconde;
void setup() {
Serial.begin(9600);
pinMode(SDswich, INPUT);
pinMode(SDled, OUTPUT);
pinMode(button, INPUT);
Controllino_RTC_init(0);
//Controllino_SetTimeDate(26, 3, 4, 2017, 10, 4, 25); //set a new start values to RTC chip, when you run the program it needs to bij '//'
pinMode(53, OUTPUT);
if (!SD.begin(53)) {
Serial.println("initialization failed!");
return;
}
}
void opslaan() {
//SD.begin(53);
digitalWrite(SDled, HIGH);
myFile = SD.open("datalog.txt", O_CREAT | O_WRITE);
if (myFile) {
myFile.print("counter: ");
myFile.print(counter);
myFile.print("\t");
myFile.print(dag);
myFile.print("/");
myFile.print(maand);
myFile.print("/");
myFile.print(jaar);
myFile.print("\t");
myFile.print(uur);
myFile.print(":");
myFile.print(minuut);
myFile.print(":");
myFile.println(seconde);
myFile.close();
}
}
void loop() {
dag = Controllino_GetDay();
maand = Controllino_GetMonth();
jaar = Controllino_GetYear();
uur = Controllino_GetHour();
minuut = Controllino_GetMinute();
seconde = Controllino_GetSecond();
val = digitalRead(button);
if (val == HIGH) {
currentState = 1;
}
else {
currentState = 0;
}
if (currentState != previousState) {
if (currentState == 1) {
counter = counter + 1;
Serial.print("real counter: ");
Serial.println(counter);
SDstage = 1;
}
previousState = currentState;
delay(50);
}
if (digitalRead(SDswich) == 1 && SDtest == 0) {
SD.begin(53);
if (!SD.begin(53)) {
Serial.println("initialization failed!");
return;
}
else {
Serial.println("initialization completed!");
SDtest = 1;
}
}
if (SDstage == 1 && SDtest == 1) {
opslaan();
SDstage = 0;
}
else {
digitalWrite(SDled, LOW);
SDtest = 0;
}
}
