Hello, I am trying to take data from a sensor and save it to a SD card, with the option to connect via Bluetooth on my phone. I have gotten all the the modules to work separately but I cannot get them to work together.
I can read the data and send it over a bluetooth connection with this code but then nothing is saved to the SD card:
#include <SoftwareSerial.h>// import the serial library
#include <SPI.h>
#include <SD.h>
SoftwareSerial Genotronex(10, 9); // RX, TX
int BluetoothData; // the data given from Computer
const int triggerPin = 5;
const int echoPin = 6;
const int chipSelect = 4;
void setup() {
Genotronex.begin(9600);
Genotronex.println("Will send data ..");
// // make sure that the default chip select pin is set to
// // output, even if you don't use it:
// SD.begin(chipSelect);
}
void loop() {
long duration, feet, inches, cm;
pinMode(triggerPin, OUTPUT);
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(5);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// convert the time into a distance
cm = duration / 29 / 2;
const byte nVals = 1; //# of data variables you have
int var1 = cm; //change to a data variable from sensor
int data[nVals] = {var1};
String label = "Var";
//its a loop because this was originally sending more than on variable
if (Genotronex.available()) {
for (int i = 0; i < nVals; i++) {
String stuff = label + i + ": ";
Genotronex.print(stuff + data[i]);
}
Genotronex.println("");
}
File dataFile = SD.open("DATALOG.txt", FILE_WRITE); //name of file to save data to
//its a loop because this was originally saving more than on variable
if (dataFile) {
for (int i = 0; i < nVals; i++) {
String stuff = label + i + ": ";
dataFile.print(stuff + data[i]);
}
dataFile.println("");
dataFile.close();
}
delay(1000);// prepare for next data ...
}
But when I uncommentSD.begin(chipSelect);
It saves to the SD card but the only thing I get over the Bluetooth connection is "Will send data .."
I am using SD Card Writer and Bluetooth module
I am using an Arduino Uno and as far as wiring I have
Bluetooth Module
TXD >> 10
RXD >> 9
SD Card Writer
CS >> 4
MOSI >> 11
SCK >> 13
MISO >> 12
In the example for SD Card Datalogger it has pinMode(10, OUTPUT);
But I wasn't sure if I should include that if I'm using it with SoftwareSerial Genotronex(10, 9);
It looks like SD.begin() is causing the problem but I don't know why. Could someone help me figure out what is causing this?
Thank you,
Tyler