Go Down

Topic: Trying to build a device that senses cars passing by (Read 148 times) previous topic - next topic

Hello all!  I'm purchasing my first Arduino with it, and the first thing that I'm trying to do is get it to recognize vehicles passing by within a 8-12 foot range and then ring a bell when one passes by.  I'm not sure how to start this project, or what type of sensors I should use.  Does anyone have any advice?  Thank you so much!


JimboZA

Ultrasound sensors might be the easiest, this has a range of 4m, this of 6.
meArm build blog:     http://jimbozamearm.blogspot.com/

Thanks a lot!  Loud noises shouldn't interfere with the signal, right? 

JimboZA

Quote
Loud noises shouldn't interfere with the signal, right?
Not if "ultra" means what I think it means, no....  ;)

There's loads of code for those things by the way: vendor sites like adafruit and sparkfun probably have, and there's some in the Playground, iirc.

A thing you need to think about: the Arduino will loop faster than the car gets out of the beam, so the code needs to set a flag when it sees the beam respond so that next time through loop() it doesn't think it's a new vehicle. Then clear the flag when the beam is clear, ready for the next vehicle.

edit.... although that said, just setting the bell pin high while the vehicle is there may be enough for the alert, and you're not counting the vehicles are you? Then you needn't worry about the foregoing.
meArm build blog:     http://jimbozamearm.blogspot.com/

primus852

I am currently in the process of creating a very similar thing, only for RC cars and mainly as a laptimer/counter.

I have written some code already, but as the parts have not arrived yet, no guarantee for it to work :) Additonal Note: I am also kind of new to the whole Arduino thing, so don't yell at me for bad coding.

Code: [Select]
// ---------------------------------------------------------------------------
// See: http://www.instructables.com/id/Ultrasonic-Range-detector-using-Arduino-and-the-SR/?lang=de
// External libraries used: Sainsmart LCDKeys (https://github.com/Phaiax/sainsmartkeypad // NewPing (UltraSonic): https://code.google.com/p/arduino-new-ping/
// ---------------------------------------------------------------------------

#include <NewPing.h>
#include <LiquidCrystal.h>
#include <sainsmartkeypad.h>

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

//Global Vars
boolean raceStarted       = false;
unsigned int lapStart     = 0;
unsigned int lastLap      = 0;
unsigned int bestLapTime  = 0;
unsigned int elapsedTime  = 0;
unsigned int initialDist  = 0;
int minWait               = 2000; // Wait in ms before turning on Sonar again

//LCD Sepcific
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
SainsmartKeypad keypad(0);
int value = 0;
uint8_t key;

//Ultrasonic Sensor
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  Serial.begin(115200);
  //WHY NOT Serial.begin(9600);
  Serial.println("Laptimer Ready");
  initialDist = (sonar.ping() / US_ROUNDTRIP_CM);
  //Debugging
  Serial.print("Initial Distance: ");
  Serial.print(initialDist);
  Serial.println("cm");
 
}

void loop() {
 
  //Sainsmart Library Setup // ONLY FOR TESTING CURRENTLY
  key = keypad.getKey_fastscroll();
  if(key != SAMPLE_WAIT) // Do not refresh screen every loop
  {
     switch(key)
     {
       case UP_KEY:
        value++;
        break;
       case DOWN_KEY:
        value--;
        break;
     }
     lcd.setCursor(5,1);
     lcd.print(value);
     lcd.print("        ");
  }
  delay(30); //29ms should be the shortest delay between pings.
  unsigned int distance = (sonar.ping() / US_ROUNDTRIP_CM);

  //First contact with Sensor
  if (distance > 0 && distance != initialDist && raceStarted == false){
    //GO!
    Serial.println("Race Started!");

    //Debugging
    Serial.print("Detected ");
    Serial.print(distance);
    Serial.println("cm");

    //Start the Race
    raceStarted = true;
   
    //Start the Timer
    lapStart = millis();
  }
 
   //Check for elapsed time
   elapsedTime = millis() - lapStart;
 
  //Nth contact with the sensor (after minWait)
  if (distance > 0 && distance != initialDist && raceStarted == true && elapsedTime > minWait){
    //Stop the current Laptime
    lastLap = elapsedTime;
   
    //Start the new Lap
    lapStart = millis();
   
    //Convert to readable Time and Print
    showTime(lastLap, 1);
   
    //Check if new best Lap
    //-->FirstRun
    if(bestLapTime == 0){
      showTime(lastLap, 2);
    }
    //-->Nth run
    if(lastLap < bestLapTime){
      showTime(lastLap, 2);
    }
  }
}


void showTime(unsigned long t_milli, unsigned int lcdLine){
   char buffer[20];
   int days, hours, mins, secs;
   int fractime;
   unsigned long inttime;

   inttime  = t_milli / 1000;
   fractime = t_milli % 1000;

   days     = inttime / (24*3600);
   inttime  = inttime % (24*3600);

   hours    = inttime / 3600;
   inttime  = inttime % 3600;

   mins     = inttime / 60;
   inttime  = inttime % 60;

   secs = inttime;

   sprintf(buffer, "%02d:%02d.%03d", mins, secs, fractime);
   //Determine which Line to print the Result
   if (lcdLine == 1){
     Serial.println(buffer); //LCD Line 1
   }
   if (lcdLine == 2){
     Serial.println(buffer); //LCD Line 2
   }
}


I use the SainSmart LCD/Key Shield

PM/Reply if you need some more help

Cheers,

jremington

A magnetometer (electronic compass) will sense the steel in automobile bodies several meters away.

Thank you so much for the help, guys! I bought 2 of these:

http://www.miniinthebox.com/ultrasonic-sensor-hc-sr04-distance-measuring-module_p903315.html?currency=USD&litb_from=paid_adwords_shopping&gclid=CjwKEAjwo5OpBRDU64qO07OXq00SJADn5hYnuRxshkN25oRpxizwu_BEX8wrzTal56vhe2ssnXhftBoC7UTw_wcB

Does anyone know where I can find the information I need on what to buy to hook these babies up to the Arduino Uno and code them?  Thank you!

JimboZA

Did you dig in the Playground as I suggested?...... there you will find this.
meArm build blog:     http://jimbozamearm.blogspot.com/

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy