How to add a timeout?

Could someone please help me work out how to add a timeout to the Garage Parking Assistant code by user Bcjams.
Another user added a timeout to the code but fundamentally changed it at the same time. The project doesn't react the same. I'm looking to keep the exact same functionality as the code below, but hopefully introduce a timeout that turns the leds off after 60 seconds if no further movement is picked up by the sensor (the car is parked). Sometimes we have the garage open for an hour or two, so it would be nice to not have the leds on the entire time the garage door is open.

Any help on the coding front would be most appreciated

/*
* Garage Parking Sensor - Published By Bob Torrence
*/
#include <FastLED.h>
#include <QuickStats.h>
QuickStats stats; //initialize an instance of this class
// defining the pins
#define LED_PIN     7
#define NUM_LEDS    15
const int trigPin = 9;
const int echoPin = 10;
// defining variables
CRGB leds[NUM_LEDS];
float duration;
float durationarray[15];
int distance;
int stopdistance=115; //parking position from sensor (CENTIMETERS)
int startdistance=400; //distance from sensor to begin scan as car pulls in(CENTIMETERS) 
int increment=((startdistance-stopdistance)/15);
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
Serial.begin(9600); // Starts the serial communication
}
void loop() {
  for (int i=0;i<=14;i++){
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
durationarray[i] = pulseIn(echoPin, HIGH);
distance= durationarray[i]*0.034/2;
Serial.print(distance);
Serial.print(" ");
  }
 duration = (stats.median(durationarray,15));
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
  
if (distance<stopdistance){
  for (int i = 0; i <= 14; i++) {
    leds[i] = CRGB ( 255, 0, 0);
    FastLED.show();
     }
}
else
if (distance<stopdistance+increment){
  for (int i = 1; i <= 14; i++) {
    leds[i] = CRGB ( 0, 0, 0);
  }
 for (int i = 0; i <= 0; i++) {
    leds[i] = CRGB ( 255, 255, 0);
  }
  FastLED.show();
  delay(50);
}
else
if (distance<stopdistance+increment*2){
  for (int i = 2; i <= 14; i++) {
    leds[i] = CRGB ( 0, 0, 0);
  }
 for (int i = 0; i <= 1; i++) {
    leds[i] = CRGB ( 255, 255, 0);
  }
  FastLED.show();
  delay(50);
}
else
if (distance<stopdistance+increment*3){
   for (int i = 3; i <= 14; i++) {
    leds[i] = CRGB ( 0, 0, 0);
  }
 for (int i = 0; i <= 2; i++) {
    leds[i] = CRGB ( 255, 255, 0);
  }
  FastLED.show();
  delay(50);
}
else
if (distance<stopdistance+increment*4){
   for (int i = 4; i <= 14; i++) {
    leds[i] = CRGB ( 0, 0, 0);
  }
 for (int i = 0; i <= 3; i++) {
    leds[i] = CRGB ( 0, 255,0);
  }
  FastLED.show();
  delay(50);
}
else
if (distance<stopdistance+increment*5){
   for (int i = 5; i <= 14; i++) {
    leds[i] = CRGB ( 0, 0, 0);
  }
 for (int i = 0; i <= 4; i++) {
  leds[i] = CRGB ( 0, 255,0);
  }
  FastLED.show();
  delay(50);
}
else
if (distance<stopdistance+increment*6){
   for (int i = 6; i <= 14; i++) {
    leds[i] = CRGB ( 0, 0, 0);
  }
 for (int i = 0; i <= 5; i++) {
   leds[i] = CRGB ( 0, 255,0);
  }
  FastLED.show();
  delay(50);
}
else
if (distance<stopdistance+increment*7){
   for (int i = 7; i <= 14; i++) {
    leds[i] = CRGB ( 0, 0, 0);
  }
 for (int i = 0; i <= 6; i++) {
   leds[i] = CRGB ( 0, 255,0);
  }
  FastLED.show();
  delay(50);
}
else
if (distance<stopdistance+increment*8){
   for (int i = 8; i <= 14; i++) {
    leds[i] = CRGB ( 0, 0, 0);
  }
 for (int i = 0; i <= 7; i++) {
    leds[i] = CRGB ( 0, 255,0);
  }
  FastLED.show();
  delay(50);
}
else
if (distance<stopdistance+increment*9){
   for (int i = 9; i <= 14; i++) {
    leds[i] = CRGB ( 0, 0, 0);
  }
 for (int i = 0; i <= 8; i++) {
   leds[i] = CRGB ( 0, 255,0);
  }
  FastLED.show();
  delay(50);
}
else
if (distance<stopdistance+increment*10){
   for (int i = 10; i <= 14; i++) {
    leds[i] = CRGB ( 0, 0, 0);
  }
 for (int i = 0; i <= 9; i++) {
    leds[i] = CRGB ( 0, 255,0);
  }
  FastLED.show();
  delay(50);
}
else
if (distance<stopdistance+increment*11){
   for (int i = 11; i <= 14; i++) {
    leds[i] = CRGB ( 0, 0, 0);
  }
 for (int i = 0; i <= 10; i++) {
    leds[i] = CRGB ( 0, 255,0);
  }
  FastLED.show();
  delay(50);
}
else
if (distance<stopdistance+increment*12){
   for (int i = 12; i <= 14; i++) {
    leds[i] = CRGB ( 0, 0, 0);
  }
 for (int i = 0; i <= 11; i++) {
    leds[i] = CRGB ( 0, 255,0);
  }
  FastLED.show();
  delay(50);
}
else

if (distance<stopdistance+increment*13){
   for (int i = 13; i <= 14; i++) {
    leds[i] = CRGB ( 0, 0, 0);
  }
 for (int i = 0; i <= 12; i++) {
   leds[i] = CRGB ( 0, 255,0);
  }
  FastLED.show();
  delay(50);
}
else
if (distance<stopdistance+increment*14){
   for (int i = 14; i <= 14; i++) {
    leds[i] = CRGB ( 0, 0, 0);
  }
 for (int i = 0; i <= 13; i++) {
   leds[i] = CRGB ( 0, 255,0);
  }
  FastLED.show();
  delay(50);
}
else
if (distance>=stopdistance+increment*14){
 for (int i = 0; i <= 14; i++) {
   leds[i] = CRGB ( 0, 255,0);
  }
  FastLED.show();
  delay(50);
}
}

