Hello everyone I'm new on Arduino,
Actually I have a big problem with my SD card, I ty to do a datalog on a SD card and transfer it on a application via Bluetooth.
When I first started it worked well with an analogic output on A2.
I wanted to change my code to have a second SPI and then datalog from an ADC to the SD card.
When I changed the code and started use SPI library it prints to me : " SD card...... not initializing"
I tried everything I could by loading the old code but nothing works anymore but I know the wiring are correct because I didn't change it and it worked perfectly before!!!
I even tried the cardInfo code but it doesn't recognize my card anymore....
I think the problem comes from the CS which is 10 for my SD card ans 13 for the SPi but even by loading the old code it didn't work so I don't know what to do ...
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
RTC_PCF8523 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
const int chipSelect = 10;
int compteur = 1;
int state;
int t = 1;
int p = 0;
int a = 0;
int k = 1;
int q = 0;
int x = 0;
int y = 0;
int g = 0;
const int spi_ss = 13; // set SPI SS Pin
uint8_t byte_0, byte_1; // First and second bytes read
uint16_t spi_bytes; // final 12 bit shited value
float value; // decimal voltage
float vref = 5.0; // voltage on Vref pin
// Check for user input
char n, inputs[BUFSIZE];
int d = -1;
// Echo received data
while ( ble.available() )
{
int c = ble.read();
d = d+1;
Serial.println(d);
}
if ((t-p)< -20000) {
t = millis();
}
if ((t-p)> 20000) {
p = millis();
}
state = analogRead(5);
if ((t-p)<=3000 ){
if (state > 800) {
t = millis();
}
else {
p = millis();
}
}
if (d == 1) {
analogWrite(0,LOW);
analogWrite(1,LOW);
analogWrite(2,LOW);
k = 1;
}
if ((t-p)>3000 or d == 2 ) {
if (k == 1) {
analogWrite(0,HIGH); //Demande à l’Arduino d’envoyer du courant dans la connexion 13
analogWrite(1,LOW);
analogWrite(2,LOW);
k = 2;
p = millis();
}
}
if ((t-p)>3000 or d == 3 ) {
if (k == 2) {
analogWrite(0,LOW);
analogWrite(1,HIGH);
analogWrite(2,LOW);
k = 3;
p = millis();
}
}
if ((t-p)>3000 or d == 4 ) {
if (k == 3) {
analogWrite(0,LOW);
analogWrite(1,LOW);
analogWrite(2,HIGH);
k = 1;
p = millis();
}
}
if (d == 5) {
SD.remove("datalog.txt");
ble.println("fichier effacé");
}
pinMode(spi_ss, OUTPUT); // Set SPI slave select pin as output
digitalWrite(spi_ss, HIGH); // Make sure spi_ss is held high
SPI.begin(); // begin SPI
digitalWrite(10, LOW);
SPI.beginTransaction(SPISettings(1000, MSBFIRST, SPI_MODE2));
// set speed bit format and clock/data polarity while starting SPI transaction
digitalWrite(spi_ss, LOW);
// write the LTC CS pin low to initiate ADC sample and data transmit
byte_0 = SPI.transfer(0); // read firt 8 bits
byte_1 = SPI.transfer(0); // read second 8 bits
//
digitalWrite(spi_ss, HIGH);
// wite LTC CS pin high to stop LTC from transmitting zeros.
SPI.endTransaction();
// close SPI transaction
spi_bytes = ( byte_0 + byte_1 );
// & B000 inital 3 bits two for HI-Z offset one for null bit by & and shift into spi_bytes
// then we add the remaining byte and shift right to remove bit 12
value = vref * (float(spi_bytes) / 2048.0);
// finaly we recover the true value in volts. 1LSB = vref/2048
//
if ((analogRead(0) > 800 and (t-p)<3000)) {
delay(100);
int val = value;
ble.print(val);
ble.println();
}
if (analogRead(1) > 800) {
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
digitalWrite(spi_ss, LOW);
// make a string for assembling the data to log:
String dataString_0 = "";
String dataString_1 = "";
dataString_0 += String(byte_0);
dataString_1 += String(byte_1);
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString_0);
dataFile.println(dataString_1);
dataFile.close();
// print to the serial port too:
Serial.println(dataString_0 + dataString_1 );
q = q+1;
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}
if (analogRead(2) > 800 and q>100) {
analogWrite(1,LOW);
while (! ble.isConnected() and (t-p)>-10000) {
p = millis();
delay(500);
}
delay(10000);
File dataFile = SD.open("datalog.txt");
// if the file is available, write to it:
if (dataFile) {
DateTime now = rtc.now();
ble.print(now.year(), DEC);
ble.print('/');
ble.print(now.month(), DEC);
ble.print('/');
ble.print(now.day(), DEC);
ble.print(" (");
ble.print(daysOfTheWeek[now.dayOfTheWeek()]);
ble.print(") ");
ble.print(now.hour(), DEC);
ble.print(':');
ble.print(now.minute(), DEC);
ble.print(':');
ble.print(now.second(), DEC);
ble.println();
ble.println("BEGINNING");
ble.println(q);
while (dataFile.available()) {
byte_0 = dataFile.read();
byte_1 = dataFile.read();
spi_bytes = ( byte_0 + byte_1 );
value = vref * (float(spi_bytes) / 2048.0);
Serial.print(value);
ble.print(value);
analogWrite(2, HIGH);
delay(20);
analogWrite(2, LOW);
delay(20);
}
dataFile.close();
ble.println("FIN");
y = millis();
SD.remove("datalog.txt");
q = 0;
}
else {
Serial.println("error opening datalog.txt");
}
}
analogWrite(2,LOW);
}