0

when I use the sd card on the mega or the arduino uno it works. Everything is fine. However, when I use it in combination with the USB shield host it doesn't work. I know the usb shield host works because the LCD display works and the barcode reader works in it. It is not the sd card because the sd card works with the boards alone. It is only when I add the shield that there is a connection issue.

I get the error: Initialisation failed in the serial monitor. I tried writing it high but that didn't work.

Any ideas?

/////////////////////////////////////////////////////////////////
//            Arduino Simple Data Logger             v1.03     //
//         Get the latest version of the code here:            //
//         http://educ8s.tv/arduino-data-logger-project


//  RTC: scl-scl pin, sda-sda pin,vcc-5v, gnd-gnd pin
//    SD: vcc-5v, grnd-grnd, mosi-pin 11,miso-pin 12,sck-13 pin, CS(chip select)-10
//  change to csv if you want to open in excel. the log file.

/////////////////////////////////////////////////////////////////

#include <Wire.h>
#include "ds3231.h"
#include <SD.h>
#include <SPI.h>
#include <stdint.h> // add this and the following 2 lines

#define BUFF_MAX 128
#define DS3231_INTCN 0x4
#define DS3231_A1IE 0x1
#define DS3231_A2IE 0x2

int CS_PIN = 4;

File file;

String timeString;
String dateString;

int entryId = 0;

uint8_t time[8];
char recv[BUFF_MAX];
unsigned int recv_size = 0;
unsigned long prev = 0, interval = 1000;

void setup()
{

    Serial.begin(9600);
    Wire.begin();

    initializeSD();

    DS3231_init(DS3231_INTCN);
    memset(recv, 0, BUFF_MAX);

    parse_cmd("T302410401112018",16); //Set time
}

void loop()
{
    String logEntry;
    char in;
    char buff[BUFF_MAX];
    unsigned long now = millis();

    // Log data once in a while
    if ((now - prev > interval) && (Serial.available() <= 0)) {
        getTime();
        logEntry = createLogEntry();
        writeEntryToFile(logEntry);
        prev = now;
    }

    if (Serial.available() > 0) {
        in = Serial.read();

        if ((in == 10 || in == 13) && (recv_size > 0)) {
            parse_cmd(recv, recv_size);
            recv_size = 0;
            recv[0] = 0;
        } else if (in < 48 || in > 122) {
            ;       // ignore ~[0-9A-Za-z]
        } else if (recv_size > BUFF_MAX - 2) {   // drop lines that are too long
            // drop
            recv_size = 0;
            recv[0] = 0;
        } else if (recv_size < BUFF_MAX - 2) {
            recv[recv_size] = in;
            recv[recv_size + 1] = 0;
            recv_size += 1;
        }

    }
}

