Hi everyone,
I am working on a part of a project in which I have to store data within a SD card from a bluetooth device. The bluetooth module (nRF8001 Adafruit) and the SD Card are both connected to an Arduino UNO (I have used the Mega also) through the SPI interface.
I know I have to enable and disable the CS or SS (chip select) of each device in order to independently communicate with the master (Arduino).
The issue is that I am trying to add to the original code (Bluetooth code from the Adafruit) the part that theoretically allows to write on the SD card. However, I am not succeding.
I downloaded an app from the Adafruit website to communicate between my phone and the serial monitor of the Arduino. So when I type something on the phone I can see it on the serial monitor and vice versa.
So I would like to able to write on the SD card at the same time that when I send something from the phone to the serial monitor. So far I would like to do that just creating the file on the SD card and write on it the same stuff that are shown on the serial monitor.
The code is as follows:
// This version uses the internal data queing so you can treat it like Serial (kinda)!
#include <SD.h>
#include <SPI.h>
#include "Adafruit_BLE_UART.h"
byte address=0x00;
File myFile;
char c;
// Connect CLK/MISO/MOSI to hardware SPI
// e.g. On UNO & compatible: CLK = 13, MISO = 12, MOSI = 11
#define ADAFRUITBLE_REQ 10
#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);
/**************************************************************************/
/*!
Configure the Arduino and start advertising with the radio
*/
/**************************************************************************/
void setup(void)
{
pinMode(4, OUTPUT); //Digital SS Pin SD Card
pinMode(10, OUTPUT); //Digital SS Pin bluetooth
Serial.begin(9600);
while(!Serial); // Leonardo/Micro should wait for serial init
Serial.println(F("Adafruit Bluefruit Low Energy nRF8001 Print echo demo"));
// BTLEserial.setDeviceName("NEWNAME"); /* 7 characters max! */
BTLEserial.begin();
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
}
}
/**************************************************************************/
/*!
Constantly checks for new events on the nRF8001
*/
/**************************************************************************/
aci_evt_opcode_t laststatus = ACI_EVT_DISCONNECTED;
void loop()
{
// Tell the nRF8001 to do whatever it should be working on.
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()) {
Serial.print("* "); 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);
activationbt(c);
activationsd(c);
sdcard(c);
}
}
}
char activationbt(char value)
//Send value from the Bluetooth module to SPI interface
{
digitalWrite(10, LOW); //It activates the Bluetooth module
SPI.transfer(address);
SPI.transfer(value);
digitalWrite(10, HIGH);
}
char activationsd(char value)
//Send value from the SPI interface to SD Card
{
digitalWrite(4, LOW);
SPI.transfer(address);
SPI.transfer(value);
digitalWrite(4, HIGH);
}
char sdcard(char value)
{
myFile = SD.open("sdcard.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
myFile.println(value);
Serial.println(value);
// close the file:
myFile.close();
Serial.println("done.");
}else {
// if the file didn't open, print an error:
Serial.println("error opening sdcard.txt");
}
}
I was wondering if someone might take a look at the code and help me out.
Any ideas?
Thank you in advance for your help.
Regards.