Nicky ValdiviaSpencer HustonDaniel MartinezChris Mullins
Published

Backup Sensor with Display

Safety first! Upgrade any vehicle with this state-of-the-art safety technology that is designed to help you be a better driver.

IntermediateFull instructions provided2 hours801
Backup Sensor with Display

Things used in this project

Story

Read more

Schematics

Ultrasonic Backup Sensor Diagram

Backup Sensor with Display schematic

Code

Ultrasonic backup Sensor

Arduino
/*
 * UltrasonicDisplayOnTerm.ino
 * Example sketch for ultrasonic ranger
 *
 * Copyright (c) 2012 seeed technology inc.
 * Website    : www.seeed.cc
 * Author     : LG, FrankieChu
 * Create Time: Jan 17,2013
 * Change Log :
 *
 * The MIT License (MIT)
 *
 * 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.
 */


/***************************************************************************/
//	Function: Measure the distance to obstacles in front and print the distance
//			  value to the serial terminal.The measured distance is from
//			  the range 0 to 400cm(157 inches).
//	Hardware: Grove - Ultrasonic Ranger
//	Arduino IDE: Arduino-1.0
/*****************************************************************************/
#include "TM1637.h"
#include "Ultrasonic.h"
#define CLK 40
#define DIO 39
#define BLINK_LED RED_LED
#define ULTRASONIC_PIN 27
#define BUZZER_PIN 36

Ultrasonic ultrasonic(ULTRASONIC_PIN);
TM1637 tm1637(CLK, DIO);
int8_t bits[4] = {0};
//Previous mesurments of the ultrasonic sensor used to calcualte running average.
long M1;
long M2;
long M3;
long M4;
long M5;
long M6;
long M7;
long M8;
long M9;
long M10;

int length = 1;         /* the number of notes */
char notes[] = "ccggaagffeeddc "; //notes in the song
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 }; //length of each note
int tempo = 200;

void setup()
{
  long RangeInCentimeters; //Varible to get range of ultrasonic sensor
  Serial.begin(9600);
  tm1637.init();                       /* initialize 4-Digit Display */
  tm1637.set(BRIGHT_TYPICAL);          /* set the brightness */    
  pinMode(BLINK_LED, OUTPUT);            /* declare the LED pin as an OUTPUT */
  pinMode(BUZZER_PIN, OUTPUT); //Turn the buzzer on
  Serial.println("test"); //Serial Test
  RangeInCentimeters = ultrasonic.MeasureInCentimeters(); //Messure the distance from the ultrasonic sensor
  //Save the distance to the running average so there is no error on start.
  M1=RangeInCentimeters;
  M2=RangeInCentimeters;
  M3=RangeInCentimeters;
  M4=RangeInCentimeters;
  M5=RangeInCentimeters;
  M6=RangeInCentimeters;
  M7=RangeInCentimeters;
  M8=RangeInCentimeters;
  M9=RangeInCentimeters;
  M10=RangeInCentimeters;
}
void loop()
{
	long RangeInCentimeters;
  long Distance;
  long Distance2;
  RangeInCentimeters = ultrasonic.MeasureInCentimeters();//Read new ultrasonic distance
  //shift saved vales up one in memory.
  M10=M9;
  M9=M8;
  M8=M7;
  M7=M6;
  M6=M5;
  M5=M4;
  M4=M3;
  M3=M2;
  M2=M1;
  M1=RangeInCentimeters;
  Distance = (M1+M2+M3+M4+M5+M6+M7+M8+M9+M10)/10; //Calculate long running average
  Distance2 = (M1+M2+M3+M4+M5)/5;//Calculate short running average
	//Serial.println("The distance to obstacles in front is: "); //serial test code
	//Serial.print(Distance);//0~400cm
	//Serial.println(" cm");
  memset(bits, 0, 4);//start up display writing
  if(Distance2<90 || RangeInCentimeters<50){ //play buzzer if short running average less than 90 or range is less than 50
    playTone(1000, 10);
  }
  else{
    delay(10);
  }
  for(int i = 3; i >= 0; i--) //send distance to display.
  {
    bits[i] = Distance % 10;
    Distance = Distance / 10;  
    tm1637.display(i, bits[i]);
  }
}

void playTone(int tone, int duration) 
{
  for (long i = 0; i < duration * 1000L; i += tone * 2) 
  {
    digitalWrite(BUZZER_PIN, HIGH);
    delayMicroseconds(tone);
    digitalWrite(BUZZER_PIN, LOW);
    delayMicroseconds(tone);
  }
}

char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };

void playNote(char note, int duration) 
{
  
  
  // play the tone corresponding to the note name
  for (int i = 0; i < 8; i++) 
  {
    if (names[i] == note) 
    {
      playTone(tones[i], duration);
    }
  }
}

Credits

Nicky Valdivia

Nicky Valdivia

1 project • 0 followers
Spencer Huston

Spencer Huston

1 project • 0 followers
Daniel Martinez

Daniel Martinez

1 project • 0 followers
Chris Mullins

Chris Mullins

1 project • 0 followers

Comments