void parse_cmd(char *cmd, int cmdsize)
{
    uint8_t i;
    uint8_t reg_val;
    char buff[BUFF_MAX];
    struct ts t;

    // TssmmhhWDDMMYYYY aka set time
    if (cmd[0] == 84 && cmdsize == 16) {
        //T355720619112011
        t.sec = inp2toi(cmd, 1);
        t.min = inp2toi(cmd, 3);
        t.hour = inp2toi(cmd, 5);
        t.wday = inp2toi(cmd, 7);
        t.mday = inp2toi(cmd, 8);
        t.mon = inp2toi(cmd, 10);
        t.year = inp2toi(cmd, 12) * 100 + inp2toi(cmd, 14);
        DS3231_set(t);
        //Serial.println("OK");
    } else if (cmd[0] == 49 && cmdsize == 1) {  // "1" get alarm 1
        DS3231_get_a1(&buff[0], 59);
        //Serial.println(buff);
    } else if (cmd[0] == 50 && cmdsize == 1) {  // "2" get alarm 1
        DS3231_get_a2(&buff[0], 59);
        // Serial.println(buff);
    } else if (cmd[0] == 51 && cmdsize == 1) {  // "3" get aging register
        //Serial.print("aging reg is ");
        //Serial.println(DS3231_get_aging(), DEC);
    } else if (cmd[0] == 65 && cmdsize == 9) {  // "A" set alarm 1
        DS3231_set_creg(DS3231_INTCN | DS3231_A1IE);
        //ASSMMHHDD
        for (i = 0; i < 4; i++) {
            time[i] = (cmd[2 * i + 1] - 48) * 10 + cmd[2 * i + 2] - 48; // ss, mm, hh, dd
        }
        byte flags[5] = { 0, 0, 0, 0, 0 };
        DS3231_set_a1(time[0], time[1], time[2], time[3], flags);
        DS3231_get_a1(&buff[0], 59);
        // Serial.println(buff);
    } else if (cmd[0] == 66 && cmdsize == 7) {  // "B" Set Alarm 2
        DS3231_set_creg(DS3231_INTCN | DS3231_A2IE);
        //BMMHHDD
        for (i = 0; i < 4; i++) {
            time[i] = (cmd[2 * i + 1] - 48) * 10 + cmd[2 * i + 2] - 48; // mm, hh, dd
        }
        byte flags[5] = { 0, 0, 0, 0 };
        DS3231_set_a2(time[0], time[1], time[2], flags);
        DS3231_get_a2(&buff[0], 59);
        //Serial.println(buff);

    } else if (cmd[0] == 68 && cmdsize == 1) {  // "D" - reset status register alarm flags
        reg_val = DS3231_get_sreg();
        reg_val &= B11111100;
        DS3231_set_sreg(reg_val);
    } else if (cmd[0] == 70 && cmdsize == 1) {  // "F" - custom fct
        reg_val = DS3231_get_addr(0x5);
        // Serial.print("orig ");
        //Serial.print(reg_val,DEC);
        //Serial.print("month is ");
        //Serial.println(bcdtodec(reg_val & 0x1F),DEC);
    } else if (cmd[0] == 71 && cmdsize == 1) {  // "G" - set aging status register
        DS3231_set_aging(0);
    }
}


void getTime()
{
    String minute;
    String hour;
    struct ts t;

    DS3231_get(&t);

    if(t.min<10)
    {
        minute = "0"+String(t.min);
    } else
    {
        minute = String(t.min);
    }

    timeString = String(t.hour)+":"+minute;
    dateString = String(t.mon)+"/"+t.mday;
}

String createLogEntry()
{
    String logEntry;
    entryId ++;
    logEntry = String(entryId)+","+dateString+","+timeString;
    return logEntry;
}

void writeEntryToFile(String entry)
{
    openFileToWrite("Olirians.csv");
    Serial.println(entry);
    writeToFile(entry);
    closeFile();
}

void initializeSD()
{
    pinMode(CS_PIN, OUTPUT);

    if (SD.begin())
    {
    } else
    {
        return;
    }
}

int openFileToWrite(char filename[])
{
    file = SD.open(filename, FILE_WRITE);

    if (file)
    {
        return 1;
    } else
    {
        return 0;
    }
}

int writeToFile(String text)
{
    if (file)
    {
        file.println(text);
        return 1;
    } else
    {
        return 0;
    }
}

void closeFile()
{
    if (file)
    {
        file.close();
    }
}

closed as unclear what you're asking by Juraj, sempaiscuba, VE7JRO, Greenonline, MatsK Nov 6 '18 at 20:10

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

2

USB Host Shield uses pin 10 as CS. Use pin 4 as CS for the SD module.

To set CS pin to SD library, set it as parameter SD.begin:

SD.begin(CS_PIN);

Note: Level conversion IC on some cheap SD modules disturb the SPI bus and the module can't be used with other SPI devices.

  • Do you mean put CS in pin 4 of the usb shield? I did put the sd module in pin 10 and tried it in pin 4 of the shield – Kayaway Oct 31 '18 at 16:35
  • the shield has no pin 4. it is the pin of the Uno under the shield – Juraj Oct 31 '18 at 16:40
  • test the SD module again solo with pin 4 as CS. set the pin 4 in sketch SD.begin(4); – Juraj Oct 31 '18 at 16:46
  • That didn't work. I will upload my code – Kayaway Oct 31 '18 at 16:57
  • use pin 4 as CS. SD.begin(4) or SD.begin(SD_CS_PIN) – Juraj Oct 31 '18 at 17:22
0

Both the USB host shield and the SD card use the SPI interface - Are you having them on different chip selects? The Host shield uses pin 10 as CS.

  • No, I'm putting the usb module on pin 10 – Kayaway Oct 31 '18 at 16:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.