Il codice ha diversi commenti, sono tutte le varie prove che sto facendo :-[
#include <SPI.h>
#include <SD.h>
#include <SoftwareSerial.h>
///storage/sdcard0/Bluetooth
// dichiarazione variabili accelerometro
int pinX = A0;
int pinY = A1;
int pinZ = A2;
int indice;
int X;
int Y;
int Z;
float x;
float y;
float z;
int noiseX = 0;
int noiseY = 0;
int noiseZ = 0;
// settaggi per bluetooth
int rxPin = 2;
int txPin = 3;
SoftwareSerial bluetooth(rxPin, txPin);
// settaggio per SD
const int chipSelect = 10; // CS su pin 10
//settato a 1 per richiedere password ed avviare la scrittura (alla prima accensione)
int sw = 1;
File dataFile;
// menu
String menu = "Insert: \n 0 for reading SD \n 1 for deleting SD \n 2 for writing SD";
// password per settaggio bluetooth
String passwd = "Tommaso2011";
// setto il delay
int tm = 1000;
// contatore di secondi da accensione
//int sec = 0;
//int sec = millis(); impostato qui sec è sempre uguale a zero
// tempo da quando acceso arduino
int sec = 0;
void setup(){
// SERIAL MONITOR
Serial.begin(9600);
//Serial.println("Letture Accelerometro");
analogReference(EXTERNAL);
//Serial.println("Inserisci la password seguita dal simbolo @ per avviare il programma");
Serial.println("Insert the password, followed by @, for starting the software");
// SDCard
//Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed or not present");
// don't do anything more:
return;
}
// BLUETOOTH
bluetooth.begin(9600);
switch (sw) {
case '0':
//bluetooth.println("\nWriting...");
//bluetooth.println("0");
break;
case '1':
//bluetooth.println("Insert the password, followed by @, for starting the software");
//bluetooth.println("1");
break;
case '2':
//bluetooth.println("Error opening datalog.log.");
//bluetooth.println("2");
break;
case '3':
//bluetooth.println("File removed.");
//bluetooth.println("3");
break;
case '4':
//bluetooth.println("\nNone file to remove.");
//bluetooth.println("4");
break;
case '5':
//bluetooth.println("End reading datalog.log.");
//bluetooth.println("5");
break;
case '6':
//bluetooth.println("Sorry, bad password.");
//bluetooth.println("6");
break;
case '7':
//bluetooth.println("Reading datalog.log...");
//bluetooth.println("7");
break;
//default: bluetooth.println(sw);
}
//bluetooth.println("Insert the password, followed by @, for starting the software");
// PASSWORD
int passWD();
}
void loop() {
if (sw != 7) {
bluetooth.println(sw);
}
// configuro password per collegamento al bluetooth
if (sw == 1) { //righe 62 e 157
sw = passWD();
} else if (sw == 0) { //riga 157
// make a string for assembling the data to log:
String dataStringForBluetooth = "";
// read three sensors and append to the string:
X = analogRead(pinX);
dataStringForBluetooth += String(X);
Y = analogRead(pinY);
dataStringForBluetooth += " ";
dataStringForBluetooth += String(Y);
Z = analogRead(pinZ);
dataStringForBluetooth += " ";
dataStringForBluetooth += String(Z);
// contatore di secondi da accensione
dataStringForBluetooth += " ";
dataStringForBluetooth += String(sec);
// 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.log", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataStringForBluetooth);
//dataFile.println(dataString, BIN);
dataFile.close();
Serial.println(dataStringForBluetooth);
}
// if the file isn't open, pop up an error:
else {
Serial.println("\nError opening datalog.log.");
sw = 2;
// ATTENZIONE: mettere in app la condizione che il dato ricevuto (tipo byte quindi bisogna vedere la corrispondenza ASCII) per essere scritto su file deve essere compreso tra 48 e 57 inclusi, oppure uguale a 10, 13, 32 altrimenti deve essere visualizzato nel campo textreceived
}
}
if (bluetooth.available() > 0) {
//if (Serial.available() > 0) {
// visualizzo il menu
Serial.println(menu);
//bluetooth.println(menu);
//byte command = Serial.read();
byte command = bluetooth.read();
//Serial.write(command);
switch (command) { //controllo che sia un comando valido
case '0': // trasferimento/lettura file
sw = 7;
dataFile = SD.open("datalog.log"); //Riapro il file
if (dataFile) {//Se il file è stato aperto correttamente
Serial.println("\nReading datalog.log...");
//bluetooth.println("\nReading datalog.log...");
//tm = 10000;
//bluetooth.flush();
bluetooth.println("#");
//delay(1000);
//String stringXYZ = "";
while (dataFile.available()) {
//Serial.write(dataFile.read()); //deve essere commentato altrimenti i dati che arrivano al cell non sono corretti
bluetooth.println(char(dataFile.read())); // trasmetto le righe del file tramite bluetooth
//stringXYZ += char(dataFile.read()); // non si possono inviare in quanto i tempi di lettura del file possono essere superiori ai tempi del loop, più che altro la lunghezza del messaggio
}
//bluetooth.println(stringXYZ);
bluetooth.println("|");
//bluetooth.flush();
//delay(1000);
//tm = 1000;
Serial.println("End reading datalog.log.");
sw = 5;
dataFile.close();
}
else {
Serial.println("\nError in opening datalog.log");
sw = 2;
}
break;
case '1': // eliminazione file
// Check to see if the file exists:
if (SD.exists("datalog.log")) {
// delete file:
Serial.println("\nRemoving datalog.log...");
//bluetooth.println("\nRemoving datalog.log...");
SD.remove("datalog.log");
Serial.println("File removed.");
sw = 3;
//bluetooth.println("File removed.");
} else {
Serial.println("\nNone file to remove.");
sw = 4;
//bluetooth.println("\nNone file to remove.");
}
break;
case '2': // eliminazione file
sw = 0; // riattivo la scrittura su SD
Serial.println("\nWriting...");
//bluetooth.println("\nWriting...");
break;
}
}
// } riga 62
sec++;
delay(tm); // il bluetooth riceve un comando al secondo
} // fine loop
int passWD() { // richiede la password, se ok avvia la scrittura (per il primo avvio)
String stringApp = "";
if (bluetooth.available()) {
//if (Serial.available()) {
byte command;
do {
command = bluetooth.read();
//command = Serial.read();
if (command != 255 and command != 64) {
//Serial.println(char(command));
stringApp += char(command);
}
} while (command != '@');
//bluetooth.println(stringApp); solo per leggere la password inserita
//Serial.println(stringApp);
if (stringApp == passwd) {
sw = 0; // se la password corrisponde avvia la scrittura su SD
} else {
bluetooth.write("\nSorry, bad password.");
Serial.write("\nSorry, bad password.");
sw = 6;
}
}
return sw;
}