Trying to get an atlas ph sensor(pH Kit | Atlas Scientific) to read simple ph value to Sparkfun microSD (SparkFun Level Shifting microSD Breakout - DEV-13743 - SparkFun Electronics).
I've almost no experience with sd loggers beyond the most rote application of sample sketches. The ph sensor reads fine on a serial monitor, and I've done the sample code that sparkfun provides where one just types on the serial monitor and it logs to sd, so I know the components are fine.
Been trying to synch the two. I get an error code on the function "initializeCard", saying that it wasn't declared, though I"m sure it's not my only error. Here's what I've got so far:
#include <SPI.h>
#include <SD.h>
File fd;
const uint8_t BUFFER_SIZE = 20;
char fileName[] = "pHdatalogger.txt"; // SD library only supports up to 8.3 names
char buff[BUFFER_SIZE+2] = ""; // Added two to allow a 2 char peek for EOF state
uint8_t index = 0;
#include <SoftwareSerial.h> //we have to include the SoftwareSerial library, or else we can't use it
#define rx 2 //define what pin rx is going to be
#define tx 3 //define what pin tx is going to be
const uint8_t chipSelect = 8;
const uint8_t cardDetect = 9;
SoftwareSerial myserial(rx, tx); //define how the soft serial port is going to work
String inputstring = ""; //a string to hold incoming data from the PC
String sensorstring = ""; //a string to hold the data from the Atlas Scientific product
boolean input_string_complete = false; //have we received all the data from the PC
boolean sensor_string_complete = false; //have we received all the data from the Atlas Scientific product
float pH; //used to hold a floating point number that is the pH
void setup() { //set up the hardware
myserial.begin(57600); //set baud rate for the software serial port to 9600
inputstring.reserve(10); //set aside some bytes for receiving data from the PC
sensorstring.reserve(30); //set aside some bytes for receiving data from Atlas Scientific product
Serial.begin(57600);
while (!Serial); // Wait for serial port to connect (ATmega32U4 type PCBAs)
// Note: To satisfy the AVR SPI gods the SD library takes care of setting
// SS_PIN as an output. We don't need to.
pinMode(cardDetect, INPUT);
initializeCard();
}
void serialEvent() { //if the hardware serial port_0 receives a char
inputstring = Serial.readStringUntil(13); //read the string until we see a
input_string_complete = true; //set the flag used to tell if we have received a completed string from the PC
}
void loop() { //here we go...
if (input_string_complete) { //if a string from the PC has been received in its entirety
myserial.print(inputstring); //send that string to the Atlas Scientific product
myserial.print('\r'); //add a to the end of the string
inputstring = ""; //clear the string
input_string_complete = false; //reset the flag used to tell if we have received a completed string from the PC
}
if (myserial.available() > 0) { //if we see that the Atlas Scientific product has sent a character
char inchar = (char)myserial.read(); //get the char we just received
sensorstring += inchar; //add the char to the var called sensorstring
if (inchar == '\r') { //if the incoming character is a
sensor_string_complete = true; //set the flag
}
}
if (sensor_string_complete == true) { //if a string from the Atlas Scientific product has been received in its entirety
Serial.println(sensorstring); //send that string to the PC's serial monitor
/* //uncomment this section to see how to convert the pH reading from a string to a float
if (isdigit(sensorstring[0])) { //if the first character in the string is a digit
pH = sensorstring.toFloat(); //convert the string to a floating point number so it can be evaluated by the Arduino
if (pH >= 7.0) { //if the pH is greater than or equal to 7.0
Serial.println("high"); //print "high" this is demonstrating that the Arduino is evaluating the pH as a number and not as a string
}
if (pH <= 6.999) { //if the pH is less than or equal to 6.999
Serial.println("low"); //print "low" this is demonstrating that the Arduino is evaluating the pH as a number and not as a string
}
}
*/
sensorstring = ""; //clear the string
sensor_string_complete = false; //reset the flag used to tell if we have received a completed string from the Atlas Scientific product
}
PrintToSD();
}
void PrintToSD() // this function saves the data to the SD card
{
File datafile = SD.open("pHdatalogger.txt", FILE_WRITE);
if(datafile)
{
datafile.print(pH);
datafile.print(",");
}
////////////////////////////////////////////////////////////////////////////////
// Do everything from detecting card through opening the demo file
////////////////////////////////////////////////////////////////////////////////
void initializeCard(void)
{
Serial.print(F("Initializing SD card..."));
// Is there even a card?
if (!digitalRead(cardDetect))
{
Serial.println(F("No card detected. Waiting for card."));
while (!digitalRead(cardDetect));
delay(250); // 'Debounce insertion'
}
// Card seems to exist. begin() returns failure
// even if it worked if it's not the first call.
if (!SD.begin(chipSelect) && !alreadyBegan) // begin uses half-speed...
{
Serial.println(F("Initialization failed!"));
initializeCard(); // Possible infinite retry loop is as valid as anything
}
else
{
alreadyBegan = true;
}
Serial.println(F("Initialization done."));
Serial.print(fileName);
if (SD.exists(fileName))
{
Serial.println(F(" exists."));
}
else
{
Serial.println(F(" doesn't exist. Creating."));
}
Serial.print("Opening file: ");
Serial.println(fileName);
Serial.println(F("Enter text to be written to file. 'EOF' will terminate writing."));
}
////////////////////////////////////////////////////////////////////////////////
// This function is called after the EOF command is received. It writes the
// remaining unwritten data to the µSD card, and prints out the full contents
// of the log file.
////////////////////////////////////////////////////////////////////////////////
void eof(void)
{
index -= 3; // Remove EOF from the end
flushBuffer();
// Re-open the file for reading:
fd = SD.open(fileName);
if (fd)
{
Serial.println("");
Serial.print(fileName);
Serial.println(":");
while (fd.available())
{
Serial.write(fd.read());
}
}
else
{
Serial.print("Error opening ");
Serial.println(fileName);
}
fd.close();
}
atlas_ph_to_sd.ino (7.22 KB)