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);
}
}
}