This may be of help:

First of all, you need to rewrite these 14 identical pieces normally

Instead of checking each of the 15 distances with a separate condition, you can just calculate the number of diodes to lit and use this number in one single code:

int diodes_to_lit =0;
if ( distance >stopdistance)  diodes_to_lit = (distance  - stopdistance)/increment; 
if (diodes_to_lit > 14)   diodes_to_lit = 14;

for (int i = diodes_to_lit +1 ; i <= 14; i++) {
    leds[i] = CRGB ( 0, 0, 0);
  }
 for (int i = 0; i <=diodes_to_lit ; i++) {
   leds[i] = CRGB ( 0, 255,0);
  }
  FastLED.show();
  delay(50);
 

And when you fix the code - explain what kind of delay you need. How many diodes should lit before the minute expires. and how long after the timeout is over?

here's a basic timer, using a timestamp is both a timestamp and flag indicating that the timer is active

a button press is used to enable the timer. presumably your code would enable/set it whenever distance is > stopDistance

const byte PinLed = 13;
const byte PinBut = A1;
byte       butState;

enum { Off = HIGH, On = LOW };

unsigned long Period  = 3000;
unsigned long msecLst;

// -----------------------------------------------------------------------------
void
loop (void)
{
    unsigned long msec = millis ();

    if (0 != msecLst && msec - msecLst > Period)  {
        msecLst = 0;
        digitalWrite (PinLed, Off);
    }

    byte but = digitalRead (PinBut);
    if (butState != but)  {
        butState = but;

        msecLst = 0 < msec ? msec : 1;
        digitalWrite (PinLed, On);
    }
}

void
setup (void)
{
    Serial.begin (9600);

    pinMode (PinLed, OUTPUT);
    digitalWrite (PinLed, Off);

    pinMode (PinBut, INPUT_PULLUP);
    butState = digitalRead (PinBut);
}

I did not write the original code, I'm not that good at coding. I don't want to totally re-write what the original author did, as it works. I just want to introduce a line or two that would power off the leds if the car had been parked for a minute, rather than the leds just staying on the entire time the garage door is open. The idea above seems right, but could anyone show me it implemented into the original code? Like I said I'm a coding novice at absolute best. :slightly_smiling_face:

