#----------------------------------------------------------
# ULTRASONIC DISTANCE MESUREMENT
# ==============================
#
# In this project a HC-SR04 type ultrasonic sensor module is
# connected to the Raspberry Pi Pico. The program displays
# distance to an object in-front of the sensor
#
# 
# File : Ultrasonic.py
# Date : February 2021
#------------------------------------------------------------
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
import framebuf
import utime
trig1 = Pin(0, Pin.OUT) # trig pin
echo1 = Pin(1, Pin.IN) # echo pin
trig2 = Pin(14, Pin.OUT) # trig pin
echo2 = Pin(15, Pin.IN) # echo pin
trig3 = Pin(26, Pin.OUT) # trig pin
echo3 = Pin(27, Pin.IN) # echo pin
trig4 = Pin(16, Pin.OUT) # trig pin
echo4 = Pin(17, Pin.IN) # echo pin
trig5 = Pin(03, Pin.OUT) # trig pin
echo5 = Pin(04, Pin.IN)
LED1 = Pin(02, Pin.OUT)
LED2 = Pin(28, Pin.OUT)
IR_sensor = Pin(6, Pin.IN, Pin.PULL_UP)
buzzer = Pin(7, Pin.OUT)
i2c = I2C (0, scl = Pin(21),   sda = Pin(20),  freq = 200000)
oled = SSD1306_I2C(128, 64, i2c)  







while True:
  if IR_sensor.value():
    buzzer.value(1)
  else:
    buzzer.value(0)
  #------------------Program for Right front---------------
  limit_low=30
  limit_high=300
  trig1.value(0)
  utime.sleep_us(5) # Wait until settled
  trig1.value(1) # Send trig pulse
  utime.sleep_us(10) # 10 microseconds
  trig1.value(0) # Remove trig pulse
  while echo1.value() == 0: # Wait for echo 1
    pass
  Tmrstrt1 = utime.ticks_us()
  while echo1.value() == 1: # Wait for echo 0
    pass
  Tmrend1 = utime.ticks_us()
  Duration1 = utime.ticks_diff(Tmrend1, Tmrstrt1)
  distancecm1 = Duration1 * 0.0171
  print("first",distancecm1)
  if distancecm1>=limit_low and distancecm1<=limit_high:
    print('danger')
    LED1.value(1)
  else:
    print('Safe')
    LED1.value(0)
  utime.sleep(1)
   #------------------Program for Left front---------------
  trig2.value(0)
  utime.sleep_us(5) # Wait until settled
  trig2.value(1)
  utime.sleep_us(10) # 10 microseconds
  trig2.value(0)
  while echo2.value() == 0: # Wait for echo 2
    pass
  Tmrstrt2 = utime.ticks_us()
  while echo2.value() == 1: # Wait for echo 0
    pass
  Tmrend2 = utime.ticks_us()
  Duration2 = utime.ticks_diff(Tmrend2, Tmrstrt2)
  distancecm2 = Duration2 * 0.0171
  print("second",distancecm2)
  if distancecm2>=limit_low and distancecm2<=limit_high:
    print('danger')
    LED1.value(1)
  else:
    print('Safe')
    LED1.value(0)
  utime.sleep(0.01)
  #------------------Program for Left back---------------
  trig3.value(0)
  utime.sleep_us(5) # Wait until settled
  trig3.value(1)
  utime.sleep_us(10) # 10 microseconds
  trig3.value(0)
  while echo3.value() == 0: # Wait for echo 2
    pass
  Tmrstrt3 = utime.ticks_us()
  while echo3.value() == 1: # Wait for echo 0
    pass
  Tmrend3 = utime.ticks_us()
  Duration3 = utime.ticks_diff(Tmrend3, Tmrstrt3)
  distancecm3 = Duration3 * 0.0171
  print("Third",distancecm3)
  if distancecm3>=limit_low and distancecm3<=limit_high:
    print('danger')
    LED2.value(1)
  else:
    print('Safe')
    LED2.value(0)
  utime.sleep(0.01)
  #------------------Program for right back---------------
  trig4.value(0)
  utime.sleep_us(5) # Wait until settled
  trig4.value(1)
  utime.sleep_us(10) # 10 microseconds
  trig4.value(0)
  while echo4.value() == 0: # Wait for echo 2
    pass
  Tmrstrt4 = utime.ticks_us()
  while echo4.value() == 1: # Wait for echo 0
    pass
  Tmrend4 = utime.ticks_us()
  Duration4 = utime.ticks_diff(Tmrend4, Tmrstrt4)
  distancecm4 = Duration4 * 0.0171
  print("Fourth",distancecm4)
  utime.sleep(0.01)
  #------------------Program for parking---------------
  trig5.value(0)
  utime.sleep_us(5) # Wait until settled
  trig5.value(1)
  utime.sleep_us(10) # 10 microseconds
  trig5.value(0)
  while echo5.value() == 0: # Wait for echo 2
    pass
  Tmrstrt5 = utime.ticks_us()
  while echo5.value() == 1: # Wait for echo 0
    pass
  Tmrend5 = utime.ticks_us()
  Duration5 = utime.ticks_diff(Tmrend5, Tmrstrt5)
  distancecm5 = Duration5 * 0.0171
  print("parking",distancecm5)
  if distancecm4>=limit_low and distancecm4<=limit_high:
    print('danger')
    LED2.value(1)
  else:
    print('Safe')
    LED2.value(0)
  #----OLED start----
  oled.fill(0)
  oled.text('Hi SVECW(A)', 0,0)
  oled.text('distance(cm):',0,10)
  oled.text(str(distancecm5),105,10)
  oled.show()

  #----OLED end------

  
  utime.sleep(0.01)
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT