Skip to content

ALTO – Arduino Parking Assistant

This content will be shown before all post

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;
}
This content will be shown after all post