Skip to content

ALTO – Arduino Parking Assistant

Arduino controlled stop light using 2 PING))) Ultrasonic Sensors.  Sensors detect which lane of the two lane garage is active and lights the lamp accordingly.  Red threshold as well as maximum distance PING can see is hard coded.  Logic is also placed to avoid false changes, such as the Yellow lamp cannot illuminate unless Green is currently lit – to avoid a mishap if the PING sensor measurement jumps from hitting obscure portions of the car.

Inside Stop Light

Arduino Protosheild for ALTO Project

ALTO - Arduino Parking Assistant

const int relayRed = 13;
const int relayYellow = 12;
const int relayGreen = 11;

const int pingPinLeft = 7;
const int pingPinRight = 8;

int distanceLeft = 0;
int distanceRight = 0;

int previousLeft = 0;
int previousRight = 0;

String previousColor = "OFF";

int thresholdLeft = 58;
int thresholdRight = 58;

int greenLeft = 144;
int greenRight = 133;

String color = " ";
int similiarColor = 0;
int timeout = 250;

void setup() {
  Serial.begin(9600);
  pinMode(relayRed, OUTPUT);
  pinMode(relayYellow, OUTPUT);
  pinMode(relayGreen, OUTPUT);
  digitalWrite(relayRed, HIGH);
  digitalWrite(relayYellow, HIGH);
  digitalWrite(relayGreen, HIGH);
  delay(1000);
  digitalWrite(relayRed, LOW);
  digitalWrite(relayYellow, LOW);
  digitalWrite(relayGreen, LOW);
}

void loop()
{
  distanceLeft = pingDistance(pingPinLeft);
  distanceRight = pingDistance(pingPinRight);
  
  String colorLeft = determineColor(distanceLeft, thresholdLeft, greenLeft, previousLeft, previousColor, "LEFT");
  String colorRight = determineColor(distanceRight, thresholdRight, greenRight, previousRight, previousColor, "RIGHT");
 
if(((colorLeft == "RED" && colorRight == "RED") || (colorLeft == "OFF" && colorRight == "RED") || (colorLeft == "RED" && colorRight == "OFF")) && similiarColor < timeout) {
   digitalWrite(relayRed, HIGH);
   digitalWrite(relayYellow, LOW);
   digitalWrite(relayGreen, LOW);
   if (color == "RED") {
         color = previousColor = "RED";
         similiarColor++;
       } else {
         color = previousColor = "RED";
         similiarColor = 0;
       }
 } else if((colorLeft == "OFF" && colorRight == "OFF") || similiarColor >= timeout) {
   digitalWrite(relayRed, LOW);
   digitalWrite(relayYellow, LOW);
   digitalWrite(relayGreen, LOW);
   color = previousColor = "OFF";
 } else if(colorLeft == "YELLOW" || colorRight == "YELLOW") {
   digitalWrite(relayRed, LOW);
   digitalWrite(relayYellow, HIGH);
   digitalWrite(relayGreen, LOW);
   color = previousColor = "YELLOW";
   similiarColor = 0;
 } else if(colorLeft == "GREEN" || colorRight == "GREEN"){
   digitalWrite(relayRed, LOW);
   digitalWrite(relayYellow, LOW);
   digitalWrite(relayGreen, HIGH);
   color = previousColor = "GREEN";
   similiarColor = 0;
 } else {
   color = "NO CHANGE";
   similiarColor++;
 }
 
delay(50);
}


String determineColor(int distance, int threshold, int green, int previous, String previousColor, String side) {
   
    String returnColor;
     if (distance >= previous + 5 || distance <= previous - 5 ) {
       returnColor = "VOID";
     } else if(distance <= threshold) {
       if(previousColor == "YELLOW" || previousColor == "RED") {returnColor = "RED";} else { returnColor = "VOID"; }
     } else if(distance > green) {
       returnColor = "OFF";
     } else if (distance == previous) {
       returnColor = "OFF";
     }else if(distance < 1.5*threshold) {
       if(previousColor == "GREEN" || previousColor == "YELLOW") {returnColor = "YELLOW";} else { returnColor = "VOID"; }
       similiarColor = 0;
     } else if(distance < green) {
       if(previousColor == "OFF" || previousColor == "GREEN") {returnColor = "GREEN";} else { returnColor = "VOID"; }
       similiarColor = 0;
     }
     if (side == "LEFT") {
        previousLeft = distance; 
     } else {
        previousRight = distance; 
     }
     return returnColor;
}


long pingDistance(long pingPin)
{
  long duration, inches, cm;

  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  return microsecondsToInches(duration); 
}
long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}
Find this useful? Take just a moment and give a $1. Thanks!
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
  • Philv32

    Nice setup. How do I buy this? I have the light just need the controller

    [Reply]

    Sators reply on August 2nd, 2012:

    Well, it’s not a retail item as I bought all of the individual pieces and soldered it and coded it from scratch…but all of the hardware is available at http://www.sparkfun.com if you’re wanting to make one yourself.  Though, if you’re looking for one to be made custom for you, hit me up with a email at [email protected]

    [Reply]

  • Mrjtwill

    Where do you get a simple traffic light to hack/mod?  Everything I have found online is $28/$35 or more..

    [Reply]

    Sators reply on August 8th, 2012:

    I found mine on Craigslist for $75.

    [Reply]

  • Giacomo

    Hi,
    thank you for sharing this project. I made something similar, but without the traffic light. Can you suggest me some shops to buy it at low price ? Thank you

    [Reply]

    Sators reply on November 9th, 2014:

    Cool – good for you! I found the stop light I am using from Craigslist…good luck!

    [Reply]

  • gradof6b

    This is super cool, A total must build. I’m a total noob so I hope it’s not too annoying to ask if you have a wiring diagram?

    [Reply]

  • Rob

    Wow, nice work. Very cool. I’ve actually been toying with this EXACT SAME IDEA for some time now. You beat me to it. 😉

    http://www.electrolund.com/2010/07/traffic-light-parking-assistant/

    Having young kids hasn’t helped my project life.

    [Reply]

  • kmccb

    I would love one of these! Any way you can share your wiring diagram and parts list? I’m sure many would appreciate and benefit from it!!

    Thanks

    [Reply]

  • I actually have been intending to get back into the project with an update – current version has the Ping sensors constantly running which causes them to burn up over the course of time. Update would be to put a door sensor on the garage door to sense when the door is actually open and then turn on the Ping and the stop light accordingly…shutting them off once the door is closed…

    [Reply]