Components and supplies
Pelicase 1120
RV 3028 RTC Breakout
Hook Up Wire Kit, 22 AWG
clear plastic tube, 4mm internal diamter
DC POWER JACK 2.1MM BARREL-TYPE PCB MOUNT
Barrel jack power switch
Flash Memory Card, MicroSD Card
4mm double headed bulkhead connector
SparkFun Level Shifting microSD Breakout
4mm internal diameter rubber fuel hose
Arduino Pro Mini 328 - 5V/16MHz
MPX5100DP Pressure Sensor
4mm hose T-connector
Tools and machines
Soldering iron (generic)
Plier, Needle Nose
Solder Wire, Lead Free
Project description
Code
New! Overnight sleep
arduino
I've added a few lines and rearranged the code so the arduino goes to sleep at night, as we are only open 10am - 5pm. This saves a bit of battery! you need to physically connect pin 2 on the arduino to the alm or int pin on the RTC.
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 8am 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 17RV3028 rtc; // get the clock going 18 19const uint8_t airSensor(A0); // connect pressure sensor from A0 pin to ground 20 21const int wakeUpPin = 2; // Use pin2 to wake up. 22 23//The below variables control what the alarm will be set to. See the RV-3028 library for more details 24int alm_minute = 28; // this is ignored 25int alm_hour = 8; // wake up at 8am 26int alm_date_or_weekday = 2; // this is ignored 27bool alm_isweekday = false; // this is ignored 28uint8_t alm_mode = 5; // this mode means we just check when the hours match = alarm 29 30movingAvg airAvg(20); // sets the moving average - change the figure in brackets to what you want 31 32unsigned long new_time = 0; // set some variables for preventing reading multiple spikes 33unsigned long old_time = 0; // 34 35String timestamp; // 36 37const int chipSelect = 8; // SD card pin 38int ledPin = 5; // the pin the LED is connected to 39 40File logFile; // the logging file 41 42 43void wakeUp() 44{ 45 // Just a handler for the pin interrupt. 46} 47 48/* -------------------------------------------- 49 /* setup the average, pullup the air sensor, 50 begin the serial monitor and show an initial 51 reading so we knnow it is working 52 --------------------------------------------*/ 53void setup() 54{ 55 pinMode(wakeUpPin, INPUT_PULLUP); // Configure wake up pin as input. 56 57 Wire.begin(); 58 Serial.begin(9600); // begin serial monitor 59 if (rtc.begin() == false) 60 { 61 Serial.println("Something went wrong, check wiring"); 62 while (1); 63 } 64 else 65 Serial.println("RTC online!"); 66 //Enable alarm interrupt 67 rtc.enableAlarmInterrupt(alm_minute, alm_hour, alm_date_or_weekday, alm_isweekday, alm_mode); 68 69 70 delay(1000); 71 72 pinMode(airSensor, INPUT_PULLUP); // air sensor 73 74 airAvg.begin(); //averages 75 76 pinMode(chipSelect, OUTPUT); 77 digitalWrite(chipSelect, HIGH); //ALWAYS pullup the ChipSelect pin with the SD library 78 delay(100); 79 80 // initialize the SD card 81 Serial.print("Initializing SD card..."); 82 83 // make sure that the default chip select pin is set to 84 // output, even if you don't use it: 85 pinMode(8, OUTPUT); 86 87 // see if the card is present and can be initialized: 88 if (!SD.begin(chipSelect)) { 89 Serial.println("Card failed, or not present"); 90 // don't do anything more: 91 return; 92 } 93 Serial.println("Card initialized."); 94 Serial.print("Logging to: "); 95 Serial.println("TRAFFIC.CSV"); 96 logFile = SD.open("TRAFFIC.CSV", FILE_WRITE); 97 logFile.println(""); 98 logFile.println("NEW SESSION"); 99 logFile.close(); 100 101 Serial.println("Setup complete"); 102 Serial.println("initial reading"); 103 int pc = analogRead(airSensor); // read the sensor 104 Serial.println(pc); 105 delay(250); 106} 107 108/* -------------------------------------------- 109 /* Each loop should comapare the reading against 110 the moving average, and if it is greater than 111 the specific amount, it records to SD and serial 112 --------------------------------------------*/ 113void loop() 114{ 115 rtc.updateTime(); // get the time 116 int hour = rtc.getHours(); // get the hour 117 if (hour == 22) // if it's a match, go to sleep 118 { 119 Serial.println("sleep time"); 120 GoToSleep(); //go to the sleep void 121 } 122 CountCars(); // or just keep counting the cars 123} 124 125void GoToSleep() 126{ 127 // Allow wake up pin to trigger interrupt on low. 128 attachInterrupt(0, wakeUp, LOW); // go to the wakeUp void when alarm triggers 129 // Enter power down state with ADC and BOD module disabled. 130 // Wake up when wake up pin is low. 131 logFile = SD.open("TRAFFIC.CSV", FILE_WRITE); // open TRAFFIC.CSV file on SD Card and write to it, plus a few lines to know it's all working. you can remove these once you are happy it is 132 logFile.print("went to sleep"); 133 logFile.print(","); 134 logFile.print(rtc.stringDate()); 135 logFile.print("-"); 136 logFile.println(rtc.stringTime()); 137 logFile.close(); 138 Serial.println("sleep time"); 139 delay(50); 140 LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); 141 142 // Wakes up here! 143 detachInterrupt(0); //disable the interupt pin 144 145 Serial.println("alarm"); //let me know we woke up OK 146 logFile = SD.open("TRAFFIC.CSV", FILE_WRITE); // open TRAFFIC.CSV file on SD Card and write to it 147 logFile.print("woke up"); 148 logFile.print(","); 149 logFile.print(rtc.stringDate()); 150 logFile.print("-"); 151 logFile.println(rtc.stringTime()); 152 logFile.close(); 153 delay(50); 154 155 CountCars(); //go back to counting cars 156} 157 158void CountCars() 159{ 160 rtc.updateTime(); // get the time 161 int pc = analogRead(airSensor); // read the sensor 162 int avg = airAvg.reading(pc); // calculate the moving average 163 int avgPlus = avg + 5; // to simplify conditional below 164 unsigned long new_time = millis(); // this is to make sure spikes are spaced, in case a single count causes a double spike 165 166 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 167 168 // if the reading is greater than the average & however many ms has passed since last time, print it. 169 // This is the ms value between spikes - change it to help calibrate your counter 170 // eg don't count a spike if it occurs within 400ms of the last one 171 if ((pc > avgPlus) && ((new_time - old_time) > 400)) 172 { 173 // write data to serial 174 Serial.print(rtc.stringDate()); 175 Serial.print(" "); 176 Serial.print(rtc.stringTime()); 177 Serial.print(", "); 178 Serial.print(pc); 179 Serial.print(", "); 180 Serial.println(avg); 181 182 // write data to the csv file 183 logFile = SD.open("TRAFFIC.CSV", FILE_WRITE); // open TRAFFIC.CSV file on SD Card and write to it 184 Serial.println("log"); 185 logFile.print(rtc.stringDate()); 186 logFile.print(","); 187 logFile.print(rtc.stringTime()); 188 logFile.print(","); 189 logFile.print(pc); 190 logFile.print(","); 191 logFile.println(avg); 192 logFile.close(); 193 Serial.println("done."); 194 195 old_time = new_time; // spacing spikes 196 197 } 198 else 199 { 200 delay(5); 201 202 } 203 204}
Car counter pressure test
arduino
This code allows you to see that the pressure sensor is working. It's just the main code with some bits commented out.
1/* --------------------------------------------------------------- 2 I am trying to 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 ---------------------------------------------------------------*/ 7 8#include <movingAvg.h> // https://github.com/JChristensen/movingAvg 9#include <SD.h> 10#include <SPI.h> 11#include <Wire.h> 12#include <RV-3028-C7.h> // this is a low-power clock 13 14const uint8_t airSensor(A0); // connect pressure sensor from A0 pin to ground 15movingAvg airAvg(20); // define the moving average object 16// 17//unsigned long new_time=0; // set some variables for preventing reading multiple spikes 18unsigned long old_time=0; // 19 20RV3028 rtc; // get the clock going 21 22String timestamp; // 23 24 25const int chipSelect = 8; // SD card pin 26int ledPin = 5; // the pin the LED is connected to 27 28File logFile; // the logging file 29 30 31 32/* -------------------------------------------- 33/* setup the average, pullup the air sensor, 34 * begin the serial monitor and show an initial 35 * reading so we knnow it is working 36 --------------------------------------------*/ 37void setup() 38{ 39 Wire.begin(); 40 Serial.begin(9600); // begin serial monitor 41 if (rtc.begin() == false) 42 { 43 Serial.println("Something went wrong, check wiring"); 44 while (1); 45 } 46 else 47 Serial.println("RTC online!"); 48 49 delay(1000); 50 pinMode(airSensor, INPUT_PULLUP); // air sensor 51 52 airAvg.begin(); //averages 53 54 pinMode(chipSelect, OUTPUT); 55 digitalWrite(chipSelect, HIGH); //ALWAYS pullup the ChipSelect pin with the SD library 56 delay(100); 57 58 // initialize the SD card 59 Serial.print("Initializing SD card..."); 60 61 // make sure that the default chip select pin is set to 62 // output, even if you don't use it: 63 pinMode(8, OUTPUT); 64 65 // see if the card is present and can be initialized: 66 if (!SD.begin(chipSelect)) { 67 Serial.println("Card failed, or not present"); 68 // don't do anything more: 69 return; 70 } 71 Serial.println("Card initialized."); 72 Serial.print("Logging to: "); 73 Serial.println("TRAFFIC.CSV"); 74 logFile = SD.open("TRAFFIC.CSV", FILE_WRITE); 75 logFile.println(""); 76 logFile.println("NEW SESSION"); 77 logFile.close(); 78 79 Serial.println("Setup complete"); 80 Serial.println("initial reading"); 81 int pc = analogRead(airSensor); // read the sensor 82 Serial.println(pc); 83} 84 85/* -------------------------------------------- 86/* Each loop should comapare the reading against 87 * the moving average, and if it is greater than 88 * the specific amount, print this to the monitor 89 --------------------------------------------*/ 90void loop() 91{ 92 rtc.updateTime(); // get the time 93 int pc = analogRead(airSensor); // read the sensor 94 int avg = airAvg.reading(pc); // calculate the moving average 95// int avgPlus2 = avg + 2; // to simplify conditional below 96// unsigned long new_time = millis(); // this is to make sure peaks are spaced, in case a single count causes a double spike 97// 98 delay(10); // For some reason, the If statement that follows doesn't work without a delay here????? 99// 100// if ((pc > avgPlus2) && ((new_time - old_time) > 500)) // if the reading is greater than the average, print it 101 { 102 103 // write data to serial 104 Serial.print(rtc.stringDate()); 105 Serial.print(" "); 106 Serial.print(rtc.stringTime()); 107 Serial.print(", "); 108 Serial.print(pc); 109 Serial.print(", "); 110 Serial.println(avg); 111 112 logFile = SD.open("TRAFFIC.CSV", FILE_WRITE); // open TRAFFIC.CSV file on SD Card and write to it 113 logFile.print(rtc.stringDate()); 114 logFile.print(" "); 115 logFile.print(rtc.stringTime()); 116 logFile.print(", "); 117 logFile.print(pc); 118 logFile.print(", "); 119 logFile.println(avg); 120 logFile.close(); 121 122 123// old_time = new_time; // spacing spikes 124 125 } 126//// else 127// { 128// delay(1); // this is needed for some reason to make the IF statement work 129// 130// 131// } 132} 133 134 135
Car counter pressure test
arduino
This code allows you to see that the pressure sensor is working. It's just the main code with some bits commented out.
1/* --------------------------------------------------------------- 2 3 I am trying to record a reading when the reading from an air 4 pressure 5 sensor jumps suddenly - ie when a car drives over a tube 6 attached to the 7 sensor. The Pressure Sensor is a MPX5500DP 8 I am using the movingAvg library 9 to simplify the maths a bit. 10 ---------------------------------------------------------------*/ 11 12#include 13 <movingAvg.h> // https://github.com/JChristensen/movingAvg 14#include <SD.h> 15#include 16 <SPI.h> 17#include <Wire.h> 18#include <RV-3028-C7.h> // this 19 is a low-power clock 20 21const uint8_t airSensor(A0); // connect pressure sensor 22 from A0 pin to ground 23movingAvg airAvg(20); // define the moving average 24 object 25// 26//unsigned long new_time=0; // set some variables for preventing 27 reading multiple spikes 28unsigned long old_time=0; // 29 30RV3028 rtc; 31 // get the clock going 32 33String timestamp; // 34 35 36 37const int chipSelect = 8; // SD card pin 38int ledPin = 5; // 39 the pin the LED is connected to 40 41File logFile; // the logging file 42 43 44 45/* 46 -------------------------------------------- 47/* setup the average, pullup the 48 air sensor, 49 * begin the serial monitor and show an initial 50 * reading 51 so we knnow it is working 52 --------------------------------------------*/ 53void 54 setup() 55{ 56 Wire.begin(); 57 Serial.begin(9600); // begin 58 serial monitor 59 if (rtc.begin() == false) 60 { 61 Serial.println("Something 62 went wrong, check wiring"); 63 while (1); 64 } 65 else 66 Serial.println("RTC 67 online!"); 68 69 delay(1000); 70 pinMode(airSensor, INPUT_PULLUP); // 71 air sensor 72 73 airAvg.begin(); //averages 74 75 pinMode(chipSelect, 76 OUTPUT); 77 digitalWrite(chipSelect, HIGH); //ALWAYS pullup the ChipSelect pin 78 with the SD library 79 delay(100); 80 81 // initialize the SD card 82 83 Serial.print("Initializing SD card..."); 84 85 // make sure that the default 86 chip select pin is set to 87 // output, even if you don't use it: 88 pinMode(8, 89 OUTPUT); 90 91 // see if the card is present and can be initialized: 92 if 93 (!SD.begin(chipSelect)) { 94 Serial.println("Card failed, or not present"); 95 96 // don't do anything more: 97 return; 98 } 99 Serial.println("Card 100 initialized."); 101 Serial.print("Logging to: "); 102 Serial.println("TRAFFIC.CSV"); 103 104 logFile = SD.open("TRAFFIC.CSV", FILE_WRITE); 105 logFile.println(""); 106 107 logFile.println("NEW SESSION"); 108 logFile.close(); 109 110 Serial.println("Setup 111 complete"); 112 Serial.println("initial reading"); 113 int pc = analogRead(airSensor); 114 // read the sensor 115 Serial.println(pc); 116} 117 118/* -------------------------------------------- 119/* 120 Each loop should comapare the reading against 121 * the moving average, and 122 if it is greater than 123 * the specific amount, print this to the monitor 124 125 --------------------------------------------*/ 126void loop() 127{ 128 rtc.updateTime(); 129 // get the time 130 int pc = analogRead(airSensor); // 131 read the sensor 132 int avg = airAvg.reading(pc); // calculate the moving 133 average 134// int avgPlus2 = avg + 2; // to simplify conditional 135 below 136// unsigned long new_time = millis(); // this is to make sure peaks 137 are spaced, in case a single count causes a double spike 138// 139 delay(10); 140 // For some reason, the If statement that follows doesn't work without a delay 141 here????? 142// 143// if ((pc > avgPlus2) && ((new_time - old_time) > 500)) 144 // if the reading is greater than the average, print it 145 { 146 147 // 148 write data to serial 149 Serial.print(rtc.stringDate()); 150 Serial.print(" 151 "); 152 Serial.print(rtc.stringTime()); 153 Serial.print(", "); 154 Serial.print(pc); 155 156 Serial.print(", "); 157 Serial.println(avg); 158 159 logFile = SD.open("TRAFFIC.CSV", 160 FILE_WRITE); // open TRAFFIC.CSV file on SD Card and write to it 161 logFile.print(rtc.stringDate()); 162 163 logFile.print(" "); 164 logFile.print(rtc.stringTime()); 165 logFile.print(", 166 "); 167 logFile.print(pc); 168 logFile.print(", "); 169 logFile.println(avg); 170 171 logFile.close(); 172 173 174// old_time = new_time; // spacing spikes 175 176 177 } 178//// else 179// { 180// delay(1); // this is needed for some 181 reason to make the IF statement work 182// 183// 184// } 185} 186 187 188
Car Counter Full Code
arduino
This code counts each time the pressure sensor is triggered
1/* --------------------------------------------------------------- 2 3 I am trying to record a reading when the reading from an air 4 pressure 5 sensor jumps suddenly - ie when a car drives over a tube 6 attached to the 7 sensor. The Pressure Sensor is a MPX5500DP 8 I am using the movingAvg library 9 to simplify the maths a bit. 10 Sarah Dalrymple - Aetos999 11 ---------------------------------------------------------------*/ 12 13#include 14 <movingAvg.h> // https://github.com/JChristensen/movingAvg 15#include <SD.h> 16#include 17 <SPI.h> 18#include <Wire.h> 19#include <RV-3028-C7.h> // https://github.com/constiko/RV-3028_C7-Arduino_Library 20 21const 22 uint8_t airSensor(A0); // connect pressure sensor from A0 pin to ground 23 24// 25 This is the moving average - change the figure in brackets to what you want 26 27movingAvg 28 airAvg(20); 29 30unsigned long new_time=0; // set some variables for 31 preventing reading multiple spikes 32unsigned long old_time=0; // 33 34RV3028 35 rtc; // get the clock going 36 37String timestamp; // 38 39 40const int chipSelect = 8; // SD card pin 41int ledPin = 5; // 42 the pin the LED is connected to 43 44File logFile; // the logging file 45 46 47 48/* 49 -------------------------------------------- 50/* setup the average, pullup the 51 air sensor, 52 * begin the serial monitor and show an initial 53 * reading 54 so we knnow it is working 55 --------------------------------------------*/ 56void 57 setup() 58{ 59 Wire.begin(); 60 Serial.begin(9600); // begin 61 serial monitor 62 if (rtc.begin() == false) 63 { 64 Serial.println("Something 65 went wrong, check wiring"); 66 while (1); 67 } 68 else 69 Serial.println("RTC 70 online!"); 71 72 delay(1000); 73 pinMode(airSensor, INPUT_PULLUP); // 74 air sensor 75 76 airAvg.begin(); //averages 77 78 pinMode(chipSelect, 79 OUTPUT); 80 digitalWrite(chipSelect, HIGH); //ALWAYS pullup the ChipSelect pin 81 with the SD library 82 delay(100); 83 84 // initialize the SD card 85 86 Serial.print("Initializing SD card..."); 87 88 // make sure that the default 89 chip select pin is set to 90 // output, even if you don't use it: 91 pinMode(8, 92 OUTPUT); 93 94 // see if the card is present and can be initialized: 95 if 96 (!SD.begin(chipSelect)) { 97 Serial.println("Card failed, or not present"); 98 99 // don't do anything more: 100 return; 101 } 102 Serial.println("Card 103 initialized."); 104 Serial.print("Logging to: "); 105 Serial.println("TRAFFIC.CSV"); 106 107 logFile = SD.open("TRAFFIC.CSV", FILE_WRITE); 108 logFile.println(""); 109 110 logFile.println("NEW SESSION"); 111 logFile.close(); 112 113 Serial.println("Setup 114 complete"); 115 Serial.println("initial reading"); 116 int pc = analogRead(airSensor); 117 // read the sensor 118 Serial.println(pc); 119} 120 121/* -------------------------------------------- 122/* 123 Each loop should comapare the reading against 124 * the moving average, and 125 if it is greater than 126 * the specific amount, print this to the monitor 127 128 --------------------------------------------*/ 129void loop() 130{ 131 rtc.updateTime(); 132 // get the time 133 int pc = analogRead(airSensor); // 134 read the sensor 135 int avg = airAvg.reading(pc); // calculate the moving 136 average 137 int avgPlus = avg + 5; // to simplify conditional below 138 139 unsigned long new_time = millis(); // this is to make sure peaks are spaced, 140 in case a single count causes a double spike 141 142 delay(1); // For some 143 reason, the If statement that follows doesn't work without a delay here?? I think 144 this is a bug in my system 145 146 // if the reading is greater than the average 147 & however many ms has passed since last time, print it. 148 // This is the ms value 149 between peaks - change it to help calibrate your counter 150 if ((pc > avgPlus) 151 && ((new_time - old_time) > 400)) 152 { 153 154 // write data to serial 155 156 Serial.print(rtc.stringDate()); 157 Serial.print(" "); 158 Serial.print(rtc.stringTime()); 159 160 Serial.print(", "); 161 Serial.print(pc); 162 Serial.print(", "); 163 164 Serial.println(avg); 165 166 logFile = SD.open("TRAFFIC.CSV", FILE_WRITE); 167 // open TRAFFIC.CSV file on SD Card and write to it 168 Serial.println("log"); 169 170 logFile.print(rtc.stringDate()); 171 logFile.print(" "); 172 logFile.print(rtc.stringTime()); 173 174 logFile.print(", "); 175 logFile.print(pc); 176 logFile.print(", "); 177 178 logFile.println(avg); 179 logFile.close(); 180 Serial.println("done."); 181 182 183 old_time = new_time; // spacing spikes 184 185 } 186 else 187 { 188 delay(1); 189 // this is needed for some reason to make the IF statement work 190 191 192 193 } 194} 195 196
New! Overnight sleep
arduino
I've added a few lines and rearranged the code so the arduino goes to sleep at night, as we are only open 10am - 5pm. This saves a bit of battery! you need to physically connect pin 2 on the arduino to the alm or int pin on the RTC.
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 8am 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 17RV3028 rtc; // get the clock going 18 19const uint8_t airSensor(A0); // connect pressure sensor from A0 pin to ground 20 21const int wakeUpPin = 2; // Use pin2 to wake up. 22 23//The below variables control what the alarm will be set to. See the RV-3028 library for more details 24int alm_minute = 28; // this is ignored 25int alm_hour = 8; // wake up at 8am 26int alm_date_or_weekday = 2; // this is ignored 27bool alm_isweekday = false; // this is ignored 28uint8_t alm_mode = 5; // this mode means we just check when the hours match = alarm 29 30movingAvg airAvg(20); // sets the moving average - change the figure in brackets to what you want 31 32unsigned long new_time = 0; // set some variables for preventing reading multiple spikes 33unsigned long old_time = 0; // 34 35String timestamp; // 36 37const int chipSelect = 8; // SD card pin 38int ledPin = 5; // the pin the LED is connected to 39 40File logFile; // the logging file 41 42 43void wakeUp() 44{ 45 // Just a handler for the pin interrupt. 46} 47 48/* -------------------------------------------- 49 /* setup the average, pullup the air sensor, 50 begin the serial monitor and show an initial 51 reading so we knnow it is working 52 --------------------------------------------*/ 53void setup() 54{ 55 pinMode(wakeUpPin, INPUT_PULLUP); // Configure wake up pin as input. 56 57 Wire.begin(); 58 Serial.begin(9600); // begin serial monitor 59 if (rtc.begin() == false) 60 { 61 Serial.println("Something went wrong, check wiring"); 62 while (1); 63 } 64 else 65 Serial.println("RTC online!"); 66 //Enable alarm interrupt 67 rtc.enableAlarmInterrupt(alm_minute, alm_hour, alm_date_or_weekday, alm_isweekday, alm_mode); 68 69 70 delay(1000); 71 72 pinMode(airSensor, INPUT_PULLUP); // air sensor 73 74 airAvg.begin(); //averages 75 76 pinMode(chipSelect, OUTPUT); 77 digitalWrite(chipSelect, HIGH); //ALWAYS pullup the ChipSelect pin with the SD library 78 delay(100); 79 80 // initialize the SD card 81 Serial.print("Initializing SD card..."); 82 83 // make sure that the default chip select pin is set to 84 // output, even if you don't use it: 85 pinMode(8, OUTPUT); 86 87 // see if the card is present and can be initialized: 88 if (!SD.begin(chipSelect)) { 89 Serial.println("Card failed, or not present"); 90 // don't do anything more: 91 return; 92 } 93 Serial.println("Card initialized."); 94 Serial.print("Logging to: "); 95 Serial.println("TRAFFIC.CSV"); 96 logFile = SD.open("TRAFFIC.CSV", FILE_WRITE); 97 logFile.println(""); 98 logFile.println("NEW SESSION"); 99 logFile.close(); 100 101 Serial.println("Setup complete"); 102 Serial.println("initial reading"); 103 int pc = analogRead(airSensor); // read the sensor 104 Serial.println(pc); 105 delay(250); 106} 107 108/* -------------------------------------------- 109 /* Each loop should comapare the reading against 110 the moving average, and if it is greater than 111 the specific amount, it records to SD and serial 112 --------------------------------------------*/ 113void loop() 114{ 115 rtc.updateTime(); // get the time 116 int hour = rtc.getHours(); // get the hour 117 if (hour == 22) // if it's a match, go to sleep 118 { 119 Serial.println("sleep time"); 120 GoToSleep(); //go to the sleep void 121 } 122 CountCars(); // or just keep counting the cars 123} 124 125void GoToSleep() 126{ 127 // Allow wake up pin to trigger interrupt on low. 128 attachInterrupt(0, wakeUp, LOW); // go to the wakeUp void when alarm triggers 129 // Enter power down state with ADC and BOD module disabled. 130 // Wake up when wake up pin is low. 131 logFile = SD.open("TRAFFIC.CSV", FILE_WRITE); // open TRAFFIC.CSV file on SD Card and write to it, plus a few lines to know it's all working. you can remove these once you are happy it is 132 logFile.print("went to sleep"); 133 logFile.print(","); 134 logFile.print(rtc.stringDate()); 135 logFile.print("-"); 136 logFile.println(rtc.stringTime()); 137 logFile.close(); 138 Serial.println("sleep time"); 139 delay(50); 140 LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); 141 142 // Wakes up here! 143 detachInterrupt(0); //disable the interupt pin 144 145 Serial.println("alarm"); //let me know we woke up OK 146 logFile = SD.open("TRAFFIC.CSV", FILE_WRITE); // open TRAFFIC.CSV file on SD Card and write to it 147 logFile.print("woke up"); 148 logFile.print(","); 149 logFile.print(rtc.stringDate()); 150 logFile.print("-"); 151 logFile.println(rtc.stringTime()); 152 logFile.close(); 153 delay(50); 154 155 CountCars(); //go back to counting cars 156} 157 158void CountCars() 159{ 160 rtc.updateTime(); // get the time 161 int pc = analogRead(airSensor); // read the sensor 162 int avg = airAvg.reading(pc); // calculate the moving average 163 int avgPlus = avg + 5; // to simplify conditional below 164 unsigned long new_time = millis(); // this is to make sure spikes are spaced, in case a single count causes a double spike 165 166 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 167 168 // if the reading is greater than the average & however many ms has passed since last time, print it. 169 // This is the ms value between spikes - change it to help calibrate your counter 170 // eg don't count a spike if it occurs within 400ms of the last one 171 if ((pc > avgPlus) && ((new_time - old_time) > 400)) 172 { 173 // write data to serial 174 Serial.print(rtc.stringDate()); 175 Serial.print(" "); 176 Serial.print(rtc.stringTime()); 177 Serial.print(", "); 178 Serial.print(pc); 179 Serial.print(", "); 180 Serial.println(avg); 181 182 // write data to the csv file 183 logFile = SD.open("TRAFFIC.CSV", FILE_WRITE); // open TRAFFIC.CSV file on SD Card and write to it 184 Serial.println("log"); 185 logFile.print(rtc.stringDate()); 186 logFile.print(","); 187 logFile.print(rtc.stringTime()); 188 logFile.print(","); 189 logFile.print(pc); 190 logFile.print(","); 191 logFile.println(avg); 192 logFile.close(); 193 Serial.println("done."); 194 195 old_time = new_time; // spacing spikes 196 197 } 198 else 199 { 200 delay(5); 201 202 } 203 204}
Car Counter Full Code
arduino
This code counts each time the pressure sensor is triggered
1/* --------------------------------------------------------------- 2 I am trying to 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 Sarah Dalrymple - Aetos999 7 ---------------------------------------------------------------*/ 8 9#include <movingAvg.h> // https://github.com/JChristensen/movingAvg 10#include <SD.h> 11#include <SPI.h> 12#include <Wire.h> 13#include <RV-3028-C7.h> // https://github.com/constiko/RV-3028_C7-Arduino_Library 14 15const uint8_t airSensor(A0); // connect pressure sensor from A0 pin to ground 16 17// This is the moving average - change the figure in brackets to what you want 18 19movingAvg airAvg(20); 20 21unsigned long new_time=0; // set some variables for preventing reading multiple spikes 22unsigned long old_time=0; // 23 24RV3028 rtc; // get the clock going 25 26String timestamp; // 27 28const int chipSelect = 8; // SD card pin 29int ledPin = 5; // the pin the LED is connected to 30 31File logFile; // the logging file 32 33 34 35/* -------------------------------------------- 36/* setup the average, pullup the air sensor, 37 * begin the serial monitor and show an initial 38 * reading so we knnow it is working 39 --------------------------------------------*/ 40void setup() 41{ 42 Wire.begin(); 43 Serial.begin(9600); // begin serial monitor 44 if (rtc.begin() == false) 45 { 46 Serial.println("Something went wrong, check wiring"); 47 while (1); 48 } 49 else 50 Serial.println("RTC online!"); 51 52 delay(1000); 53 pinMode(airSensor, INPUT_PULLUP); // air sensor 54 55 airAvg.begin(); //averages 56 57 pinMode(chipSelect, OUTPUT); 58 digitalWrite(chipSelect, HIGH); //ALWAYS pullup the ChipSelect pin with the SD library 59 delay(100); 60 61 // initialize the SD card 62 Serial.print("Initializing SD card..."); 63 64 // make sure that the default chip select pin is set to 65 // output, even if you don't use it: 66 pinMode(8, OUTPUT); 67 68 // see if the card is present and can be initialized: 69 if (!SD.begin(chipSelect)) { 70 Serial.println("Card failed, or not present"); 71 // don't do anything more: 72 return; 73 } 74 Serial.println("Card initialized."); 75 Serial.print("Logging to: "); 76 Serial.println("TRAFFIC.CSV"); 77 logFile = SD.open("TRAFFIC.CSV", FILE_WRITE); 78 logFile.println(""); 79 logFile.println("NEW SESSION"); 80 logFile.close(); 81 82 Serial.println("Setup complete"); 83 Serial.println("initial reading"); 84 int pc = analogRead(airSensor); // read the sensor 85 Serial.println(pc); 86} 87 88/* -------------------------------------------- 89/* Each loop should comapare the reading against 90 * the moving average, and if it is greater than 91 * the specific amount, print this to the monitor 92 --------------------------------------------*/ 93void loop() 94{ 95 rtc.updateTime(); // get the time 96 int pc = analogRead(airSensor); // read the sensor 97 int avg = airAvg.reading(pc); // calculate the moving average 98 int avgPlus = avg + 5; // to simplify conditional below 99 unsigned long new_time = millis(); // this is to make sure peaks are spaced, in case a single count causes a double spike 100 101 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 102 103 // if the reading is greater than the average & however many ms has passed since last time, print it. 104 // This is the ms value between peaks - change it to help calibrate your counter 105 if ((pc > avgPlus) && ((new_time - old_time) > 400)) 106 { 107 108 // write data to serial 109 Serial.print(rtc.stringDate()); 110 Serial.print(" "); 111 Serial.print(rtc.stringTime()); 112 Serial.print(", "); 113 Serial.print(pc); 114 Serial.print(", "); 115 Serial.println(avg); 116 117 logFile = SD.open("TRAFFIC.CSV", FILE_WRITE); // open TRAFFIC.CSV file on SD Card and write to it 118 Serial.println("log"); 119 logFile.print(rtc.stringDate()); 120 logFile.print(" "); 121 logFile.print(rtc.stringTime()); 122 logFile.print(", "); 123 logFile.print(pc); 124 logFile.print(", "); 125 logFile.println(avg); 126 logFile.close(); 127 Serial.println("done."); 128 129 old_time = new_time; // spacing spikes 130 131 } 132 else 133 { 134 delay(1); // this is needed for some reason to make the IF statement work 135 136 137 } 138} 139 140
Downloadable files
Arduino Car Counter
This is a rough approximation of the circuit; the RTC and the pressure sensor aren't the models you use in my project but they have the same connections. (I'm rubbish at fritzing, half of the parts aren't in the library and nothing lines up with the holes. )
Arduino Car Counter
Car Counter Fritzing
Car Counter Fritzing
Arduino Car Counter
This is a rough approximation of the circuit; the RTC and the pressure sensor aren't the models you use in my project but they have the same connections. (I'm rubbish at fritzing, half of the parts aren't in the library and nothing lines up with the holes. )
Arduino Car Counter
Comments
Only logged in users can leave comments