-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParallax_PIR.ino
More file actions
164 lines (140 loc) · 5.43 KB
/
Parallax_PIR.ino
File metadata and controls
164 lines (140 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//****************************************************************
// OSBSS PIR motion sensor datalogger - v0.01
// Last edited on March 30, 2015
//****************************************************************
#include <PowerSaver.h>
#include <DS3234lib3.h>
#include <SdFat.h>
#include <EEPROM.h>
// Global objects and variables ******************************
char filename[15] = "log.csv"; // Filename. Format: "12345678.123". Cannot be more than 8 characters in length, contain spaces or start with a number
PowerSaver chip; // declare object for PowerSaver class
DS3234 RTC; // declare object for DS3234 class
SdFat sd; // declare object for SdFat class
SdFile file; // declare object for SdFile class
#define POWER 4 // pin 4 supplies power to microSD card breakout
#define RTCPOWER 6 // pin 6 supplies power to DS3234 RTC breakout
#define LED 7 // pin 7 controls LED
int SDcsPin = 9; // pin 9 is micrSD card breakout CS pin
int state = 0; // this variable stores the state of PIR sensor's output (either 1 or 0)
// ISR ****************************************************************
ISR(PCINT0_vect) // Interrupt Service Routine for PCINT0 vector (pin 8)
{
asm("nop"); // do nothing
}
// Main code ****************************************************************
void setup()
{
Serial.begin(19200); // open serial at 19200 bps
pinMode(POWER, OUTPUT); // set output pins
pinMode(RTCPOWER, OUTPUT);
pinMode(LED, OUTPUT);
digitalWrite(POWER, HIGH); // turn on SD card
digitalWrite(RTCPOWER, HIGH); // turn on RTC
delay(1); // give some delay to ensure SD card is turned on properly
if(!sd.init(SPI_FULL_SPEED, SDcsPin)) // initialize SD card on the SPI bus
{
delay(10);
SDcardError();
}
else
{
delay(10);
file.open(filename, O_CREAT | O_APPEND | O_WRITE); // open file in write mode and append data to the end of file
delay(1);
String time = RTC.timeStamp(); // get date and time from RTC
file.println();
file.print("Date/Time,Occupancy"); // Print header to file
file.println();
PrintFileTimeStamp();
file.close(); // close file - very important
// give some delay by blinking status LED to wait for the file to properly close
digitalWrite(LED, HIGH);
delay(10);
digitalWrite(LED, LOW);
}
chip.sleepInterruptSetup(); // setup sleep function & pin change interrupts on the ATmega328p. Power-down mode is used here
}
void loop()
{
digitalWrite(POWER, LOW); // turn off SD card
digitalWrite(RTCPOWER, LOW); // turn off RTC
delay(1);
chip.turnOffADC(); // turn off ADC to save power
chip.turnOffSPI(); // turn off SPI bus to save power
//chip.turnOffWDT(); // turn off WatchDog timer. This doesn't work for Pro Mini (Rev 11); only works for Arduino Uno
chip.turnOffBOD();
chip.goodNight(); // put the processor in power-down mode
// code will resume from here once the processor wakes up ============== //
chip.turnOnADC(); // turn on ADC once the processor wakes up
chip.turnOnSPI(); // turn on SPI bus once the processor wakes up
delay(1); // important delay to ensure SPI bus is properly activated
digitalWrite(POWER, HIGH); // turn on SD card
digitalWrite(RTCPOWER, HIGH); // turn on RTC
delay(1); // give some delay to ensure SD card is turned on properly
RTC.checkDST(); // check and account for Daylight Savings Time in US
printToSD(); // print data to SD card
}
void printToSD()
{
pinMode(SDcsPin, OUTPUT);
if(!sd.init(SPI_FULL_SPEED, SDcsPin)) // very important - reinitialize SD card on the SPI bus
{
delay(10);
SDcardError();
}
else
{
delay(10);
file.open(filename, O_WRITE | O_AT_END); // open file in write mode
delay(1);
String time = RTC.timeStamp(); // get date and time from RTC
SPCR = 0; // reset SPI control register
state = digitalRead(8); // read current state of PIR. 1 = movement detected, 0 = no movement.
if(state == 1)
{
file.print(time);
file.print(",");
file.print("0");
file.println();
file.print(time);
file.print(",");
file.print("1");
}
else if(state == 0)
{
file.print(time);
file.print(",");
file.print("1");
file.println();
file.print(time);
file.print(",");
file.print("0");
}
file.println();
PrintFileTimeStamp();
file.close(); // close file - very important
// give some delay by blinking status LED to wait for the file to properly close
digitalWrite(LED, HIGH);
delay(10);
digitalWrite(LED, LOW);
}
}
// file timestamps ****************************************************************
void PrintFileTimeStamp() // Print timestamps to data file. Format: year, month, day, hour, min, sec
{
file.timestamp(T_WRITE, RTC.year, RTC.month, RTC.day, RTC.hour, RTC.minute, RTC.second); // date modified
file.timestamp(T_ACCESS, RTC.year, RTC.month, RTC.day, RTC.hour, RTC.minute, RTC.second); // date accessed
}
// SD card Error response ****************************************************************
void SDcardError()
{
for(int i=0;i<3;i++) // blink LED 3 times to indicate SD card read/write error
{
digitalWrite(LED, HIGH);
delay(50);
digitalWrite(LED, LOW);
delay(150);
}
}
//****************************************************************