Code: Select all
const int sensorPin = 0; // select the input pin for the sensor
const int alarmPin = 3; // select the pin for the buzzer
const int buttonPin = 13; // select the pin for the button
int sensorValue = 0; // variable to store the value coming from the sensor
int alarmCount = 0; // stores number of times alarm was triggered
int grindTime = 0; // stores total seconds sensor was above threshold
int average = 0; // stores average length of time each alarm was on
int alarmState=LOW; // sets the inital alarmState to off
int lastAlarmState=LOW; // sets the inital lastAlarmState to off
int onCounter = 0; // sets onCounter to zero
int offCounter = 0; // sets offCounter to zero
int keepAlarmOn = 0; // sets initial state to off
int buttonState = 0; // sets the inital buttonState
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 5, 11, 12);
// A simple data logger for the Arduino analog pins
// how many milliseconds between grabbing data and logging it. 1000 ms is once a second
#define LOG_INTERVAL 1000 // mills between entries (reduce to take more/faster data)
// how many milliseconds before writing the logged data permanently to disk
// set it to the LOG_INTERVAL to write each time (safest)
// set it to 10*LOG_INTERVAL to write all data every 10 datareads, you could lose up to
// the last 10 reads if power is lost but it uses less power and is much faster!
#define SYNC_INTERVAL 1000 // mills between calls to flush() - to write data to the card
uint32_t syncTime = 0; // time of last sync()
#define ECHO_TO_SERIAL 1 // echo data to serial port
#define WAIT_TO_START 0 // Wait for serial input in setup()
// the digital pins that connect to the LEDs
//#define redLEDpin 3
#define greenLEDpin 2
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);
// red LED indicates error
digitalWrite(greenLEDpin, HIGH);
while(1);
}
void setup(void)
{
pinMode(alarmPin, OUTPUT);
alarmState=digitalRead(3); // reads pin 3 to set alarmState to HIGH or LOW
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input:
lcd.begin(16, 2); // set up the LCD's number of columns and rows:
lcd.print("hello, world!");
Serial.begin(9600);
Serial.println();
// use debugging LEDs
// pinMode(redLEDpin, OUTPUT);
pinMode(greenLEDpin, OUTPUT);
#if WAIT_TO_START
Serial.println("Type any character 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();
RTC.begin();
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
if (!RTC.begin()) {
logfile.println("RTC failed");
#if ECHO_TO_SERIAL
Serial.println("RTC failed");
#endif //ECHO_TO_SERIAL
}
logfile.println("Millis, Date, Time, Alarm, Sensor, Triggers, Grind Time, Avg Grind");
#if ECHO_TO_SERIAL
Serial.println("Millis, Date, Time, Alarm, Sensor, Triggers, Grind Time, Avg Grind");
#endif //ECHO_TO_SERIAL
}
void loop(void)
{
int threshold = 8;
sensorValue = analogRead(sensorPin);
lastAlarmState=alarmState; // stores last value for alarmState
alarmState=digitalRead(3); // reads pin 3 to set alarmState to HIGH or LOW
buttonState = digitalRead(buttonPin); // read the state of the pushbutton value:
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(millis()/1000);
if(analogRead(sensorPin) >= threshold)
{
onCounter++;
offCounter = 0;
}
if((analogRead(sensorPin) >= threshold) && onCounter >= 3)
{
digitalWrite(alarmPin, HIGH); // turn alarmPin on
}
if(analogRead(sensorPin) >= 100)
{
digitalWrite(alarmPin, HIGH);
keepAlarmOn = 1;
}
if(analogRead(sensorPin) < threshold)
{
onCounter = 0;
offCounter++;
}
if((analogRead(sensorPin) <= 0) && offCounter >= 3 && keepAlarmOn != 1)
{
digitalWrite(alarmPin, LOW); // turn alarm off after a delay of millis
}
if (buttonState == HIGH)
{
digitalWrite(alarmPin, LOW);
keepAlarmOn = 0;
}
if(alarmState==HIGH && lastAlarmState==LOW)
{
alarmCount++; // add 1 to counter each time new alarm triggers
}
if(alarmState == HIGH)
{
grindTime++;
logfile.print(", ");
logfile.print("150"); // prints 300 while alarm is active
#if ECHO_TO_SERIAL
Serial.print(", ");
Serial.print("150"); // prints 300 while alarm is active
#endif
}
else
{
logfile.print(", ");
logfile.print("0"); // prints 0 while the alarm is not active
#if ECHO_TO_SERIAL
Serial.print(", ");
Serial.print("0"); // prints 0 while the alarm is not active
#endif
}
logfile.print(", ");
logfile.print(sensorValue);
logfile.print(", ");
logfile.print(alarmCount);
logfile.print(", ");
logfile.print(grindTime);
logfile.print(", ");
logfile.println(grindTime/alarmCount);
#if ECHO_TO_SERIAL
Serial.print(", ");
Serial.print(sensorValue);
Serial.print(", ");
Serial.print(alarmCount);
Serial.print(", ");
Serial.print(grindTime);
Serial.print(", ");
Serial.println(grindTime/alarmCount);
#endif
DateTime now;
// delay for the amount of time we want between readings
delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
digitalWrite(greenLEDpin, HIGH);
// log milliseconds since starting
uint32_t m = millis();
logfile.print(m); // milliseconds since start
logfile.print(", ");
#if ECHO_TO_SERIAL
Serial.print(m); // milliseconds since start
Serial.print(", ");
#endif
// fetch the time
now = RTC.now();
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);
#if ECHO_TO_SERIAL
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);
#endif //ECHO_TO_SERIAL
digitalWrite(greenLEDpin, LOW);
// Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
// which uses a bunch of power and takes time
if ((millis() - syncTime) < SYNC_INTERVAL) return;
syncTime = millis();
// blink LED to show we are syncing data to the card & updating FAT!
//digitalWrite(redLEDpin, HIGH);
logfile.flush();
// digitalWrite(redLEDpin, LOW);
}