Hesam Moshiri
Published © CC BY-NC-SA

An Ultrasonic Range Finder Using Arduino

An Ultrasonic Range Finder Using an SRF05 and an ATTiny85

IntermediateFull instructions provided1 hour4,336
An Ultrasonic Range Finder Using Arduino

Things used in this project

Hardware components

TS2937
×1
ATTiny85
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Sch

Code

Code

Arduino
#include <Tiny4kOLED.h>
#include <NewPing.h>


unsigned long uS = 0;


#define TRIGGER_PIN  3   
#define ECHO_PIN     4   
#define MAX_DISTANCE 41  


NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
unsigned int CM = 0, IN = 0;
unsigned char cnt = 0;


void setup() {
  oled.begin(128, 64, sizeof(tiny4koled_init_128x64br), tiny4koled_init_128x64br);
  oled.on();
  oled.setCursor(0, 1);
  oled.setFont(FONT8X16);
  oled.clear();
  oled.print("ULS Range Finder");
}


void loop() {
  uS = sonar.ping();
  CM = sonar.convert_cm(uS) + CM;
  IN = sonar.convert_in(uS) + IN;
  cnt ++;
  if (cnt == 10)
  {
    oled.setCursor(0, 2);
    oled.print(CM / 10);
    if (CM / 10 < 10)
    {
      oled.setCursor(9, 2);
      oled.print(" Centimeter ");
    } else
    {
      oled.setCursor(17, 2);
      oled.print(" Centimeter");
    }


    oled.setCursor(0, 4);
    oled.print(IN / 10);
    if (IN / 10 < 10)
    {
      oled.setCursor(9, 4);
      oled.print(" Inches ");
    } else
    {
      oled.setCursor(17, 4);
      oled.print(" Inches");
    }


    switch (CM / 10)
    {


      case 0:
        oled.setCursor(0, 6);
        oled.print(" [out of range] ");
        break;
      case 1 ... 4:
        oled.setCursor(0, 6);
        oled.print("==              ");
        break;
      case 5 ... 9:
        oled.setCursor(0, 6);
        oled.print("====            ");
        break;
      case 10 ... 14:
        oled.setCursor(0, 6);
        oled.print("=====           ");
        break;
      case 15 ... 19:
        oled.setCursor(0, 6);
        oled.print("=======         ");
        break;
      case 20 ... 24:
        oled.setCursor(0, 6);
        oled.print("=========       ");
        break;
      case 25 ... 29:
        oled.setCursor(0, 6);
        oled.print("===========     ");
        break;
      case 30 ... 34:
        oled.setCursor(0, 6);
        oled.print("=============   ");
        break;
      case 35 ... 39:
        oled.setCursor(0, 6);
        oled.print("=============== ");
        break;
      case 40 ... 41:
        oled.setCursor(0, 6);
        oled.print("================");
        break;
    }


    CM = 0;
    IN = 0;
    cnt = 0;
  }
  delay(10);
}

Credits

Hesam Moshiri

Hesam Moshiri

51 projects • 32 followers
https://www.youtube.com/c/MyVanitar/videos

Comments