Hello all.
As part of my project I am trying to store the value obtained from 4 sensors every 30 seconds into individual files on the mini SD card.
I am currently using an Adruino mega attached to the ethernet shield.
How can i set it so that it records the data in the SD card every X seconds?
Any help is greatly appreciated.
Thank you.
This is my function to do so:
void LogData()
{
// make a string for assembling the data to log:
// Temperature
String dataString1 = "";
dataString1 += String(tempRaw);
// p.H.
String dataString2 = "";
dataString2 += String(pHRaw);
// Yeast 1
String dataString3 = "";
dataString3 += String(yeast1Raw);
// Yeast 2
String dataString4 = "";
dataString4 += String(yeast2Raw);
// 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("Temp.txt", FILE_WRITE);
dataFile.print("Temperature = ");
dataFile.println(dataString1);
dataFile.close();
File dataFile1 = SD.open("p.H..txt", FILE_WRITE);
dataFile1.print("pH = ");
dataFile1.println(dataString2);
dataFile1.close();
File dataFile2 = SD.open("Yeast1.txt", FILE_WRITE);
dataFile2.print("Yeast1 = ");
dataFile2.println(dataString3);
dataFile2.close();
File dataFile3 = SD.open("Yeast2.txt", FILE_WRITE);
dataFile2.print("Yeast2 = ");
dataFile2.println(dataString4);
dataFile2.close();
}
And this is my full code:
#include <SD.h>
// On the Ethernet Shield, CS is pin 4.
const int chipSelect = 4;
// Variable for reading the pushbutton status
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int Button;
// Variables for calculated values to be displayed on LCD
float celcius;
float ph;
float oxygen;
float yeast;
// Variables for raw sensor inputs
int tempRaw = 0;
int pHRaw = 0;
int yeast1Raw = 0;
int yeast2Raw = 0;
// Variables for temperature sensor and calculations
const int analogInPin = 1; // Analog input pin that the potentiometer is attached to
int i = 0;
int n;
int sample = 30;
float tempRawArray[30];
float averagetempRaw;
float sumtempRaw;
float referenceVoltage;
float volts;
float voltDivisor;
float millivolts;
float kelvin;
int x;
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
referenceVoltage = 5000;
// initialize the pushbutton pins (24,26,28,30) as inputs:
pinMode(24, INPUT);
pinMode(26, INPUT);
pinMode(30, INPUT);
pinMode(32, INPUT);
// Ethernet Shield Initialization
int analogPin = 2;
Serial.begin(9600);
clearLCD();
}
void loop()
{
buttonStateFunction();
LCDDisplay();
//phSense();
TemperatureSense();
//phSense();
//x++;
//if(x>200){
LogData();
//TempControl();
//}
//x=0;
}
//////////////////////////////////////
// Temperature Sensor Functions
/////////////////////////////////////
void TemperatureSense() {
tempRaw = analogRead(A0);
//Arduino analogReads in a value 0-1024
voltDivisor = 1024/referenceVoltage;
kelvin = millivolts/10;
volts = millivolts / 1000;
celcius = kelvin - 273.15;
}
void TempControl (){
int TempInput = analogRead(A0);
if(TempInput > 560){
digitalWrite(13, HIGH);
}
else
{
digitalWrite(13, LOW);
}
}
//////////////////////////////////////
// p.H. Sensor Functions
/////////////////////////////////////
void phSense(){
pHRaw = analogRead(A1);
}
//////////////////////////////////////
// Conductance Functions
/////////////////////////////////////
void yeast1Sense(){
yeast1Raw = analogRead(A2);
}
//////////////////////////////////////
// Turbidity Sensor Functions
/////////////////////////////////////
void yeast2Sense(){
yeast2Raw = analogRead(A3);
}
//////////////////////////////////////
// Buttons Functions
/////////////////////////////////////
void buttonStateFunction() { //Check which button is being pushed
// Check if buttons have been pushed
int buttonState1 = digitalRead(24);
int buttonState2 = digitalRead(26);
int buttonState3 = digitalRead(30);
int buttonState4 = digitalRead(32);
// Check which button has been pushed and assign a variable to it
if (buttonState1 == HIGH) {
Button = 1;
}
else if(buttonState2 == HIGH) {
Button = 2;
}
else if(buttonState3 == HIGH) {
Button = 3;
}
else if(buttonState4 == HIGH) {
Button = 4;
}
}
//////////////////////////////////////
// Data Logging
/////////////////////////////////////
void LogData()
{
// make a string for assembling the data to log:
// Temperature
String dataString1 = "";
dataString1 += String(tempRaw);
// p.H.
String dataString2 = "";
dataString2 += String(pHRaw);
// Yeast 1
String dataString3 = "";
dataString3 += String(yeast1Raw);
// Yeast 2
String dataString4 = "";
dataString4 += String(yeast2Raw);
// 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("Temp.txt", FILE_WRITE);
dataFile.print("Temperature = ");
dataFile.println(dataString1);
dataFile.close();
File dataFile1 = SD.open("p.H..txt", FILE_WRITE);
dataFile1.print("pH = ");
dataFile1.println(dataString2);
dataFile1.close();
File dataFile2 = SD.open("Yeast1.txt", FILE_WRITE);
dataFile2.print("Yeast1 = ");
dataFile2.println(dataString3);
dataFile2.close();
File dataFile3 = SD.open("Yeast2.txt", FILE_WRITE);
dataFile2.print("Yeast2 = ");
dataFile2.println(dataString4);
dataFile2.close();
}
//////////////////////////////////////
// SerLCD Functions
/////////////////////////////////////
void LCDDisplay() { //Decide what to display on the LCD
switch (Button) {
case 1:
// clearLCD();
selectLineOne();
Serial.print("Temperature ");
selectLineTwo();
Serial.print(tempRaw);
Serial.print(" ");
Serial.print("Celcius ");
break;
case 2:
//clearLCD();
selectLineOne();
Serial.print("p.H. ");
selectLineTwo();
Serial.print(pHRaw);
Serial.print(" ");
break;
case 3:
//clearLCD();
selectLineOne();
Serial.print("Volume Fraction");
selectLineTwo();
Serial.print("Of Yeast");
Serial.print(" ");
Serial.print(yeast1Raw);
Serial.print(" ");
break;
case 4:
selectLineOne();
Serial.print("Volume Fraction");
selectLineTwo();
Serial.print("Of Yeast");
Serial.print(" ");
Serial.print(yeast1Raw);
Serial.print(" ");
break;
default:
selectLineOne();
Serial.print("Please select");
selectLineTwo();
Serial.print("what to display");
delay(4000);
clearLCD();
selectLineOne();
Serial.print("Button 1 for");
selectLineTwo();
Serial.print("Temperature");
delay(3000);
clearLCD();
selectLineOne();
Serial.print("Button 2 for");
selectLineTwo();
Serial.print("p.H. levels");
delay(3000);
clearLCD();
selectLineOne();
Serial.print("Button 3 for");
selectLineTwo();
Serial.print("Oxygen Levels");
delay(3000);
clearLCD();
selectLineOne();
Serial.print("Button 4 for");
selectLineTwo();
Serial.print("Yeast Levels");
delay(3000);
//clearLCD();
break;
}
}
void selectLineOne(){ //puts the cursor at line 0 char 0.
Serial.print(0xFE, BYTE); //command flag
Serial.print(128, BYTE); //position
}
void selectLineTwo(){ //puts the cursor at line 0 char 0.
Serial.print(0xFE, BYTE); //command flag
Serial.print(192, BYTE); //position
}
void goTo(int position) { //position = line 1: 0-15, line 2: 16-31, 31+ defaults back to 0
if (position<16){ Serial.print(0xFE, BYTE); //command flag
Serial.print((position+128), BYTE); //position
}else if (position<32){Serial.print(0xFE, BYTE); //command flag
Serial.print((position+48+128), BYTE); //position
} else { goTo(0); }
}
void clearLCD(){
Serial.print(0xFE, BYTE); //command flag
Serial.print(0x01, BYTE); //clear command.
}