Please be aware of all shipping deadlines before placing your order - we cannot guarantee orders will arrive before Christmas!

Vehicle Traffic Counter with UNO R3, WingShield, Datalogger and RGB LCD
Moderators: adafruit_support_bill, adafruit

Vehicle Traffic Counter with UNO R3, WingShield, Datalogger and RGB LCD

by KietCallies on Thu Sep 19, 2013 6:46 pm

Using MPX5500DP Freescale Board Mount Pressure / Force Sensors (5V sensors), I am trying to create a vehicle traffic tube counter, logging the count with the Datalogger Shield and displaying the count in realtime on the RGB LCD Shield. I utilize a WingShield to gain easy access to the Arduino UNO R3 pins. I started small. Get the Arduino to read the pressure for the sensor. Good. Then display the a count each time there is a pressure difference. Good. I then tried it with two sensors, to see if I could count multiple lanes. Good.

However, when I add the Datalogger to the equation with multiple sensors, problem. The timestamp was being recorded as erratic gibberish. The display would display the result of one tube then not the other. The card would initialize, but not write any data to the SD card. I have tested the voltage going to the sensors (5V). I have tested the voltage at the LCD Shield (top of the stack), 5V. Am I missing something?

Individually, the sensors work flawless. Together erratic results. Ultimately, I would like to count traffic on three different lanes. Any suggestions?

Here is my code for two lane counter:

Code: Select all | TOGGLE FULL SIZE
// include the library code:
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>

// The shield uses the I2C SCL and SDA pins. On classic Arduinos
// this is Analog 4 and 5 so you can't use those for analogRead() anymore
// However, you can connect other I2C sensors to the I2C bus and share
// the I2C bus.
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
RTC_DS1307 rtc; // define the Real Time Clock object

// These #defines make it easy to set the backlight color
#define RED 0x1
#define YELLOW 0x3
#define GREEN 0x2
#define TEAL 0x6
#define BLUE 0x4
#define VIOLET 0x5
#define WHITE 0x7

#define ECHO_TO_SERIAL   1 // echo data to serial port
#define WAIT_TO_START    0 // Wait for serial input in setup()

// these constants won't change:
const int ledPin = 13;      // led connected to digital pin 13
const int airSensorLane1 = A0; // the Lane 1 air pressure differential sensor is connected to analog pin 0
const int airSensorLane2 = A1; // the Lane 1 air pressure differential sensor is connected to analog pin 0
const int chipSelect = 10; // for the data logging shield, we use digital pin 10 for the SD cs line

File logfile; // the logging file

String timestamp;


// these variables will change:
int sensorReadingLane1 = 0;      // variable to store the value read from the sensor pin A0
int sensorReadingLane2 = 0;      // variable to store the value read from the sensor pin A0
int ledState = LOW;         // variable used to store the last LED status, to toggle the light
int initialLane1 = 0;
int initialLane2 = 0;
int countLane1 = 0;
int countLane2 = 0;

void setup() {
  pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
 
  Serial.begin(9600);       // use the serial port
 
  Serial.println("Starting setup...");
  delay(5000);
 
  initialLane1 = analogRead(airSensorLane1);
  initialLane2 = analogRead(airSensorLane2);
 
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  lcd.setBacklight(RED);
 
  // 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(10, OUTPUT);
 
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("CARD FAILURE!");
    // don't do anything more:
    return;
  }
  Serial.println("Card initialized."); 
  Serial.print("Logging to: ");
  Serial.println("TRAFFIC.CSV");
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Card initialized");
  lcd.setCursor(0,1);
  lcd.print("TRAFFIC.CSV");
 
  logfile = SD.open("TRAFFIC.CSV", FILE_WRITE);
  logfile.println("");
  logfile.println("NEW SESSION");
  //#if ECHO_TO_SERIAL
    //Serial.println("Creating header: milliseconds,time,lane,pressure,initialPressure");
  //#endif
 
  Wire.begin(); 
  if (!rtc.begin()) {
    if (logfile) {
      logfile.println("RTC failed");
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("RTC FAILURE!");
    }
    Serial.println("RTC failed");
  }
  logfile.close();
  Serial.println("Setup complete");
  Serial.print("Initial Lane 1 Pressure Reading:");
  Serial.println(initialLane1);
  Serial.print("Initial Lane 2 Pressure Reading:");
  Serial.println(initialLane2);
}

