Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ---------------------------------------------------------------
- Record a reading when the reading from an air
- pressure sensor jumps suddenly - ie when a car drives over a tube
- attached to the sensor. The Pressure Sensor is a MPX5500DP
- I am using the movingAvg library to simplify the maths a bit.
- It goes to sleep between 10pm and 7am as there is no traffic then.
- ---------------------------------------------------------------*/
- #include <movingAvg.h> // https://github.com/JChristensen/movingAvg
- #include <SD.h>
- #include <SPI.h>
- #include <Wire.h>
- #include <RV-3028-C7.h> // https://github.com/constiko/RV-3028_C7-Arduino_Library
- #include "LowPower.h" // low power library
- const uint8_t airSensor(A0); // connect pressure sensor from A0 pin to ground
- const int wakeUpPin = 2; // Use pin2 to wake up.
- //The below variables control what the alarm will be set to. See the RV-3028 library for more details
- int alm_minute = 59; // I only need the correct minute while testing
- int alm_hour = 07;
- int alm_date_or_weekday = 2;
- bool alm_isweekday = false;
- uint8_t alm_mode = 6; // this mode means we just check when the minutes match
- movingAvg airAvg(20); // sets the moving average - change the figure in brackets to what you want
- unsigned long new_time = 0; // set some variables for preventing reading multiple spikes
- unsigned long old_time = 0; //
- RV3028 rtc; // get the clock going
- String timestamp; //
- const int chipSelect = 8; // SD card pin
- int ledPin = 5; // the pin the LED is connected to
- File logFile; // the logging file
- void wakeUp()
- {
- // Just a handler for the pin interrupt.
- }
- /* --------------------------------------------
- /* setup the average, pullup the air sensor,
- begin the serial monitor and show an initial
- reading so we knnow it is working
- --------------------------------------------*/
- void setup()
- {
- pinMode(wakeUpPin, INPUT_PULLUP); // Configure wake up pin as input.
- Wire.begin();
- Serial.begin(9600); // begin serial monitor
- if (rtc.begin() == false)
- {
- Serial.println("Something went wrong, check wiring");
- while (1);
- }
- else
- Serial.println("RTC online!");
- //Enable alarm interrupt
- rtc.enableAlarmInterrupt(alm_minute, alm_hour, alm_date_or_weekday, alm_isweekday, alm_mode);
- delay(1000);
- pinMode(airSensor, INPUT_PULLUP); // air sensor
- airAvg.begin(); //averages
- pinMode(chipSelect, OUTPUT);
- digitalWrite(chipSelect, HIGH); //ALWAYS pullup the ChipSelect pin with the SD library
- delay(100);
- // 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(8, 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.");
- Serial.print("Logging to: ");
- Serial.println("TRAFFIC.CSV");
- logFile = SD.open("TRAFFIC.CSV", FILE_WRITE);
- logFile.println("");
- logFile.println("NEW SESSION");
- logFile.close();
- Serial.println("Setup complete");
- Serial.println("initial reading");
- int pc = analogRead(airSensor); // read the sensor
- Serial.println(pc);
- }
- /* --------------------------------------------
- /* Each loop should comapare the reading against
- the moving average, and if it is greater than
- the specific amount, it records to SD and serial
- --------------------------------------------*/
- void loop()
- {
- int min = rtc.getMinutes(); // get the minute
- if (min == 58) // if it's a match, go to sleep
- {
- // Allow wake up pin to trigger interrupt on low.
- attachInterrupt(0, wakeUp, LOW);
- Serial.print("going to sleep");
- delay(500);
- // Enter power down state with ADC and BOD module disabled.
- // Wake up when wake up pin is low.
- LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
- Serial.println("alarm");
- delay(10);
- detachInterrupt(0);
- }
- else // if it's not, keep counting cars
- {
- rtc.updateTime(); // get the time
- int pc = analogRead(airSensor); // read the sensor
- int avg = airAvg.reading(pc); // calculate the moving average
- int avgPlus = avg + 5; // to simplify conditional below
- unsigned long new_time = millis(); // this is to make sure spikes are spaced, in case a single count causes a double spike
- delay(1); // For some reason, the If statement that follows doesn't work without a delay here?? I think this is a bug in my system
- // if the reading is greater than the average & however many ms has passed since last time, print it.
- // This is the ms value between spikes - change it to help calibrate your counter
- // eg don't count a spike if it occurs within 400ms of the last one
- if ((pc > avgPlus) && ((new_time - old_time) > 400))
- {
- // write data to serial
- Serial.print(rtc.stringDate());
- Serial.print(" ");
- Serial.print(rtc.stringTime());
- Serial.print(", ");
- Serial.print(pc);
- Serial.print(", ");
- Serial.println(avg);
- logFile = SD.open("TRAFFIC.CSV", FILE_WRITE); // open TRAFFIC.CSV file on SD Card and write to it
- Serial.println("log");
- logFile.print(rtc.stringDate());
- logFile.print(",");
- logFile.print(rtc.stringTime());
- logFile.print(",");
- logFile.print(pc);
- logFile.print(",");
- logFile.println(avg);
- logFile.close();
- Serial.println("done.");
- old_time = new_time; // spacing spikes
- }
- else
- {
- delay(500);
- Serial.print("awake ");
- Serial.println(rtc.stringTime());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement