So I've been trying to log my time, however, I keep getting weird characters in my excel sheet but it looks fine on the serial monitor how do I fix this?
#include "SoftwareSerial.h"
#include "SD.h"
#include "Wire.h"
SoftwareSerial K_30_Serial(4,5); //Sets up a virtual serial port
//Using pin 5 for Rx and pin 4 for Tx
byte readCO2[]={0xFE, 0X44, 0X00, 0X08, 0X02, 0X9F, 0X25}; //Command packet to read Co2 (see app note)
byte response[]={0,0,0,0,0,0,0}; //create an array to store the response
//multiplier for value. default is 1. set to 3 for K-30 3% and 10 for K-33 ICB
int valMultiplier = 10;
int pin = 8;
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms =3000;//sampe 30s ;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
float concentration = 0;
int temperatureCommand = 3;
int humidityCommand = 5;
int clockPin = 2;
int dataPin = 3;
int val;
int ack;
float temperature;
float humidity;
const int chipSelect=10;
float refresh_rate=0.0;
long Num=1;
void setup()
{
Wire.begin();
K_30_Serial.begin(9600);
Serial.begin(9600);
Serial.print("Initializing SD card...");
Wire.beginTransmission(0x68); // address DS3231
Wire.write(0x0E); // select register
Wire.write(0b00011100); // w
pinMode(10, OUTPUT);
pinMode(8,INPUT);
starttime = millis();//get the current time;
pinMode (2, INPUT);
pinMode ( 3, INPUT);
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
return;
}
Serial.println("card initialized.");
File commandFile= SD.open("COMMANDS.txt");
if (commandFile)
{
Serial.println("Reading Command File");
float decade=pow(10,(commandFile.available()-1));
while
(commandFile.available())
{
float temp= (commandFile.read()-'0');
refresh_rate= temp* decade + refresh_rate;
decade=decade/10;
}
Serial.print("Refresh Rate=");
Serial.print( refresh_rate);
Serial.println("ms");
commandFile.close();
}
else
{
Serial.println("Could not readd command File");
return;
}
File logFile=SD.open("LOG18.csv",FILE_WRITE);
if (logFile)
{
logFile.println(",,,,");
String header= "Num,Temperature,Humidity,CO2,Dust concentration,time";
logFile.println(header);
logFile.close();
Serial.println(header);
}
else
{
Serial.println ("Couldn't Open log file");
}
}
void loop()
{sendRequest(readCO2);
unsigned long valCO2 = getValue(response);
Serial.print("Co2 ppm = ");
Serial.println(valCO2);
delay(refresh_rate);
duration = pulseIn(pin, LOW);
lowpulseoccupancy = lowpulseoccupancy+duration;
if ((millis()-starttime) > sampletime_ms)//if the sampel time == 30s
{
ratio = lowpulseoccupancy/(sampletime_ms10.0); // Integer percentage 0=>100
concentration = 1.1pow(ratio,3)-3.8pow(ratio,2)+520ratio+0.62; // using spec sheet curve
Serial.print(lowpulseoccupancy);
Serial.print(",");
Serial.print(ratio);
Serial.print(",");
Serial.println(concentration);
lowpulseoccupancy = 0;
starttime = millis();
delay (refresh_rate);
}
sendCommandSHT(temperatureCommand, dataPin, clockPin);
waitForResultSHT(dataPin);
val = getData16SHT(dataPin, clockPin);
skipCrcSHT(dataPin, clockPin);
temperature = (float)val * 0.01 - 40;
temperature = (float)temperature;
sendCommandSHT(humidityCommand, dataPin, clockPin);
waitForResultSHT(dataPin);
val = getData16SHT(dataPin, clockPin);
skipCrcSHT(dataPin, clockPin);
humidity = -4.0 + 0.0405 * val + -0.0000028 * val * val;
Serial.print(temperature100);
Serial.println(humidity100);
delay(refresh_rate);
// send request to receive data starting at register 0
{
Wire.beginTransmission(0x68); // 0x68 is DS3231 device address
Wire.write((byte)0); // start at register 0
Wire.endTransmission();
Wire.requestFrom(0x68, 3); // request three bytes (seconds, minutes, hours)
while(Wire.available())
{
int seconds = Wire.read(); // get seconds
int minutes = Wire.read(); // get minutes
int hours = Wire.read(); // get hours
seconds = (((seconds & 0b11110000)>>4)*10 + (seconds & 0b00001111)); // convert BCD to decimal
minutes = (((minutes & 0b11110000)>>4)*10 + (minutes & 0b00001111)); // convert BCD to decimal
hours = (((hours & 0b00100000)>>5)*20 + ((hours & 0b00010000)>>4)*10 + (hours & 0b00001111)); // convert BCD to decimal (assume 24 hour mode)
Serial.print(hours); Serial.print(":"); Serial.print(minutes); Serial.print(":"); Serial.println(seconds);
delay (1000);
String dataString= String(Num)+","+String(temperature)+","+String(humidity)+","+String(valCO2)+","+String(concentration)+","+String (hours)+":"+String(minutes)+":"+String (seconds);
File logFile= SD.open("LOG18.csv",FILE_WRITE);
if (logFile)
{
logFile.println(dataString);
logFile.close();
Serial.println(dataString);
}
else
{
Serial.println("couldn't open log File");
}
Num++;
delay(refresh_rate);
}
}
}
void sendRequest(byte packet[])
{
while(!K_30_Serial.available()) //keep sending request until we start to get a response
{
K_30_Serial.write(readCO2,7);
delay(50);
}
int timeout=0; //set a timeoute counter
while(K_30_Serial.available() < 7 ) //Wait to get a 7 byte response
{
timeout++;
if(timeout > 10) //if it takes to long there was probably an error
{
while(K_30_Serial.available()) //flush whatever we have
K_30_Serial.read();
break; //exit and try again
}
delay(50);
}
for (int i=0; i < 7; i++)
{
response = K_30_Serial.read();
}
}
unsigned long getValue(byte packet[])
{
int high = packet[3]; //high byte for value is 4th byte in packet in the packet
int low = packet[4]; //low byte for value is 5th byte in the packet
unsigned long val = high*256 + low; //Combine high byte and low byte with this formula to get value
return val* valMultiplier;
}
int shiftIn(int dataPin, int clockPin, int numBits) {
-
int ret = 0;*
-
for (int i=0; i<numBits; ++i) {*
-
digitalWrite(clockPin, HIGH);*
-
//delay(10); not needed :)*
_ ret = ret*2 + digitalRead(dataPin);_ -
digitalWrite(clockPin, LOW);*
-
}*
-
return(ret);*
}
// send a command to the SHTx sensor
void sendCommandSHT(int command, int dataPin, int clockPin) { -
int ack;*
-
// transmission start*
-
pinMode(dataPin, OUTPUT);*
-
pinMode(clockPin, OUTPUT);*
-
digitalWrite(dataPin, HIGH);*
-
digitalWrite(clockPin, HIGH);*
-
digitalWrite(dataPin, LOW);*
-
digitalWrite(clockPin, LOW);*
-
digitalWrite(clockPin, HIGH);*
-
digitalWrite(dataPin, HIGH);*
-
digitalWrite(clockPin, LOW);*
-
// shift out the command (the 3 MSB are address and must be 000, the last 5 bits are the command)*
-
shiftOut(dataPin, clockPin, MSBFIRST, command);*
-
// verify we get the right ACK*
-
digitalWrite(clockPin, HIGH);*
-
pinMode(dataPin, INPUT);*
-
ack = digitalRead(dataPin);*
-
if (ack != LOW)*
-
Serial.println("ACK error 0");*
-
digitalWrite(clockPin, LOW);*
-
ack = digitalRead(dataPin);*
-
if (ack != HIGH)*
-
Serial.println("ACK error 1");*
}
// wait for the SHTx answer
void waitForResultSHT(int dataPin) { -
int ack;*
-
pinMode(dataPin, INPUT);*
-
for(int i=0; i<100; ++i) {*
-
delay(10);*
-
ack = digitalRead(dataPin);*
-
if (ack == LOW)*
-
break;*
-
}*
-
if (ack == HIGH)*
-
Serial.println("ACK error 2");*
}
// get data from the SHTx sensor
int getData16SHT(int dataPin, int clockPin) { -
int val;*
-
// get the MSB (most significant bits)*
-
pinMode(dataPin, INPUT);*
-
pinMode(clockPin, OUTPUT);*
-
val = shiftIn(dataPin, clockPin, 8);*
_ val *= 256; // this is equivalent to val << 8;_ -
// send the required ACK*
-
pinMode(dataPin, OUTPUT);*
-
digitalWrite(dataPin, HIGH);*
-
digitalWrite(dataPin, LOW);*
-
digitalWrite(clockPin, HIGH);*
-
digitalWrite(clockPin, LOW);*
-
// get the LSB (less significant bits)*
-
pinMode(dataPin, INPUT);*
-
val |= shiftIn(dataPin, clockPin, 8);*
-
return val;*
}
// skip CRC data from the SHTx sensor
void skipCrcSHT(int dataPin, int clockPin){ -
pinMode(dataPin, OUTPUT);*
-
pinMode(clockPin, OUTPUT);*
-
digitalWrite(dataPin, HIGH);*
-
digitalWrite(clockPin, HIGH);*
-
digitalWrite(clockPin, LOW);*
}