You assume that it is possible in only a few lines. It is, in a few lines in a program that is written in a professional style that facilitates easy additions. But when that is not the case...

Also that aspect of the program heavily impacts the willingness of volunteers to accept the entire burden, vs. just error checking or performing minor corrections on something that you did yourself.

You refuse to undertake a revision to the code that would make it much easier for volunteers to work on. People notice.

Some people are content to remain at a certain level of programming skill and remain there. It isn't wrong, I suffer a bit from that. But when you do, you have to accept the limitations.

Try to understand what the program does, not how it does it (which is flawed). Then take some time to re-write it yourself. It will actually be easier in the long run.

1 Like

Please forgive my assumption. I should explain more. Firstly I would like to say that the word burden triggers a form of horror in me. After my Father's death my Mom met someone new. After about a year I would constantly hear the term 'you are such a burden on this household'. After 3yrs my Mom caught my new step father taking his anger out once too often. She did the bravest thing I've ever witnessed (she is my hero) and moved us both out of the house. We have been free from him for over a year. This year I wanted to help Mom out by making this garage parking assistant project as she always moans about parking. At 11 and a half I've not done anything like this before in my life. It took a month for the parts to slowly come from China and a further 2.5 months for me to put it together. I have never done so much soldering. My Mom was so proud when I got it all set up and installed. The only thing she asked was whether I could have it turn off after a minute when the garage door was open and we were doing yard work outside. My coding is very very basic, I'm working on very novice programs at present, and even then I struggle. My goal was certainly not to burden anyone, and you are right I thought it might be a simple change. I think I have overshared on this post now. I just hope that I can satisfy my Mom's request at some point. I think I have a few years before I can code or even read code in the way you guys can. Sorry to have been a pain.

Those are unfortunate events and circumstances. I'm sorry that it is a trigger word for you. I actually was active in the electronics hobby at age 11. So I have some idea what it's like. I remember the frustrations and occasional condescension. I found sometimes, my ideas just wouldn't work. Nobody around me knew enough to help, and there was no internet to go to back then. So a lot of projects ended in failure. That is because electronics is, well, extremely complex and difficult. A lot of what I did was copying projects, which is considered a normal way to learn. When it comes to modifications, I had very low expectations which worked well for me. It was really only much later, with more theory and experience under my belt, that I was able to change and invent things independently.

The Arduino ecosystem makes it very easy to produce very complex and interesting things, with a minimum of experience. But it does so at a cost. That is, the facilities that make it so easy, also hide a lot of underlying detail. Those details often come into play when a project needs changes. You didn't write code from scratch. If you do learn to do that, by following a graduated path from easy to difficult and absorbing technique along the way, you will find a modification like the one you want, relatively easy to complete. It is actually because you took the short cut of using someone else's code without a complete understanding of it, that you are in the position you are in now.

I help a lot of people and I don't consider it a burden, not in the least, it's fun and rewarding. But I only get that feeling when I have conveyed skill in some way. If I just edit your code to do what you ask, I would not be doing that. You would not be in any better position to attack your next project or solve your next problem. Then, you would never reach a place where you want to be, knowledge wise.

I think several years is a pessimistic prediction for how long it would take to learn to code something like this. But you need to advance step by step, fully understanding what you are presently doing before moving to the next level. Just like math. You can't leap from Arithmetic to Calculus. If you work on some simpler projects and do a lot of research to try to understand anything you don't grasp, I think you could write this code after less than one year of study. I am talking part time, too.

I think that would be more important to your Mom than whether the garage door works in a certain way.

Have a look at the code in post #4. I'm sure it would be easier to work with than the undocumented and questionable code you presented. Post #3 identifies issues that are almost as much work to clean up, as it would be to start from scratch, unless you really understand what the programmer is doing. As I pointed out, they didn't document it which makes it practically useless to learn from.

The LEDs won't consume much mains power. Forget about it. Let them stay on. Your Mom is probably more proud than she is even allowing you to see. Just move on and make something new. But this time, make it from scratch. Then you will have complete control over its evolution.

1 Like

Thank you for your kind words. I really do want to keep improving. I have faith in myself and am determined to prove my step father wrong!
I will take a shot at altering the code soon with the hopes I succeed.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.