Need added code to set display off when no movement.

K, I suck at coding. I admit, I'm a cut and paster.

Parking sensor code using the Echo thingy and a TM1638 for display. All works well except, I'd like the thing to power off the display when there is no movement. So, I need to "poll" the echo sensor thingy and to turn on the TM1638 display when something is moving, therefore I need to put in a statement somewhere in the following code that shuts off TM1638 until something is moving. Any help would be appreciated. Thanks.

// Parking Sensor sketch.
//
// This sketch is designed to interact with a simple and cheap LED
// display and an ultrasonic sensor, to guage the distance left for
// parking your car.
//
// This definitely falls into the "toy" category, as you could easily
// accomplish the same thing with some string and a ping pong ball.
//
// This would not be possible without the NewPing and TM1638 libraries,
// and the incredible work done by those people.

#include <NewPing.h>
#include <TM1638.h>
#include <EEPROM.h>

// Hardware configuration.
#define TRIGGER_PIN  12
#define ECHO_PIN     11
#define MAX_DISTANCE 200

#define DATA_PIN 8
#define CLOCK_PIN 9
#define STROBE_PIN 7

// The "green" acceptable range zone, in cm.
// Pings +/- this value around the zero point are green.
#define ACCEPTABLE_RANGE 10

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

TM1638 module(DATA_PIN, CLOCK_PIN, STROBE_PIN);

int zero = 0;
char* outOfRange = "--------";
char lastButtons = 0;

void setup() {
  module.setupDisplay(true, 7);
  
  zero = (int) EEPROM.read(0);
  if (zero == 255) {
    zero = 0;
  }
  
  Serial.begin(115200);
  Serial.println("Ready.");
}

void loop() {
  // Use the ping_median() method to get a good sample.
  unsigned int uS = sonar.ping_median(5);

  if (uS == 0) {
    // Out of range. Indicate that on the display.
    module.setDisplayToString(outOfRange);
    module.setLEDs(0x0000);
    Serial.println("Out of range.");
  } else {
    int cm = (int)uS / US_ROUNDTRIP_CM;

    // Debug print raw result.
    Serial.print("Ping: ");
    Serial.print(cm);
    Serial.println("cm");
  
    // If any button is pressed, use that as the zero.
    // Only if the button state has changed, to save us
    // writing to EEPROM all the time.
    char buttons = module.getButtons();
    if (lastButtons != buttons && buttons != 0) {
      zero = cm;
      EEPROM.write(0, (byte)zero);
    }
    lastButtons = buttons;
  
    // Calculate the actual zero point.
    cm = cm - zero;

    // Debug print adjusted result.
    Serial.print("Adjusted: ");
    Serial.print(cm);
    Serial.println("cm");
  
    // And update the display with the number,
    // and make the LEDs green or RED depending on
    // where you are.
    module.setDisplayToSignedDecNumber(cm, 0, false);
    if (cm < -ACCEPTABLE_RANGE) {
      module.setLEDs(0xFF00);
    } else if (cm < ACCEPTABLE_RANGE) {
      module.setLEDs(0x00FF);
    } else {
      module.setLEDs(0xFFFF);
    }
  }
}

mattress67:
I'd like the thing to power off the display when there is no movement.

Do you mean when the sensor is "out of range" (line 55) for a specific time ?

If so, you need to record millis() when the sensor is IN range and if the interval between the last saved value and millis() exceed the interval you can call your switch-off code. Something like this

 } else {
    int cm = (int)uS / US_ROUNDTRIP_CM;
    lastDetectTime = millis();   //   <----------------NEW

and elsewhere in loop()

if (millis() - lastDetectTime >= interval) {
    // switch off ...
}

See several things at a time

...R

Appreciate the response, and yes, the Millis() code would switch it off, but I need something to switch it on when I first come into the garage. In one parking monitor sketch example, the echo monitor continues to poll, and if something changes (distance), then something is moving and the next part of the code is executed - the part that activates the LED's (in my case, it'll be the TM1638).

Anyhow, just writing this get's me to put my thinking cap on. I'll also check out your link. Thanks again.

mattress67:
Anyhow, just writing this get's me to put my thinking cap on. I'll also check out your link. Thanks again.

If you want more help be sure to post your latest code.

...R

It looks as the only way to "shut it off" is to set the ( numeric) display to an invalid or "blank" character.
I could not find any spec which shows the character mapping.

Excellent idea Vaclav, seems so obvious now.