This is the first part of the code:
/*******************************************SD CARD and Bluetooth*******************************/
#include <SD.h>
#include <SPI.h>
#include "Adafruit_BLE_UART.h"
const uint8_t chipSelect = 10;
// This version uses the internal data queing so you can treat it like Serial (kinda)!
/*********************************BLUETOOTH*****************************************/
#define ADAFRUITBLE_REQ 6
#define ADAFRUITBLE_RDY 2 // This should be an interrupt pin, on Uno thats #2 or #3
#define ADAFRUITBLE_RST 9
Adafruit_BLE_UART BTLEserial = Adafruit_BLE_UART(ADAFRUITBLE_REQ, ADAFRUITBLE_RDY, ADAFRUITBLE_RST);
Then the SD card reading is here:
void setup()
{
Serial.begin(9600);
/*******************************************SD CARD*******************************/
Serial.print(F("Initializing SD card..."));
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(chipSelect, OUTPUT);
//check_sd_card();
// digitalWrite(6, HIGH);
uint8_t sdInitialised;
sdInitialised = SD.begin(10);
Serial.println(F("Output from initialisation:"));
Serial.println(sdInitialised);
if (!sdInitialised) {
Serial.println(F("initialization failed!"));
return;
}
Serial.println(F("initialization done AGAIN."));
write_to_sd_card();
/*********************************BLUETOOTH*****************************************/
// while(!Serial); //
Serial.println(F("Adafruit Bluefruit Low Energy nRF8001 Print echo demo"));
// BTLEserial.setDeviceName("NEWNAME"); /* 7 characters max! */
BTLEserial.begin();
}
/*********************************BLUETOOTH*****************************************/
//aci_evt_opcode_t laststatus = ACI_EVT_DISCONNECTED;
void loop()
{
// Tell the nRF8001 to do whatever it should be working on. give it some time to process.
BTLEserial.pollACI();
// Ask what is our current status
aci_evt_opcode_t status = BTLEserial.getState();
// If the status changed....
if (status != laststatus) {
// print it out!
if (status == ACI_EVT_DEVICE_STARTED) {
Serial.println(F("* Advertising started"));
}
if (status == ACI_EVT_CONNECTED) {
Serial.println(F("* Connected!"));
}
if (status == ACI_EVT_DISCONNECTED) {
Serial.println(F("* Disconnected or advertising timed out"));
}
// OK set the last status change to this one
laststatus = status;
}
if (status == ACI_EVT_CONNECTED) {
// Lets see if there's any data for us!
if (BTLEserial.available()) {
int valueOne = 2;
BTLEserial.print(valueOne); // LOOK AT HOW TO SEND PACKETS OF 20 BYTES.
// only send data if we type something first.
Serial.print(F("* ")); Serial.print(BTLEserial.available()); Serial.println(F(" bytes available from BTLE"));
}
// OK while we still have something to read, get a character and print it out
while (BTLEserial.available()) {
char c = BTLEserial.read();
Serial.print(c);
}
}
}
The functions are:
void check_sd_card(){
Sd2Card card;
SdVolume volume;
SdFile root;
//Serial.print("\nInitializing SD card...");
// we'll use the initialization code from the utility libraries
// since we're just testing if the card is working!
if (!card.init(SPI_HALF_SPEED, chipSelect)) {
Serial.println(F("initialization failed. Things to check:"));
Serial.println(F("* is a card inserted?"));
Serial.println(F("* is your wiring correct?"));
Serial.println(F("* did you change the chipSelect pin to match your shield or module?"));
return;
} else {
Serial.println(F("Wiring is correct and a card is present."));
}
// print the type of card
//Serial.print("\nCard type: ");
switch (card.type()) {
case SD_CARD_TYPE_SD1:
Serial.println(F("SD1"));
break;
case SD_CARD_TYPE_SD2:
Serial.println(F("SD2"));
break;
case SD_CARD_TYPE_SDHC:
Serial.println(F("SDHC"));
break;
default:
Serial.println(F("Unknown"));
}
// Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
if (!volume.init(card)) {
Serial.println(F("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card"));
return;
}
// print the type and size of the first FAT-type volume
uint32_t volumesize;
Serial.print(F("\nVolume type is FAT"));
Serial.println(volume.fatType(), DEC);
Serial.println();
volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
volumesize *= volume.clusterCount(); // we'll have a lot of clusters
volumesize *= 512; // SD card blocks are always 512 bytes
Serial.print(F("Volume size (bytes): "));
Serial.println(volumesize);
Serial.println(F("\nFiles found on the card (name, date and size in bytes): "));
root.openRoot(volume);
// list all files in the card with date and size
root.ls(LS_R | LS_DATE | LS_SIZE);
root.close();
}
void write_to_sd_card(){
File myFile;
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open(F("test.txt"), FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print(F("Writing to test.txt..."));
myFile.println(F("testing 1, 2, 5."));
// close the file:
myFile.close();
Serial.println(F("done."));
} else {
// if the file didn't open, print an error:
Serial.println(F("error opening test.txt"));
}
}
I hope this is OK.
Bee