Advertisement
Guest User

Go to sleep at 10pm, wake up at 6am

a guest
Jun 13th, 2021
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* ---------------------------------------------------------------
  2.     Record a reading when the reading from an air
  3.     pressure sensor jumps suddenly - ie when a car drives over a tube
  4.     attached to the sensor. The Pressure Sensor is a MPX5500DP
  5.     I am using the movingAvg library to simplify the maths a bit.
  6.     It goes to sleep between 10pm and 7am as there is no traffic then.
  7.  
  8.    ---------------------------------------------------------------*/
  9.  
  10. #include <movingAvg.h>  // https://github.com/JChristensen/movingAvg
  11. #include <SD.h>
  12. #include <SPI.h>
  13. #include <Wire.h>
  14. #include <RV-3028-C7.h>        // https://github.com/constiko/RV-3028_C7-Arduino_Library
  15. #include "LowPower.h"         // low power library
  16.  
  17. const uint8_t airSensor(A0);  // connect pressure sensor from A0 pin to ground
  18.  
  19. const int wakeUpPin = 2;      // Use pin2  to wake up.
  20.  
  21. //The below variables control what the alarm will be set to. See the RV-3028 library for more details
  22. int alm_minute = 59;      // I only need the correct minute while testing
  23. int alm_hour = 07;            
  24. int alm_date_or_weekday = 2;
  25. bool alm_isweekday = false;
  26. uint8_t alm_mode = 6;         // this mode means we just check when the minutes match
  27.  
  28. movingAvg airAvg(20);        // sets the moving average - change the figure in brackets to what you want
  29.  
  30. unsigned long new_time = 0;   // set some variables for preventing reading multiple spikes
  31. unsigned long old_time = 0;   //
  32.  
  33. RV3028 rtc;                  // get the clock going
  34.  
  35. String timestamp;           //
  36.  
  37. const int chipSelect = 8;      // SD card pin
  38. int ledPin = 5;                // the pin the LED is connected to
  39.  
  40. File logFile;               // the logging file
  41.  
  42. void wakeUp()
  43. {
  44.   // Just a handler for the pin interrupt.
  45. }
  46.  
  47. /* --------------------------------------------
  48.   /*  setup the average, pullup the air sensor,
  49.      begin the serial monitor and show an initial
  50.      reading so we knnow it is working
  51.    --------------------------------------------*/
  52. void setup()
  53. {
  54.   pinMode(wakeUpPin, INPUT_PULLUP);        // Configure wake up pin as input.
  55.  
  56.   Wire.begin();
  57.   Serial.begin(9600);                 // begin serial monitor
  58.   if (rtc.begin() == false)
  59.   {
  60.     Serial.println("Something went wrong, check wiring");
  61.     while (1);
  62.   }
  63.   else
  64.     Serial.println("RTC online!");
  65.   //Enable alarm interrupt
  66.   rtc.enableAlarmInterrupt(alm_minute, alm_hour, alm_date_or_weekday, alm_isweekday, alm_mode);
  67.  
  68.  
  69.   delay(1000);
  70.  
  71.   pinMode(airSensor, INPUT_PULLUP);   // air sensor
  72.  
  73.   airAvg.begin();                     //averages
  74.  
  75.   pinMode(chipSelect, OUTPUT);
  76.   digitalWrite(chipSelect, HIGH); //ALWAYS pullup the ChipSelect pin with the SD library
  77.   delay(100);
  78.  
  79.   // initialize the SD card
  80.   Serial.print("Initializing SD card...");
  81.  
  82.   // make sure that the default chip select pin is set to
  83.   // output, even if you don't use it:
  84.   pinMode(8, OUTPUT);
  85.  
  86.   // see if the card is present and can be initialized:
  87.   if (!SD.begin(chipSelect)) {
  88.     Serial.println("Card failed, or not present");
  89.     // don't do anything more:
  90.     return;
  91.   }
  92.   Serial.println("Card initialized.");
  93.   Serial.print("Logging to: ");
  94.   Serial.println("TRAFFIC.CSV");
  95.   logFile = SD.open("TRAFFIC.CSV", FILE_WRITE);
  96.   logFile.println("");
  97.   logFile.println("NEW SESSION");
  98.   logFile.close();
  99.  
  100.   Serial.println("Setup complete");
  101.   Serial.println("initial reading");
  102.   int pc = analogRead(airSensor); // read the sensor
  103.   Serial.println(pc);
  104. }
  105.  
  106. /* --------------------------------------------
  107.   /*  Each loop should comapare the reading against
  108.      the moving average, and if it is greater than
  109.      the specific amount, it records to SD and serial
  110.    --------------------------------------------*/
  111. void loop()
  112. {
  113.  
  114.   int min = rtc.getMinutes();    // get the minute
  115.   if (min == 58) // if it's a match, go to sleep
  116.   {
  117.     // Allow wake up pin to trigger interrupt on low.
  118.     attachInterrupt(0, wakeUp, LOW);
  119.     Serial.print("going to sleep");
  120.     delay(500);
  121.     // Enter power down state with ADC and BOD module disabled.
  122.     // Wake up when wake up pin is low.
  123.     LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
  124.     Serial.println("alarm");
  125.     delay(10);
  126.     detachInterrupt(0);
  127.   }
  128.   else  // if it's not, keep counting cars
  129.   {
  130.     rtc.updateTime();                      // get the time
  131.     int pc = analogRead(airSensor);        // read the sensor
  132.     int avg = airAvg.reading(pc);          // calculate the moving average
  133.     int avgPlus = avg + 5;                 // to simplify conditional below
  134.     unsigned long new_time = millis();     // this is to make sure spikes are spaced, in case a single count causes a double spike
  135.  
  136.     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
  137.  
  138.     // if the reading is greater than the average & however many ms has passed since last time, print it.
  139.     // This is the ms value between spikes - change it to help calibrate your counter
  140.     // eg don't count a spike if it occurs within 400ms of the last one
  141.     if ((pc > avgPlus) && ((new_time - old_time) > 400))
  142.     {
  143.  
  144.       // write data to serial
  145.       Serial.print(rtc.stringDate());
  146.       Serial.print(" ");
  147.       Serial.print(rtc.stringTime());
  148.       Serial.print(", ");
  149.       Serial.print(pc);
  150.       Serial.print(", ");
  151.       Serial.println(avg);
  152.  
  153.       logFile = SD.open("TRAFFIC.CSV", FILE_WRITE); // open TRAFFIC.CSV file on SD Card and write to it
  154.       Serial.println("log");
  155.       logFile.print(rtc.stringDate());
  156.       logFile.print(",");
  157.       logFile.print(rtc.stringTime());
  158.       logFile.print(",");
  159.       logFile.print(pc);
  160.       logFile.print(",");
  161.       logFile.println(avg);
  162.       logFile.close();
  163.       Serial.println("done.");
  164.  
  165.       old_time = new_time;  // spacing spikes
  166.  
  167.     }
  168.     else
  169.     {
  170.       delay(500);    
  171.      
  172.       Serial.print("awake ");
  173.       Serial.println(rtc.stringTime());
  174.     }
  175.   }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement