MicroPython Codes of the PicoBricks
from machine import Pin, PWM
import utime
#define the libraries
servo = PWM(Pin(21, Pin.OUT))
trigger = Pin(15, Pin.OUT)
echo = Pin(14, Pin.IN)
#define the input and output pins
servo.freq(50)
servo.duty_u16(6750)
def getDistance():
trigger.low()
utime.sleep_us(2)
trigger.high()
utime.sleep_us(5)
trigger.low()
while echo.value() == 0:
signaloff = utime.ticks_us()
while echo.value() == 1:
signalon = utime.ticks_us()
timepassed = signalon - signaloff
distance = (timepassed * 0.0343) / 2
print("The distance from object is ", distance, "cm")
return distance
#calculate distance
while True:
utime.sleep(0.01)
if int(getDistance()) <= 5: # if the distance variable is less than 5
servo.duty_u16(4010)
utime.sleep(0.3) # wait
servo.duty_u16(6750)
Arduino C Codes of the PicoBricks
#include <Servo.h>
#define trigPin 15
#define echoPin 14
//define the libraries
Servo servo;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//define the input and output pins
servo.attach(21); //define the servo pin
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
//calculate distance
if (distance < 5) { //if the distance variable is less than 5
Serial.print(distance);
Serial.println(" cm");
servo.write(179);
}
else if (distance > 5) { // if the distance variable is greater than 5
Serial.print(distance);
Serial.println(" cm");
servo.write(100);
}
}
GitHub Project Page
Tags: