I'm trying to use the Statistics.h library that Rob Tillaart developed but I'm running into a problem. I have attached my code here.
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include "Statistics.h"
#define ECHO_TO_SERIAL 1 // echo data to serial port
#define WAIT_TO_START 1 // Wait for serial input in setup()
RTC_DS1307 RTC; // define the Real Time Clock object
// for the data logging shield, we use digital pin 10 for the SD cs line
const int chipSelect = 10;
// the logging file
File logfile;
void error(char *str)
{
Serial.print("error: ");
Serial.println(str);
while(1);
}
//int IRSense1 = 4;
//int IRSense2 = 4;
//int IRSense3 = 6;
//int IR1 = 0;
//int IR2 = 0;
//int IR3 = 0;
int CdSPin = 0;
int CdSReading;
Statistics myStats;
int counter = 0;
int currentState = 0;
int previousState = 0;
void setup(void)
{
//pinMode(IRSense1, INPUT);
//pinMode(IRSense2, INPUT);
//pinMode(IRSense3, INPUT);
Serial.begin(9600);
Serial.println();
myStats.clear();
#if WAIT_TO_START
Serial.println("Type any character and press Enter to start");
while (!Serial.available());
#endif //WAIT_TO_START
// initialize the SD card
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
error("Card failed, or not present");
}
Serial.println("card initialized.");
// create a new file
char filename[] = "Logger00.CSV";
for (uint8_t i = 0; i < 100; i++) {
filename[6] = i/10 + '0';
filename[7] = i%10 + '0';
if (! SD.exists(filename)) {
// only open a new file if it doesn't exist
logfile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}
if (! logfile) {
error("couldnt create file");
}
Serial.print("Logging to: ");
Serial.println(filename);
// connect to RTC
Wire.begin();
if (!RTC.begin()) {
logfile.println("RTC failed");
#if ECHO_TO_SERIAL
Serial.println("RTC failed");
#endif //ECHO_TO_SERIAL
}
logfile.println("datetime, count");
#if ECHO_TO_SERIAL
Serial.println("datetime, count");
#endif //ECHO_TO_SERIAL
}
void loop(void)
{
DateTime now;
//IR1 = digitalRead(IRSense1);
//IR2 = digitalRead(IRSense2);
//IR3 = digitalRead(IRSense3);
analogRead(CdSPin);
delay(10);
CdSReading = analogRead(CdSPin);
myStats.add(CdSReading);
Serial.println("Count");
Serial.println(myStats.count());
Serial.println("Average");
Serial.println(myStats.average(),4);
Serial.println("Std Deviation");
Serial.println(myStats.pop_stdev(),4);
// if (CdSReading <= 940)
// {
// currentState = 1;
// }
/*if (IR1 == HIGH)
{
currentState = 1;
}
if (IR2 == HIGH)
{
currentState = 1;
}
/*
else if (IR3 == HIGH)
{
currentState = 1;
}
*/
// else
// {
// currentState = 0;
// }
if (currentState != previousState)
{
if(currentState == 1)
{
counter = counter + 1;
// fetch the time
now = RTC.now();
// log time
logfile.print('"');
logfile.print(now.year(), DEC);
logfile.print("/");
logfile.print(now.month(), DEC);
logfile.print("/");
logfile.print(now.day(), DEC);
logfile.print(" ");
logfile.print(now.hour(), DEC);
logfile.print(":");
logfile.print(now.minute(), DEC);
logfile.print(":");
logfile.print(now.second(), DEC);
logfile.print('"');
logfile.print(",");
logfile.println(counter);
#if ECHO_TO_SERIAL
Serial.print('"');
Serial.print(now.year(), DEC);
Serial.print("/");
Serial.print(now.month(), DEC);
Serial.print("/");
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(":");
Serial.print(now.minute(), DEC);
Serial.print(":");
Serial.print(now.second(), DEC);
Serial.print('"');
Serial.print(",");
//Serial.println(counter);
Serial.println(CdSReading);
#endif
}
}
previousState = currentState;
logfile.flush();
delay(1000);
}
These are the errors:
IR_Pulse_Counter_SDcomm:32: error: 'Statistics' does not name a type
IR_Pulse_Counter_SDcomm.ino: In function 'void setup()':
IR_Pulse_Counter_SDcomm:44: error: 'myStats' was not declared in this scope
IR_Pulse_Counter_SDcomm.ino: In function 'void loop()':
IR_Pulse_Counter_SDcomm:106: error: 'myStats' was not declared in this scope
To use the library, I copied the code for the .h and .cpp files into notepad, saved them as Statistics.h and Statistics.copp in C:\Program Files (x86)\Arduino\libraries\Statistics. Put in the #include, declared Statistics myStats, etc. Is there something that I'm missing?