void loop() { 
  DateTime currentTimeDate = rtc.now(); // get current timestamp
 
  // read the sensor and store it in the variable sensorReading:
  sensorReadingLane1 = analogRead(airSensorLane1);
 
  uint8_t buttons = lcd.readButtons();
 
  if (buttons & BUTTON_UP) {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("RESETTING COUNT");
    delay(5000);
    countLane1 = 0;
  }
  if (buttons & BUTTON_DOWN) {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Lane1 Count:");
    lcd.print(countLane1);
    lcd.setCursor(0,1);
    lcd.print("Lane2 Count:");
    lcd.print(countLane2);
  }
 
  // if the sensor reading is greater than the threshold:
  if (sensorReadingLane1 > initialLane1 + 2) {
   
    ledState = !ledState;   // toggle the status of the ledPin:
    digitalWrite(ledPin, ledState); // update the LED pin itself: 
   
    // log milliseconds since starting
    uint32_t m = millis();     
   
    // log time
    timestamp = String(currentTimeDate.year());
    timestamp += "/";
    timestamp += String(currentTimeDate.month());
    timestamp += "/";
    timestamp += String(currentTimeDate.day());
    timestamp += " ";
    timestamp += String(currentTimeDate.hour());
    timestamp += ":";
    timestamp += String(currentTimeDate.minute());
    timestamp += ":";
    timestamp += String(currentTimeDate.second());
    Serial.println(timestamp);
   
    ++countLane1; // increase the Lane 1 Count
    if (!SD.begin(chipSelect)) {logfile = SD.open("TRAFFIC.CSV", FILE_WRITE);} // open TRAFFIC.CSV file on SD Card
    if (logfile) {
      logfile.print(m);
      logfile.print(", ");
      logfile.print(timestamp);
      logfile.print(", 1, ");
      logfile.print(sensorReadingLane1);
      logfile.print(", ");
      logfile.println(initialLane1);
    } 
    //else {
      //lcd.clear();
      //lcd.setCursor(0,0);
      //lcd.print("UNABLE TO WRITE");
      //lcd.setCursor(0,1);
      //lcd.print("TO SD CARD!");
    //} // write data to TRAFFIC.CSV
    if (!SD.begin(chipSelect)) {logfile.close();}
    #if ECHO_TO_SERIAL
      Serial.print(m);
      Serial.print(", ");
      Serial.print(timestamp);
      Serial.print(", 1, ");
      Serial.print(sensorReadingLane1);
      Serial.print(", ");
      Serial.println(initialLane1);
      Serial.print("Lane 1 Air Differential Pressure Sensor Reading: ");
      Serial.print(sensorReadingLane1);
      Serial.print(" of ");
      Serial.println(initialLane1);
    #endif // echo to serial monitor
   
    // update LCD screen
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Lane 1:");
    lcd.print(countLane1);
    lcd.setCursor(0,1);
    lcd.print("Reading:");
    lcd.print(sensorReadingLane1);
    lcd.print(" [");
    lcd.print(initialLane1);
    lcd.print("]");     

  }
  // if the sensor reading is greater than the threshold:
  if (sensorReadingLane2 > initialLane2 + 2) {
   
    ledState = !ledState;   // toggle the status of the ledPin:
    digitalWrite(ledPin, ledState); // update the LED pin itself: 
   
    // log milliseconds since starting
    uint32_t m = millis();     
   
    // log time
    timestamp = String(currentTimeDate.year());
    timestamp += "/";
    timestamp += String(currentTimeDate.month());
    timestamp += "/";
    timestamp += String(currentTimeDate.day());
    timestamp += " ";
    timestamp += String(currentTimeDate.hour());
    timestamp += ":";
    timestamp += String(currentTimeDate.minute());
    timestamp += ":";
    timestamp += String(currentTimeDate.second());
    Serial.println(timestamp);
   
    ++countLane2; // increase the Lane 2 Count
    if (!SD.begin(chipSelect)) {logfile = SD.open("TRAFFIC.CSV", FILE_WRITE);} // open TRAFFIC.CSV file on SD Card
    if (logfile) {
      logfile.print(m);
      logfile.print(", ");
      logfile.print(timestamp);
      logfile.print(", 2, ");
      logfile.print(sensorReadingLane2);
      logfile.print(", ");
      logfile.println(initialLane2);
    } 
    //else {
      //lcd.clear();
      //lcd.setCursor(0,0);
      //lcd.print("UNABLE TO WRITE");
      //lcd.setCursor(0,1);
      //lcd.print("TO SD CARD!");
    //} // write data to TRAFFIC.CSV
    if (!SD.begin(chipSelect)) {logfile.close();}
    #if ECHO_TO_SERIAL
      Serial.print(m);
      Serial.print(", ");
      Serial.print(timestamp);
      Serial.print(", 2, ");
      Serial.print(sensorReadingLane2);
      Serial.print(", ");
      Serial.println(initialLane2);
      Serial.print("Lane 2 Air Differential Pressure Sensor Reading: ");
      Serial.print(sensorReadingLane2);
      Serial.print(" of ");
      Serial.println(initialLane2);
    #endif // echo to serial monitor
   
    // update LCD screen
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Lane 2:");
    lcd.print(countLane2);
    lcd.setCursor(0,1);
    lcd.print("Reading:");
    lcd.print(sensorReadingLane2);
    lcd.print(" [");
    lcd.print(initialLane2);
    lcd.print("]");     

  }
 
  delay(100);  // delay to avoid overloading the serial port buffer
}
KietCallies
 
Posts: 5
Joined: Fri Jan 25, 2013 3:51 pm

Re: Vehicle Traffic Counter with UNO R3, WingShield, Datalogger and RGB LCD

by adafruit_support_bill on Thu Sep 19, 2013 8:47 pm

Please do not double post. Continue this in your original thread.: viewtopic.php?f=8&t=43786#p218285
User avatar
adafruit_support_bill
 
Posts: 32638
Joined: Sat Feb 07, 2009 10:11 am