//CONSIDER USING MODE OR MEAN METHOD TO REMOVE NOISE THAT WILL BE MEASURED WHEN PUT IN A CABINET//
//Libraries\
#include<LiquidCrystal.h>
#include <Time.h>
#include <TimeLib.h>
#include <Wire.h>
#include "RTClib.h"
#include <SPI.h>
#include <SD.h>
RTC_DS1307 RTC;
File dataFile;
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
char filename[] = "dataFile.txt";
int Htime; // integer variable for on time of a PWM signal
int Ltime; // integer variable for off time of a PWM signal
int Ttime; // integer variable of the total period of the PWM signal in microseconds
int frequency; //The value returned from the freqz function
int freq; //Assigned the value returned from the freqz function
int currentMenuItem = 0;
int lastState = 0;
int x = analogRead (0);
int i = 0;
int programpin = 2;
const int chipSelect = 4;
String PC = "Photocells";
//Setup code here\
void setup() {
pinMode(programpin, OUTPUT);
Wire.begin();
lcd.begin(16, 2); // initiate LCD
// initialize serial communication at 9600 bits per second
Serial.begin(9600); // baud rate for computer serial communication
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
pinMode(chipSelect, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
RTC.begin();
if (! RTC.isrunning()) {
lcd.print("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
// uncomment it & upload to set the time, date and start run the RTC!
RTC.adjust(DateTime(DATE, TIME));
}
}
void loop()
{
mainMenu();
}
void mainMenu() {
//State = 0 every loop cycle.
int state = 0;
//Refresh the button pressed.
int x = analogRead (0);
//Set the Row 0, Col 0 position.
lcd.setCursor(0,0);
//Check analog values from LCD Keypad Shield
if (x < 100) {
//Right
} else if (x < 200) {
//Up
state = 1;
} else if (x < 400){
//Down
state = 2;
} else if (x < 600){
//Left
} else if (x < 800){
//Select
state = 3;
}
//If we are out of bounds on th menu then reset it.
if (currentMenuItem < 0 || currentMenuItem > 8) {
currentMenuItem = 0;
}
//If we have changed Index, saves re-draws.
if (state != lastState) {
if (state == 1) {
//If Up
currentMenuItem = currentMenuItem - 1;
displayMenu(currentMenuItem);
} else if (state == 2) {
//If Down
currentMenuItem = currentMenuItem + 1;
displayMenu(currentMenuItem);
} else if (state == 3) {
//If Selected
selectMenu(currentMenuItem);
}
//Save the last State to compare.
lastState = state;
}
//Small delay
delay(5);
}
//Display Menu Option based on Index.
void displayMenu(int x) {
switch (x) {
case 1:
clearPrintTitle();
lcd.print ("");
barrierprogram();
break;
case 2:
clearPrintTitle();
lcd.print ("");
break;
case 3:
clearPrintTitle();
lcd.print ("");
break;
case 4:
clearPrintTitle();
lcd.print ("");
break;
case 5:
clearPrintTitle();
lcd.print ("");
break;
case 6:
clearPrintTitle();
lcd.print ("");
break;
case 7:
clearPrintTitle();
lcd.print ("");
break;
case 8:
clearPrintTitle();
lcd.print ("Main Program");
break;
}
}
//Print a basic header on Row 1.
void clearPrintTitle() {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Select Program");
lcd.setCursor(0,1);
}
//Show the selection on Screen.
void selectMenu(int x) {
switch (x) {
case 1:
lcd.clear();
lcd.print ("");
bprogram();
break;
case 2:
lcd.clear();
lcd.print ("");
//Call the function that belongs to Option 2
break;
case 3:
lcd.clear();
lcd.print ("");
//Call the function that belongs to Option 3
break;
case 4:
lcd.clear();
lcd.print ("");
//Call the function that belongs to Option 3
break;
case 5:
lcd.clear();
lcd.print ("");
//Call the function that belongs to Option 4
break;
case 6:
lcd.clear();
lcd.print ("");
//Call the function that belongs to Option 4
break;
case 7:
lcd.clear();
SeeSDMemory();
//Call the function that belongs to Option 4
break;
case 8:
lcd.clear();
i = 1;
while (i=1)
{
mainProgram();
}
mainMenu();
//Call the function that belongs to Option 4
break;
}
}
void SeeSDMemory()
{
}
void mainProgram()
{
delay (200);
if (x < 800)
{
i = 0;
mainMenu();
}
freq = freqz();
if (freq < 350)
{
lcd.clear();
lcd.setCursor(0, 0);
DateTime now = RTC.now();
lcd.print(now.day(), DEC);
lcd.print('/');
lcd.print(now.month(), DEC);
lcd.print('/');
lcd.print(now.year(), DEC);
lcd.setCursor(0, 1);
lcd.print(now.hour(), DEC);
lcd.print(':');
byte minute = now.minute();
if (minute < 10)
{
lcd.print("0");
}
lcd.print(now.minute(), DEC);
lcd.print(':');
byte second = now.second();
if (second < 10)
{
lcd.print("0");
}
lcd.print(now.second(), DEC);
return;
}
else
{
int range = map(freq, 0, 32000, 0, 80);
switch (range) {
case 1:
setScreen();
lcd.print("");
logtime();
break;
case 2:
setScreen();
lcd.print("");
break;
case 3:
setScreen();
lcd.print("");
break;
case 4:
setScreen();
lcd.print("");
break;
case 5:
setScreen();
lcd.print("");
break;
case 6:
setScreen();
lcd.print("");
break;
case 7:
setScreen();
lcd.print("");
break;
}
}
delay(1); // delay in between reads for stability
}
int freqz()
{
Htime = pulseIn(3,HIGH); // monitors PWM in digital pin 8 and stores high time of the signal in microseconds
Ltime = pulseIn(3, LOW); // monitors PWM in digital pin 8 and stores low time of the signal in microseconds
Ttime = Htime+Ltime; // total period of the signal
frequency =1000000/Ttime; // converts the value of the period to seconds and then calculates frequency (inverse of time)
return frequency;
}
void setScreen()
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("");
lcd.setCursor(0,1);
}
void logtime()
{
File dataFile = SD.open(filename, FILE_WRITE);
if (! dataFile)
{
Serial.println("couldnt create file");
return;
}
Serial.print("Logging to: ");
Serial.println(filename);
dataFile.print(Pointer); \This is where I want differennt strings to go
DateTime now = RTC.now();
// log time
if (dataFile) {
dataFile.println(now.day(), DEC);
dataFile.print("/");
dataFile.print(now.month(), DEC);
dataFile.print("/");
dataFile.print(now.year(), DEC);
dataFile.println(" ");
dataFile.print(now.hour(), DEC);
dataFile.print(":");
dataFile.print(now.minute(), DEC);
dataFile.print(":");
dataFile.print(now.second(), DEC);
dataFile.println(" ");
Serial.println("Data Logged");
dataFile.println(" ");
dataFile.close();
}
else
{
Serial.println("error opening datalog.txt");
}
}
void bprogram()
{
for (int j = 0; j < 10; j++)
{
digitalWrite(pin, HIGH);
delay(100);
digitalWrite(pin, LOW);
delay(100);
}
}