pwm_M1A.freq(500) pwm_M1B.freq(500) pwm_M2A.freq(500) pwm_M2B.freq(500) # Setting PWM value to 0 na on all steering pins - 0V pwm_M1A.duty_u16(0) pwm_M1B.duty_u16(0) pwm_M2A.duty_u16(0) pwm_M2B.duty_u16(0) # Creating sensor object (to use the HC-SR04 sensor) cinnected on specified pins sensor = hcsr04.HCSR04(trigger_pin=15, echo_pin=14, echo_timeout_us=1000000) # Defining 0/1 variable - controls if robot is in on or off state on = 0 # Defininig maximum pwm value constant, default is max_V: 26700 (1,5 V on motor), but why not to experiment? max_V = 35000 # Defining front obstacle safe distance value /we assume using cm/ safe_distance = 30.00000 # Defining movement functions
def main(): sensor1 = hcsr04.HCSR04(4, 19) sensor2 = hcsr04.HCSR04(16, 26) nokia = screen.Screen() f = 255 * np.ones(12) nokia.print_bars(f) while (True): dists = [] dist1 = sensor1.distance_cm() now = datetime.now() clock = str(now.hour) + ":" + str(now.minute) + ":" + str(now.second) position = (20, 0) nokia.write_text(clock, position) if dist1 != -1: dists.append(dist1) if 150 < dist1: f[0] = 255 f[3] = 255 f[6] = 255 f[9] = 255 nokia.print_bars(f) if 75 < dist1 <= 150: f[0] = 0 f[3] = 255 f[6] = 255 f[9] = 255 nokia.print_bars(f) if 50 < dist1 <= 75: f[0] = 0 f[3] = 0 f[6] = 255 f[9] = 255 nokia.print_bars(f) if 25 < dist1 <= 50: f[0] = 0 f[3] = 0 f[6] = 0 f[9] = 255 nokia.print_bars(f) if 0 < dist1 < 25: f[0] = 0 f[3] = 0 f[6] = 0 f[9] = 0 nokia.print_bars(f) dist2 = sensor2.distance_cm() if 150 < dist2: f[1] = 255 f[4] = 255 f[7] = 255 f[10] = 255 nokia.print_bars(f) if 75 < dist2 <= 150: f[1] = 0 f[4] = 255 f[7] = 255 f[10] = 255 nokia.print_bars(f) if 50 < dist2 <= 75: f[1] = 0 f[4] = 0 f[7] = 255 f[10] = 255 nokia.print_bars(f) if 25 < dist2 <= 50: f[1] = 0 f[4] = 0 f[7] = 0 f[10] = 255 nokia.print_bars(f) if 0 < dist2 < 25: f[1] = 0 f[4] = 0 f[7] = 0 f[10] = 0 nokia.print_bars(f) if dist2 != -1: dists.append(dist2) distance = str(min(dists)) + " cm" position = (20, 10) nokia.write_text(distance, position)
def __init__(self): p_pwr1.value(1) self.sensor = hcsr04.HCSR04(trigger_pin=p_hcsr_trig, echo_pin=p_hcsr_echo, c=hcsr_c)
from machine import Pin import hcsr04 hcsr = hcsr04.HCSR04(18, 19) def get_distance(): return hcsr.distance_cm() print(get_distance())
def main(): sensor1 = hcsr04.HCSR04(4, 19) sensor2 = hcsr04.HCSR04(16, 26) nokia = screen.Screen() f = 255 * np.ones(12) nokia.print_bars(f) while (True): dists = [] dist1 = sensor1.distance_cm() now = datetime.now() clock = str(now.hour) + ":" + str(now.minute) + ":" + str(now.second) position = (20, 0) nokia.write_text(clock, position) if dist1 != -1: dists.append(dist1) if 150 < dist1: p = buzzer.init_buzzer(12, 100) print('ta aqui 1') bThread = th.Thread(target=buzzer.buzz, args=( 100, 5, 90 )) # Cria a thread e depois coloca-a numa lista de threads ativas bThread.start() # inicia f[0] = 255 f[3] = 255 f[6] = 255 f[9] = 255 nokia.print_bars(f) if 75 < dist1 <= 150: p = init_buzzer(12, 100) print('ta aqui 2') bThread = th.Thread(target=buzz, args=( 100, 5, 90 )) # Cria a thread e depois coloca-a numa lista de threads ativas bThread.start() # inicia f[0] = 0 f[3] = 255 f[6] = 255 f[9] = 255 nokia.print_bars(f) if 50 < dist1 <= 75: f[0] = 0 f[3] = 0 f[6] = 255 f[9] = 255 nokia.print_bars(f) if 25 < dist1 <= 50: f[0] = 0 f[3] = 0 f[6] = 0 f[9] = 255 nokia.print_bars(f) if 0 < dist1 < 25: p = init_buzzer(12, 100) print('ta aqui 3') bThread = th.Thread(target=buzz, args=( 100, 5, 90 )) # Cria a thread e depois coloca-a numa lista de threads ativas bThread.start() # inicia f[0] = 0 f[3] = 0 f[6] = 0 f[9] = 0 nokia.print_bars(f) #distance = str(min(dists)) + " cm " print('vai comecar') distance = str(dist1) + "cm" position = (20, 10) nokia.write_text(distance, position) print('agora vai')
max_steps = int(next(f)) print("max_steps=", max_steps) except: pass # Straighten robot. exec(open("servo_reset.py").read()) sleep(2) # Initialize servos. I2C_servo_bus = I2C(0, speed=100000, sda=27, scl=26) robot = Servo(I2C_servo_bus) # Initialize sonic distance sensor. # trigger=32, echo=33 distance_sensor = hcsr04.HCSR04(32, 33) # Run. right_ping_angles_len = len(right_ping_angles) right_ping_distances = [max_valid_food_distance + 1] * right_ping_angles_len left_ping_angles_len = len(left_ping_angles) left_ping_distances = [max_valid_food_distance + 1] * left_ping_angles_len head_swing_side = 'neutral' head_swing_count = 0 right_swing_count = 0 left_swing_count = 0 turn_angle_delta = forward_angle_delta angles_array_len = len(angles_array) step = 0 step_index = 0 while step_index < angles_array_len and (max_steps == -1 or step < max_steps):
def main(): user = False saveDHTData = True logFile = "logger.txt" # Get the sensors dht22S = dht22.DHT22() hcsr04S = hcsr04.HCSR04() ledS = led.LED() resetPB = push_button.PushButton(dht22S, ledS, 20) # Create the queues for each sensor qDatadht = Q.Queue(1000) qdhtExit = Q.Queue(1) qDataHC = Q.Queue(1000) qhcExit = Q.Queue(1) serverQ = Q.Queue(100) sQExit = Q.Queue(1) # Create locks dhtLock = t.Lock() hcLock = t.Lock() dhtExitL = t.Lock() hcExitL = t.Lock() serverLock = t.Lock() sExitL = t.Lock() # Create the thread for DHT sensor thread1 = sThread.SensorThread(1, "Thread1", qDatadht, dhtLock, qdhtExit, dhtExitL, dht22S) # Create thread for HC-SR04 sensor thread2 = sThread.SensorThread(2, "Thread2", qDataHC, hcLock, qhcExit, hcExitL, hcsr04S) # Thread for the server serverT = server.ServerS(3, "Server", logFile, serverQ, serverLock, sQExit, sExitL) # Start thread1.start() thread2.start() serverT.start() endProcess = False while not endProcess: dht22Data = None # Is there data from DHT22? dhtLock.acquire() if not qDatadht.empty(): dht22Data = qDatadht.get() dhtLock.release() # Is there data from HC-SR04? hcData = None hcLock.acquire() if not qDataHC.empty(): hcData = qDataHC.get() hcLock.release() # Was there a presence? if hcData: pTime = time.asctime() data = ["HC", hcData, pTime] ledS.turnOnLED() writeToFile(logFile, data) # DHT data if saveDHTData: if dht22Data: if dht22Data[0] != None: data = [" DHT", dht22Data[0], dht22Data[1]] writeToFile(logFile, data) # Verify is there are new params data = [] interval = [] serverLock.acquire() # Get the new parameters while not serverQ.empty(): data.append(serverQ.get()) serverLock.release() if not data: continue # Modify params is necessary for d in data: dSplit = d.split(",") value = float(dSplit[1]) if dSplit[0] == "RS" and value == 1: dht22S.setDefaults() ledS.turnOffLED() if dSplit[0] == "LI" and value != -1: interval.append(dSplit[1]) if dSplit[0] == "UI" and value != -1: interval.append(dSplit[1]) if dSplit[0] == "SP" and value != -1: dht22S.setPSampling(float(dSplit[1])) if dSplit[0] == "SV" and value != -1: saveDHTData = bool(int(dSplit[1])) if dSplit[0] == "TF" and value != -1: ledS.turnOffLED() # if len(interval) > 0: dht22S.setSamplingInterval(int(interval[1]), int(interval[2])) if not dht22S.withinInterval(time.time()): saveDHTData = False
from machine import Pin import hcsr04 hcsr = hcsr04.HCSR04(25, 27) def get_distance(): return hcsr.distance_cm()
def main(): global min_dist sensor1 = hcsr04.HCSR04(4, 19) sensor2 = hcsr04.HCSR04(16, 26) #sensor3 = hcsr04.HCSR04(x,y) nokia = screen.Screen() f = 255 * np.ones(12) nokia.print_bars(f) b = buzzer2.Buzzer(18, 100) b.start() while (True): dists = [] dist1 = sensor1.distance_cm() if 150 < dist1: #b.stopBuzz f[0] = 255 f[3] = 255 f[6] = 255 f[9] = 255 nokia.print_bars(f) elif 75 < dist1 <= 150: #b.buzz(100,95) f[0] = 0 f[3] = 255 f[6] = 255 f[9] = 255 nokia.print_bars(f) elif 50 < dist1 <= 75: #b.buzz(700,95) f[0] = 0 f[3] = 0 f[6] = 255 f[9] = 255 nokia.print_bars(f) elif 25 < dist1 <= 50: #b.buzz(2000,95) f[0] = 0 f[3] = 0 f[6] = 0 f[9] = 255 nokia.print_bars(f) elif 0 < dist1 < 25: #b.buzz(3000,95) f[0] = 0 f[3] = 0 f[6] = 0 f[9] = 0 nokia.print_bars(f) dist2 = sensor2.distance_cm() if 150 < dist2: f[1] = 255 f[4] = 255 f[7] = 255 f[10] = 255 nokia.print_bars(f) elif 75 < dist2 <= 150: f[1] = 0 f[4] = 255 f[7] = 255 f[10] = 255 nokia.print_bars(f) elif 50 < dist2 <= 75: f[1] = 0 f[4] = 0 f[7] = 255 f[10] = 255 nokia.print_bars(f) elif 25 < dist2 <= 50: f[1] = 0 f[4] = 0 f[7] = 0 f[10] = 255 nokia.print_bars(f) elif 0 < dist2 < 25: f[1] = 0 f[4] = 0 f[7] = 0 f[10] = 0 nokia.print_bars(f) ''' dist3 = sensor3.distance_cm() if 150 < dist3: f[2] = 255 f[5] = 255 f[8] = 255 f[11] = 255 nokia.print_bars(f) elif 75 < dist3 <= 150: f[2] = 0 f[5] = 255 f[8] = 255 f[11] = 255 nokia.print_bars(f) elif 50 < dist3 <= 75: f[2] = 0 f[5] = 0 f[8] = 255 f[11] = 255 nokia.print_bars(f) elif 25 < dist3 <= 50: f[2] = 0 f[5] = 0 f[8] = 0 f[11] = 255 nokia.print_bars(f) elif 0 < dist3 < 25: f[2] = 0 f[5] = 0 f[8] = 0 f[11] = 0 nokia.print_bars(f) ''' if dist1 != -1: dists.append(dist1) if dist2 != -1: dists.append(dist2) #if dist3 != -1: # dists.append(dist3) min_dist = min(dists) #b.run(min_dist) distance = str(min_dist) + " cm" position = (20, 10) nokia.write_text(distance, position)
import micropython import random import gc _BRIGHTNESS = const(0b11100100) _STRIP = pyb.SPI(1, pyb.SPI.MASTER, baudrate=4000000, crc=None, bits=8, firstbit=pyb.SPI.MSB, phase=1) DISTANCE_SENSORS = [ hcsr04.HCSR04(machine.Pin(p), machine.Pin(p)) for p in ('X1', 'X2', 'X3', 'X4') ] closest_distance_sensor = 0 _USER_BUTTON = pyb.Switch() @micropython.native def rainbow(h, xbgr): # h is hue between 0-119. if h < 20: xbgr[3] = 255 xbgr[2] = (h * 255) // 20 xbgr[1] = 0 elif h < 40:
def main(): sensor1 = hcsr04.HCSR04(4, 19) sensor2 = hcsr04.HCSR04(16, 26) #sensor3 = hcsr04.HCSR04(x,y) GPIO.setmode(GPIO.BCM) GPIO.setup(18, GPIO.OUT) pwm = GPIO.PWM(18, 100) nokia = screen.Screen() f = 255 * np.ones(12) nokia.print_bars(f) DC = 95 #b = buzzer.Buzzer(18, 100,0) #b.stop() while (True): dists = [] dist1 = sensor1.distance_cm() if 150 < dist1: pwm.stop() f[0] = 255 f[3] = 255 f[6] = 255 f[9] = 255 nokia.print_bars(f) elif 75 < dist1 <= 150: #b.buzz(100,95) pwm.ChangeFrequency(100) pwm.start(DC) f[0] = 0 f[3] = 255 f[6] = 255 f[9] = 255 nokia.print_bars(f) time.sleep(1) elif 50 < dist1 <= 75: #b.buzz(700,95) f[0] = 0 f[3] = 0 f[6] = 255 f[9] = 255 nokia.print_bars(f) elif 25 < dist1 <= 50: #b.buzz(2000,95) f[0] = 0 f[3] = 0 f[6] = 0 f[9] = 255 nokia.print_bars(f) elif 0 < dist1 < 25: #b.buzz(3000,95) pwm.ChangeFrequency(1000) pwm.start(DC) f[0] = 0 f[3] = 0 f[6] = 0 f[9] = 0 nokia.print_bars(f) time.sleep(1) dist2 = sensor2.distance_cm() if 150 < dist2: pwm.stop() f[1] = 255 f[4] = 255 f[7] = 255 f[10] = 255 nokia.print_bars(f) elif 75 < dist2 <= 150: f[1] = 0 f[4] = 255 f[7] = 255 f[10] = 255 nokia.print_bars(f) elif 50 < dist2 <= 75: f[1] = 0 f[4] = 0 f[7] = 255 f[10] = 255 nokia.print_bars(f) elif 25 < dist2 <= 50: f[1] = 0 f[4] = 0 f[7] = 0 f[10] = 255 nokia.print_bars(f) elif 0 < dist2 < 25: f[1] = 0 f[4] = 0 f[7] = 0 f[10] = 0 nokia.print_bars(f) ''' dist3 = sensor3.distance_cm() if 150 < dist3: f[2] = 255 f[5] = 255 f[8] = 255 f[11] = 255 nokia.print_bars(f) elif 75 < dist3 <= 150: f[2] = 0 f[5] = 255 f[8] = 255 f[11] = 255 nokia.print_bars(f) elif 50 < dist3 <= 75: f[2] = 0 f[5] = 0 f[8] = 255 f[11] = 255 nokia.print_bars(f) elif 25 < dist3 <= 50: f[2] = 0 f[5] = 0 f[8] = 0 f[11] = 255 nokia.print_bars(f) elif 0 < dist3 < 25: f[2] = 0 f[5] = 0 f[8] = 0 f[11] = 0 nokia.print_bars(f) ''' if dist1 != -1: dists.append(dist1) if dist2 != -1: dists.append(dist2) #if dist3 != -1: # dists.append(dist3) min_dist = min(dists) distance = str(min_dist) + " cm" position = (20, 10) nokia.write_text(distance, position)
button_F = Button(6, hold_time=.15) button_B = Button(13, hold_time=.15) button_L = Button(19, hold_time=.15) button_R = Button(26, hold_time=.15) button_1 = Button(16, hold_time=.2) button_2 = Button(20, hold_time=.2) button_3 = Button(21, hold_time=1) GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.cleanup(18) tempHumSensor = dht11.DHT11( pin=18) # Creating Temperature/Humidity sensor object distSensor = hcsr04.HCSR04(trig=24, echo=23) # Creating Distance sensor object execute = False move = True cameraActive = False cameraButton = False imageNum = 0 maxSpd = 200 currentLiftPos = 0 currentHeadAng = 0 joystickDZ = range(31000, 33500) distanceList = [] spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)
# micropython script that measures distance in mm. import utime import hcsr04 # define pins TRIGGER_PIN = const(4) ECHO_PIN = const(5) # setup sensor sensor = hcsr04.HCSR04(TRIGGER_PIN, ECHO_PIN) while True: distance = sensor.distance_mm() print("Distance: {:.1f} mm".format(distance), end='\r') utime.sleep_ms(500)
def main(): global min_dist sensor1 = hcsr04.HCSR04(4,19) sensor2 = hcsr04.HCSR04(16,26) sensor3 = hcsr04.HCSR04(13,6) nokia = screen.Screen() f = 255*np.ones(12) nokia.print_bars(f) z = bz.init_buzzer(18, 100) b=th.Thread(target = func, args =()) while(True): dists = [] dist1 = sensor1.distance_cm() if 150 < dist1: f[0] = 255 f[3] = 255 f[6] = 255 f[9] = 255 nokia.print_bars(f) elif 75 < dist1 <= 150: #b.buzz(100,95) f[0] = 0 f[3] = 255 f[6] = 255 f[9] = 255 nokia.print_bars(f) elif 50 < dist1 <= 75: #b.buzz(700,95) f[0] = 0 f[3] = 0 f[6] = 255 f[9] = 255 nokia.print_bars(f) elif 25 < dist1 <= 50: #b.buzz(2000,95) f[0] = 0 f[3] = 0 f[6] = 0 f[9] = 255 nokia.print_bars(f) elif 0 < dist1 < 25: #b.buzz(3000,95) f[0] = 0 f[3] = 0 f[6] = 0 f[9] = 0 nokia.print_bars(f) dist2 = sensor2.distance_cm() if 150 < dist2: f[1] = 255 f[4] = 255 f[7] = 255 f[10] = 255 nokia.print_bars(f) elif 75 < dist2 <= 150: f[1] = 0 f[4] = 255 f[7] = 255 f[10] = 255 nokia.print_bars(f) elif 50 < dist2 <= 75: f[1] = 0 f[4] = 0 f[7] = 255 f[10] = 255 nokia.print_bars(f) elif 25 < dist2 <= 50: f[1] = 0 f[4] = 0 f[7] = 0 f[10] = 255 nokia.print_bars(f) elif 0 < dist2 < 25: f[1] = 0 f[4] = 0 f[7] = 0 f[10] = 0 nokia.print_bars(f) dist3 = sensor3.distance_cm() if 150 < dist3: f[2] = 255 f[5] = 255 f[8] = 255 f[11] = 255 nokia.print_bars(f) elif 75 < dist3 <= 150: f[2] = 0 f[5] = 255 f[8] = 255 f[11] = 255 nokia.print_bars(f) elif 50 < dist3 <= 75: f[2] = 0 f[5] = 0 f[8] = 255 f[11] = 255 nokia.print_bars(f) elif 25 < dist3 <= 50: f[2] = 0 f[5] = 0 f[8] = 0 f[11] = 255 nokia.print_bars(f) elif 0 < dist3 < 25: f[2] = 0 f[5] = 0 f[8] = 0 f[11] = 0 nokia.print_bars(f) if dist1 != -1: dists.append(dist1) if dist2 != -1: dists.append(dist2) if dist3 != -1: dists.append(dist3) min_dist = min(dists) #print('ta dando'+ str(min_dist)) distance = str(min_dist) + " cm" position = (20,10) nokia.write_text(distance, position) if(not b.is_alive()): #print('morta') b=th.Thread(target = func, args =()) b.start()
def main(): sensor1 = hcsr04.HCSR04(4, 19) sensor2 = hcsr04.HCSR04(16, 26) sensor3 = hcsr04.HCSR04(13, 6) nokia = screen.Screen() f = 255 * np.ones(12) nokia.print_bars(f) buzz = bz.init_buzzer(18, 100) alarm_thread = th.Thread(target=alarm) while (True): dists = [] dist1 = sensor1.distance_cm() if 150 < dist1: f[0] = 255 f[3] = 255 f[6] = 255 f[9] = 255 nokia.print_bars(f) elif 75 < dist1 <= 150: f[0] = 0 f[3] = 255 f[6] = 255 f[9] = 255 nokia.print_bars(f) elif 50 < dist1 <= 75: f[0] = 0 f[3] = 0 f[6] = 255 f[9] = 255 nokia.print_bars(f) elif 25 < dist1 <= 50: f[0] = 0 f[3] = 0 f[6] = 0 f[9] = 255 nokia.print_bars(f) elif 0 < dist1 < 25: f[0] = 0 f[3] = 0 f[6] = 0 f[9] = 0 nokia.print_bars(f) dist2 = sensor2.distance_cm() if 150 < dist2: f[1] = 255 f[4] = 255 f[7] = 255 f[10] = 255 nokia.print_bars(f) elif 75 < dist2 <= 150: f[1] = 0 f[4] = 255 f[7] = 255 f[10] = 255 nokia.print_bars(f) elif 50 < dist2 <= 75: f[1] = 0 f[4] = 0 f[7] = 255 f[10] = 255 nokia.print_bars(f) elif 25 < dist2 <= 50: f[1] = 0 f[4] = 0 f[7] = 0 f[10] = 255 nokia.print_bars(f) elif 0 < dist2 < 25: f[1] = 0 f[4] = 0 f[7] = 0 f[10] = 0 nokia.print_bars(f) dist3 = sensor3.distance_cm() if 150 < dist3: f[2] = 255 f[5] = 255 f[8] = 255 f[11] = 255 nokia.print_bars(f) elif 75 < dist3 <= 150: f[2] = 0 f[5] = 255 f[8] = 255 f[11] = 255 nokia.print_bars(f) elif 50 < dist3 <= 75: f[2] = 0 f[5] = 0 f[8] = 255 f[11] = 255 nokia.print_bars(f) elif 25 < dist3 <= 50: f[2] = 0 f[5] = 0 f[8] = 0 f[11] = 255 nokia.print_bars(f) elif 0 < dist3 < 25: f[2] = 0 f[5] = 0 f[8] = 0 f[11] = 0 nokia.print_bars(f) if dist1 != -1: dists.append(dist1) if dist2 != -1: dists.append(dist2) if dist3 != -1: dists.append(dist3) global min_dist min_dist = min(dists) distance = str(min_dist) + " cm" position = (20, 10) nokia.write_text(distance, position) if (not alarm_thread.is_alive()): alarm_thread = th.Thread(target=alarm) alarm_thread.start()
# # Mesure de distance # # Programme permettant d'utiliser la librairie qui gere le capteur de # distance ultrason HC-SR04 # Tester sur NodeMCU Lolin et Wemos D1 mini # # Auteur iTechnoFrance # import hcsr04, time # pin_echo : pin pour mesurer la distance # pin_trig : pin pour envoyer les impulsions pin_echo = 4 # sortie D2 --> GPIO04 pin_trigger = 5 # sortie D1 --> GPIO05 # initialisation librairie HC-SR04 hc_sr04 = hcsr04.HCSR04(pin_echo, pin_trigger) while True: distance = hc_sr04.lecture_distance() if (distance == -1): print("Mesure supérieure à 4 mètres") else: print((distance), "cm") time.sleep(5)