Hackster is hosting Hackster Holidays, Finale: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Tuesday!Stream Hackster Holidays, Finale on Tuesday!
Rich Noordam
Published © GPL3+

Garage Parking Stop Light

Pull into your garage and have a distance sensor tell you when to stop and give feedback through a regular stop light interface.

BeginnerShowcase (no instructions)8 hours6,968
Garage Parking Stop Light

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Novelty Traffic Light
×1
9V 1A Switching Wall Power Supply
9V 1A Switching Wall Power Supply
×1
power supply module
×1
28 Pin DIP IC Sockets Adaptor Solder Type Socket
×1
Capacitor 22 pF
Capacitor 22 pF
×2
16 MHz Crystal
16 MHz Crystal
×1
Various Connector Adapters
×1
Resistor 10k ohm
Resistor 10k ohm
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Hand Drill
5/8'' Drill Bit
Connect Wire

Story

Read more

Schematics

RYG Stoplight Fritz

Code

Arduino code

Arduino
/*
Stop Light Code v. 1.0
By Richard Noordam
Richard.Noordam@gmail.com
Based on several base modules cobbled together.
*/

#define pinLedRed 12
#define pinLedYellow 11
#define pinLedGreen 10
#define TRIGGER 7
#define ECHO    8
#define theDelay 250
int redDistance;
int greenDistance;
int misReadValue;
int retDistance;
int compareDistance;

void setup() {
  //initialize values
  redDistance = 150;
  greenDistance = 350;
  misReadValue = 0;
  // set serial baud rate
  Serial.begin (9600);
  // set pin modes
  pinMode(pinLedRed, OUTPUT);     // Initialize RedLed pin as an output
  pinMode(pinLedYellow, OUTPUT);     // Initialize the Yellow pin as an output
  pinMode(pinLedGreen, OUTPUT);     // Initialize the Green pin as an output
  pinMode(TRIGGER, OUTPUT);
  pinMode(ECHO, INPUT);
  // set start position of LED pins
  digitalWrite(pinLedRed, LOW);     // turn Red off
  digitalWrite(pinLedYellow, LOW);  // turn Yellow off
  digitalWrite(pinLedGreen, LOW);   // turn Green off
  delay(3000);
}

// the loop function runs over and over again forever

// get distance from hc-sr04 ultrasonic distance sensor.
int getDistance(){
 long duration, distance;
  digitalWrite(TRIGGER, LOW);  
  delayMicroseconds(2); 
  digitalWrite(TRIGGER, HIGH);
  delayMicroseconds(10); 
  digitalWrite(TRIGGER, LOW);
  duration = pulseIn(ECHO, HIGH);
  distance = (duration/2) / 29.1;
  return (int)distance;
}
// turn on yellow LED
void yellowOn(){
    //Serial.println("Yellow");
    digitalWrite(pinLedGreen, LOW);    // turn Green OFF
    digitalWrite(pinLedYellow, HIGH);   // turn Yellow ON
    digitalWrite(pinLedRed, LOW);      // turn Red OFF
}
// turn on green LED
void greenOn(){
    //Serial.println("Green");
    digitalWrite(pinLedGreen, HIGH);    // turn Green on
    digitalWrite(pinLedYellow, LOW);   // turn Yellow OFF
    digitalWrite(pinLedRed, LOW);      // turn Red OFF
}
// turn on red LED
void redOn(){
    //Serial.println("Red");
    digitalWrite(pinLedGreen, LOW);    // turn Green OFF
    digitalWrite(pinLedYellow, LOW);   // turn Yellow OFF
    digitalWrite(pinLedRed, HIGH);      // turn Red ON
}

String returnColor(int myDistance){
  String retColor = "ERROR";
  if (myDistance > 0 and myDistance < redDistance){
    retColor = "Red";
  }
  if (myDistance >= redDistance and myDistance < greenDistance){
    retColor = "Yellow";
  }
  if (myDistance >= greenDistance){
    retColor = "Green";
  }
  if (retColor == "ERROR"){
    Serial.print("NoColor: Error, Range Failure:");
    Serial.println(myDistance);
    // so it turns green if too far.
    retColor = "Green";
  }
  return retColor;  
}
String color;

void loop() {
  
  retDistance = getDistance();
  Serial.print("Distance:");
  Serial.print(retDistance);
  color = returnColor(retDistance);
  Serial.print(":   Color:");
  Serial.println(color);
  // turn on green if long distance
  if (color == "Green"){
    greenOn();
  }
  if (color == "Yellow"){
    yellowOn();
  }
  if (color == "Red"){
    redOn();
  }
  delay(theDelay);                    
}

Credits

Rich Noordam
10 projects • 33 followers
Many interests, computing obviously being one of them. MS SQL Server Database Administration, Reporting, Data Science, and more.

Comments