Skip to content

Instantly share code, notes, and snippets.

@jeffgreenca
Created September 3, 2017 23:19
Show Gist options
  • Save jeffgreenca/134ebe0da1af2c16989f35dfa0a82709 to your computer and use it in GitHub Desktop.
Save jeffgreenca/134ebe0da1af2c16989f35dfa0a82709 to your computer and use it in GitHub Desktop.
Arduino Garage Parking Assistant
/*
Arduino Garage Parking Assistant
By Jeff Green
Sept 3rd, 2017
Simple parking assist using HC-SR04 for distance measurement
and MAX7219 8x8 LED grid for visual feedback.
Place at the front of your garage, and it will help you park
within about 90cm of the sensor.
This has certainly been done before - the goal here is to
provide visually satisfying feedback during the approach and
park process.
Displays three LED animations:
0. Blank Display - no object (car) detected within 200cm
1. Waterfall - object is approaching within 200cm
2. Moving Sqares - object is almost at desired location
3. Bull's Eye - object is within GOOD_ENOUGH_CM of target
4. Flashing - object is closer than desired target
The MIT License (MIT)
Copyright (c) 2017 Jeff Green
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <NewPing.h>
#include <LedControl.h>
//Sonar Rangefinder Setup
#define TRIGGER_PIN 12
#define ECHO_PIN 13
#define MAX_CM_DISTANCE 400
#define MIN_CM_DISTANCE 2
//LED Matrix Setup
#define CLK 8
#define CS 9
#define DIN 10
//Distance settings for visual feedback
#define GOOD_ENOUGH_CM 7
#define CLOSE_CM 30
#define MAX_DETECTION_CM 200
//Globals
volatile int currentDistanceCm;
int targetDistanceCm = 90; //TODO: Make operator programmable
int currentAnimation = 0;
int aiteration = 0; //Index for long animations
//Setup hardware
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_CM_DISTANCE);
LedControl lc = LedControl(DIN, CLK, CS, 0);
void setup() {
Serial.begin(115200); //Debug
initLCD();
}
void loop () {
playAnimation();
updateDistance();
if(currentDistanceCm < targetDistanceCm) {
currentAnimation = 2; //Too close!!
}
else if (currentDistanceCm < targetDistanceCm + GOOD_ENOUGH_CM) {
currentAnimation = 4; //Good enough
}
else if (currentDistanceCm < targetDistanceCm + CLOSE_CM) {
//Withint 15cm of target
currentAnimation = 3; //Getting close...
}
else if (currentDistanceCm < targetDistanceCm + MAX_DETECTION_CM) {
currentAnimation = 1; //I can see you, visual feedback
}
else {
currentAnimation = 0; //Too far away, turn off display
delay(500); //Longer delay than usual before next reading
}
}
void initLCD() {
lc.shutdown(0, false);
lc.clearDisplay(0);
lc.setIntensity(0, 2);
}
void updateDistance() {
int reading = sonar.convert_cm( sonar.ping_median(10) );
if( ! ( reading > MAX_CM_DISTANCE || reading < MIN_CM_DISTANCE ) ) {
currentDistanceCm = reading;
Serial.print("New Reading: ");
Serial.println(currentDistanceCm);
} else {
Serial.println("No update");
}
}
//Play the current animation, should take 25ms x 8 steps to complete, or about 200ms
//Longer-running animations can span by using the aiteration variable, which is looped 0-7
void playAnimation() {
aiteration++;
if (aiteration == 8) {
aiteration = 0;
}
switch(currentAnimation) {
case 1: {
lc.clearDisplay(0);
//Approaching!
byte pattern[8] = { 1, 2, 4, 8, 16, 32, 64, 128 };
for(int i = 0; i < 8; i++) {
int lastRow = aiteration == 0 ? 7 : aiteration - 1 ;
lc.setRow(0, lastRow, 0);
lc.setRow(0, aiteration, pattern[i]);
delay(45);
}
lc.clearDisplay(0);
}
break;
case 2: {
lc.clearDisplay(0);
//Flashing All Warning!
for(int num = 0; num < 6; num++) {
for(int i = 0; i < 8; i++) {
byte pattern = num % 2 == 0 ? B11111111 : B00000000;
lc.setRow(0, i, pattern);
}
delay(45);
}
}
break;
case 3: {
//Moving Concentric rings
byte pattern[4][8] {
{ B11111111,B10000001,B10000001,B10000001,B10000001,B10000001,B10000001,B11111111},
{ B00000000,B01111110,B01000010,B01000010,B01000010,B01000010,B01111110,B00000000},
{ B00000000,B00000000,B00111100,B00100100,B00100100,B00111100,B00000000,B00000000},
{ B00000000,B00000000,B00000000,B00011000,B00011000,B00000000,B00000000,B00000000}
};
for(int j = 0; j < 4; j++) {
for(int row = 0; row < 8; row++) {
lc.setRow(0, row, pattern[j][row]);
}
delay(45);
}
lc.clearDisplay(0);
}
case 4: {
//Bullseye, stable
lc.clearDisplay(0);
byte pattern[8] = { B11111111,B10000001,B10000001,B10011001,B10011001,B10000001,B10000001,B11111111};
for(int row = 0; row < 8; row++) {
lc.setRow(0, row, pattern[row]);
}
delay(200);
}
break;
default:
lc.clearDisplay(0);
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment