Parking Sensor Code Mods

Ello all!

I’m currently working on building a modded version of Parking Assist Stoplight - Arduino Project Hub and need some help.

Two things I’m trying to achieve:

-At the very least having the red parked light go out after 20 seconds if no “real” change of distance is detected (around 150mm should be fine I’m guessing)

-Get the saved distance to be saved into EEPROM (and of course load on boot). Also show “saved” on the OLED, and return to displaying the current state when the switch is pulled low for 5 seconds.

There’s a comment on the video on how to achieve sleeping the red LED but so far I’ve been unsuccessful in implementing it.

Any help would be greatly appreciated!

Thanks in advance <3

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

We need to see your code.

Thanks.. Tom.... :slight_smile:

Just curious, but what is wrong with one "off the shelf"?

OK, I know, it's not the end product that matters, and it is certainly not going to be cheaper; it's the "educational exercise". :grinning:

Whoops! Here we go

/*
 * Parking Assist Stoplight
 * Original Created by Ty Palowski
 * https://youtube.com/c/typalowski
 */

//For Rotary Encoder
int inputCLK = 4; //Encoder Pin CLK (Clock)
int inputDT = 2; //Encoder Pin DT (Data)
int inputSW = 8; //Encoder Pin SW (Switch)

int counter = 0; 
int currentStateCLK = LOW;
int previousStateCLK = currentStateCLK; 
int switchState = HIGH;

String encdir ="";

//For Ultrasonic Sensor
#define trigPin 9
#define echoPin 10

#define led1 12
#define led2 11
#define led3 13

int light1;
int light2;
int light3;
int distance1;

int const light1s = 12;
int const light2s = 24;
int const light3s = 48;

//For OLED Display
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup()
{ Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);

attachInterrupt(digitalPinToInterrupt(inputDT), update, CHANGE);

   // Set encoder pins as inputs  
   pinMode (inputCLK, INPUT);
   pinMode (inputDT, INPUT);
   pinMode(inputSW, INPUT_PULLUP);

   // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

  // Clear the buffer
  display.clearDisplay();
  display.setRotation (2);


  // Show the display buffer on the screen. You MUST call display() after
  // drawing commands to make them visible on screen!
  display.display();
  delay(2000);
}

void loop(){
// BUTTON
  switchState = digitalRead(inputSW); // Read the digital value of the switch (LOW/HIGH)
  // If the switch is pressed (LOW), print message
if (switchState == LOW) {
   Serial.println("Switch pressed");
   counter = distance1 - light1s;
  }

 long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2); //2
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 73.914; //29.1(cm)
  light1 = counter + light1s;
  light2 = light1 + light2s;
  light3 = light2 + light3s;
  distance1 = distance;


if (distance < light1)
 { //RED LIGHT
  digitalWrite(led1,HIGH);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
 }
else if (distance > light1 && distance < light2)
 { //YELLOW LIGHT
  digitalWrite(led2,HIGH);
  digitalWrite(led1,LOW);
  digitalWrite(led3,LOW);
 }
else if (distance > light2 && distance < light3)
 { //GREEN LIGHT
  digitalWrite(led3,HIGH);
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
 }
else
 {
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
 }
  displayStopDist();
  display.display();
}

void update() {
    // Read the current state of inputCLK
   currentStateCLK = digitalRead(inputCLK);
   
   if ((previousStateCLK == LOW) && (currentStateCLK == HIGH)) {  
   if (digitalRead(inputDT) == HIGH) { 
       counter --;
        }
   else {
       counter ++;
        }
      } 
   previousStateCLK = currentStateCLK; 
}

void displayStopDist(){

  // Define d as Stop Distance
  float d = light1;
  float c = distance1;
  // Clear the display
  display.clearDisplay();
  //Set the color - always use white despite actual display color
  display.setTextColor(WHITE);
  //Set the font size
  display.setTextSize(1);
  //Set the cursor coordinates
  display.setCursor(0,0);
  display.print("Parking Stop Assist");
  display.setCursor(0,11); 
  display.print("Stop Dist: "); 
  display.setTextSize(2);
  display.print(d,0);
  display.print("in");
  display.setTextSize(1);
  display.setCursor(0,25);
  //display.print("Temperature: "); 
  display.print(c,0);
  display.print("in");
}

Paul__B:
Just curious, but what is wrong with one “off the shelf”?

I had one and it got fried so I decided to build a new one with cannibalized parts from it :slight_smile:

Hi,
Have you been able to get the UN-modified version working?

Tom... :slight_smile:

Yes. Only modifications I've done in THAT code so far is just removing the boot screen :stuck_out_tongue_winking_eye:

read up on, blink without delay.

here are some ideas, this is not actual code, so will not compile and I did not define any of the variables. This is a guide so you can write your own code.

if (millis() - then >= 100 ){ // checks 10 times a second
distancetime = distancetime + distance // stores distances
counter++
then = millis() // resets the timer

this checks distance 10 times a second and adds distance and increments counter

if (counter => 100 ) { // 10 seconds
distancetimecheck = distancetime/counter
if (old_distancetimecheck == distancetimecheck)
// put in your -turn off the lights- bit
old_distancetimecheck = distancetimecheck // saves old value every 10 seconds
counter = 0 // resets counter
} // ==== end if counter =========

there is at least one major flaw :
the sensor reading would have to match exactly for this to work.
what you might want to do is to check if it is within a range

if (old_distancetimecheck == distancetimecheck+100) && (old_distancetimecheck == distancetimecheck-100)

depending on the values, it could be 100 pulses or 0.1 inch or whatever you find works to have a range.

if you have a display, then I would add the values to the display
print ("ODC") print(old_distancetimecheck)
print (" : DC") print(distancetimecheck)
print(" : diff =) (old_distancetimecheck-distancetimecheck)

This would allow you to see how much variation you get when the vehicle is parked.

more comments :
I looked at a few of the other posts about these on the Arduino projects area. seems very similar to instructables....

None seemed to have an auto-shut off.
Also, no comment about sensing the garage door. Turn of when the door opens, off when closed.

also, an upgrade, add three other ranges "green/yellow" and "yellow/red" and "red blinking"
yellow/red could be 3-2 feet, solid red - 2-1 feet, blinking = less than 1 foot