Ultrasonic distance sensor

hi guys i just finished my project i am new to programing so hope you like it 

i have also the code :

#include <SoftwareSerial.h>

 

// TX_PIN is not used by the sensor, since that the it only transmits!

#define PING_RX_PIN 6

#define PING_TX_PIN 7

 

SoftwareSerial mySerial(PING_RX_PIN, PING_TX_PIN);

int counter=0;

int resetCounter=0;

int ledState=1;

int ledDirection=1;

boolean isSend=false;

unsigned long pulseDuration=1;

int RedLedpin=13;

int BlueLedpin=8;

 

long inches = 0, mili = 0;

byte mybuffer[4] = {0};

byte bitpos = 0;

 

void setup() {

  // make led pin ready 

  pinMode(BlueLedpin, OUTPUT);

 //make wake up pin ready 

  pinMode(RedLedpin, INPUT);

 

  Serial.begin(9600);

 

mySerial.begin(9600);

}

 

 

void loop() {

bitpos = 0;

while (mySerial.available()) {

// the first byte is ALWAYS 0xFF and I'm not using the checksum (last byte)

// if your print the mySerial.read() data as HEX until it is not available, you will get several measures for the distance (FF-XX-XX-XX-FF-YY-YY-YY-FF-...). I think that is some kind of internal buffer, so I'm only considering the first 4 bytes in the sequence (which I hope that are the most recent! :D )

if (bitpos < 4) {

mybuffer[bitpos++] = mySerial.read();

} else break;

}

mySerial.flush(); // discard older values in the next read

 

mili = mybuffer[1]<<8 | mybuffer[2]; // 0x-- : 0xb3b2 : 0xb1b0 : 0x--

inches = 0.0393700787 * mili;

Serial.print("PING: ");

Serial.println(inches);

 

 

delay(25);

if (inches>55& isSend == false) {

   analogWrite(BlueLedpin, 255);

} else if (isSend == false) {

  if (ledDirection == 1) {

   analogWrite(BlueLedpin, ledState);

    if (ledState == 125) { ledDirection=-1; } else { ledState = ledState + 1; }

  } else {

    analogWrite(BlueLedpin, ledState);

    if (ledState < 1) { ledDirection=1; } else { ledState = ledState - 1; }

    }

    } else { 

    analogWrite(RedLedpin, 255);

    analogWrite(BlueLedpin, 0);

 

    }

 

    // if someone is in reach 

    if ( inches <55 && inches <=65

    ) counter = counter + 1; 

    analogWrite(RedLedpin, 200);

    if (inches > 200) { resetCounter=0; }

    if

    (resetCounter = resetCounter +1);

    if (resetCounter > 100)

    counter=0;

    resetCounter=0;

    isSend=false;

   analogWrite(RedLedpin, 200);

 

}

 

 

alarm

  • Sensors / input devices: Ultrasonic Distance Sensor

This is a companion discussion topic for the original entry at https://community.robotshop.com/robots/show/ultrasonic-distance-sensor