Go Down

Topic: SD card not initialising since I added another SPI (Read 1 time) previous topic - next topic

Kirby56530

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 ...

Code: [Select]

#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


Code: [Select]
// 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);
 
}

Gerihatrick

Could it be a memory issue?
Can you try a Mega?
Stick at it

Geek Emeritus

clarifying CS: every SPI device needs a CS pin, including the device on the Arduino. If you pull the Arduino CS pin low, the Arduino becomes the slave device.

You can use the CS pin for one module. a second module requires its own CS pin. you have to set that pin to pinMode( pinNumber, OUTPUT); and digitalWrite(pinNumber, LOW);  when you communicate with the associated module

If you are using pin 10 for more than one module you have identified your problem.
All PMs will be deleted unopened due to arrogant argumentative pot stirring Malfoys.
 If you have not read "How to use this forum - please read", in particular: "7. If you are posting code or error messages, use "code" tags":  expect rude responses

Kirby56530

Thank you for your answers,
I did it Geek Emeritus, I changed it to 12 then I have 10 for SD card and 12 for SPI ADC...
Nothing worked and my SD card is still blocked..
to clarify it : Chen I want to use SD card I do pinMode( pinSD OUTPUT); and digitalWrite(pinSD, LOW);
and when I want to use ADC I do pinMode( pinADC, OUTPUT); and digitalWrite(pinADC, LOW);?
or do I have to disable the other????

I think it could be a memory problem , a friend told me that also..
Do u have any link to erase memory??

Thank you for advance

Geek Emeritus

Quote
Chen I want to use SD card I do pinMode( pinSD OUTPUT); and digitalWrite(pinSD, LOW);
pinMode(pin10, OUTPUT) & pinMode(pinWhatever, OUTPUT)  once, in setup

SD.h will handle digitalWrite(pinSD, LOW); for you
if you use a library for the ADC, it probably handles that

Quote
think it could be a memory problem , a friend told me that also..
look at the bottom of the IDE after you compile. it will tell you how much memory you have available, have used, and have left.  talking about SRAM, not flash memory

I would try a different card, and a different module, in that order.
All PMs will be deleted unopened due to arrogant argumentative pot stirring Malfoys.
 If you have not read "How to use this forum - please read", in particular: "7. If you are posting code or error messages, use "code" tags":  expect rude responses

Kirby56530

ok I get it for the the CS swich but it doesn't work anymore so I can't test those changes...
Fro the memory I use 81% of the space and 1226 bytes dynamic memory do you think its too much and it could be that??
What do you mean by different card... different SD card or Adafruit microprocessors card? and what different module?? sorry I'm noobie ...

Geek Emeritus

rephrasing:

that which is commonly called the SD card is usually a TF card. The TF card is the little fingernail sized card you put in an adapter. The SD card is the big thumbnail card, or the adapter you put the TF card in. Most so called SD card modules should be called TF card modules.

I would try another TF card, because I have many. Then I would try another SD module, because I have a few

Quote
Fro the memory I use 81% of the space and 1226 bytes dynamic memory do you think its too much and it could be that??
I would set aside the UNO and go for the Mega, using that much memory.
All PMs will be deleted unopened due to arrogant argumentative pot stirring Malfoys.
 If you have not read "How to use this forum - please read", in particular: "7. If you are posting code or error messages, use "code" tags":  expect rude responses

Kirby56530


Kirby56530

Hello,
I've tried many TF and SD module but it doesn't work...
It says that it recognize de card but the format is not correct...
The problem is that I have tried many time to format it FAT32 and it worked before but not anymore..
do you have an idea??
Thank you

Go Up