Code for Car parking LEDs

So I am pretty new to Arduino but loving it. Anyway my goal is to mount a small project box in the garage that will tell me where to stop the car for parking. (the 1" and 3" numbers in my file are placeholders for now

I have the basics working using an uno, US 015 sensor and 2 5mm LEDs

#include <NewPing.h>
 
#define TRIGGER_PIN  12
#define ECHO_PIN     11
#define MAX_DISTANCE 500
#define RED_LED      6
#define GREEN_LED    5
 
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
//int DISTANCE1 = sonar.ping_in();                  //  built into library to return inches
//int DISTANCE2 = sonar.ping_median(10)/148;        // Take median of 10 measurements then divide by 148 to convert to inches

void setup() {
  Serial.begin(115200);
  pinMode(RED_LED, OUTPUT);                         //Set pin 6 as output (Red LED)
  pinMode(GREEN_LED, OUTPUT);                       //Set pin 5 as output (Green LED)
    
}
 
void loop() {
  delay(50);
  int DISTANCE2 = sonar.ping_median(5)/148;         // Take median of 5 measurements then divide by 148 to convert to inches
  Serial.print("Ping: ");
  Serial.print(DISTANCE2);
  Serial.println("inches");
  
  if (DISTANCE2 < 3 && DISTANCE2 > 1) {             //  If in between 1 and 3 inchs turn on red led 
    
  digitalWrite(RED_LED, HIGH);
  digitalWrite(GREEN_LED, LOW);
}

  if (DISTANCE2 >= 3) {                              //  If >= 3 inchs turn on green led

  //sonar.ping_median(10)  
  digitalWrite(RED_LED, LOW);
  digitalWrite(GREEN_LED, HIGH);
}

  else if (DISTANCE2 < 1 || DISTANCE2 > 72){         //  If < 1 or > 72 all lights off
    
  digitalWrite(RED_LED, LOW);
  digitalWrite(GREEN_LED, LOW);
}
}

Now the more advanced part. I'd like to get onto an attiny 85 and for it to only fire up once every 10 seconds to check if a car has come in range and if not in the range (<72 inches and >1 inch) then go back into ultra low power mode. The goal being to hopefully get on an Attiny 85 and battery power (most likely 3 AA's). obviously no need for high speed at all so thinking its possible.

So need it to shut off after 1min no matter what (so once parked it goes ultra low power) and not come back on until 72 inch threshold passed again so doesnt stay on when parked. Ultimately would be good to do if distance shrinking only. Meaning can stay off if backing up.

but really want to combine what I have with some suggestions like this:

https://diyodemag.com/projects/how_low_can_you_go
https://www.gammon.com.au/power

Seems I should use watchdog timers, slow clock way down, and use sleep modes but I'm quicly getting over my skis as far as knowledge. What should be my next steps?

Hi,
Have you googled

attiny85 ultrasonic sensor

There are quite a few projects using Attiny85.

Tom... :slight_smile:

Thanks Tom's and getting it running on the attiny is not my issue at all. That I have done.

I need to make it super low power so batteries will last and I have seen dozens of projects and all or either mum on the subject acting like batteries will last forever (which with an LED and the sensor on will eat up quick) or are plugged in. Hence my question.

I get the theories linked above but need help with the code.

Maybe Nick Gammon's power saving tutorial will help.

...R

Why not use a car USB charger that charges a powerbank as supply? Plug the charger in an outlet that is switched on only when ignition is on.
Saving pwr is of cource good anyway.

Well I was planning on putting it on the wall rather than the car but that's a thought I hadn't considered which if I can't figure out the first way is worth exporing.

kwledbetter:
I'd like to get onto an attiny 85 and for it to only fire up once every 10 seconds to check if a car has come in range and if not in the range (<72 inches and >1 inch) then go back into ultra low power mode...

So need it to shut off after 1min no matter what (so once parked it goes ultra low power) and not come back on until 72 inch threshold passed again so doesnt stay on when parked.

You want to fire it up every 10 seconds and you want to put it in ultra low power mode inbetween? Does that make sense? A machine working every 10 seconds is not sleeping, is it?

You also want it to stay in ultra low power mode when it is parked and then come back when 72 inch threshold is passed. What detects the pass? The machine sleeping?

Johan_Ha:
You want to fire it up every 10 seconds and you want to put it in ultra low power mode inbetween? Does that make sense? A machine working every 10 seconds is not sleeping, is it?

Effectively, it is. Remember that you can wake up and go back to sleep in a few hundred microseconds. The ratio of on to off time times the supply current, is the average current. That can be extremely low in such cases, for this reason.

In this case, the processor has to stay on just long enough to perform a ping.

So to be clear:

Objective 1: working code illuinating correct lights when in full on powered up mode - completed
Obejective 2: Do some smart coding around after ~1 active minute go into sleep mode for lowest power consumption:
Objective 3: wake up every 5-10 seconds (this is an eternity in computer land) so yes it's real sleep and do an ultrasonic ping and if something in range <72 inches then run active program, if not go back to sleep
Objective 4: Given something parked will be in range, objective 3 needs the modification that is in range and getting closer so it doesn't trigger when backing up and it doesn't trigger while parked
Objective 5: optimize low power by lowering clock, etc and other ways so 3 AA batteries will power this for months

Hope that helps. I get the concepts in Nick Gammons link above just no experience on how to do or where to start. I don't think what I'm asking should be all that difficult honestly I just am to green to know where t start with the code.