Color sensor |
---|
<p align="center"> |
Code Description |
This code is a simple color detection program that continuously checks for colors using a color sensor connected to port B of a device. If a color is detected, it prints the color name; if no color is detected, it prints “No color detected”, and it does this check every 4 seconds. |
import runloop
import color_sensor
from hub import port
import color
async def check_color():
while True: # This will create an infinite loop
color_detected = color_sensor.color(port.B)
if color_detected is not None:
print(f"Color detected: {color_detected}")
await runloop.sleep_ms(2000)
else:
print("No color detected")
await runloop.sleep_ms(2000) # wait for 4 seconds
runloop.run(check_color())`
Force sensor |
---|
<p align="center"> |
Code Description |
This code is a simple force detection program that continuously checks for force using a force sensor connected to port A of a device. If force is detected, it prints the force value; if no force is detected, it prints “No force detected”, and it does this check in an infinite loop. |
import runloop
import force_sensor
from hub import port
async def read_force():
while True:
force_detected = force_sensor.force(port.A)
if force_detected is not None:
print(f"\rForce detected: {force_detected}", end="")
else:
print("\rNo force detected", end="")
runloop.run(read_force())
Distance sensor |
---|
<p align="center"> |
Code Description |
This code is a distance sensor program that continuously measures and displays the distance of an object from the sensor. If no object is detected, it clears the sensor reading and waits for a second before repeating the process. |
import runloop
import distance_sensor
from hub import port
async def read_distance():
while True:
distance = distance_sensor.distance(port.C)
if distance is not None and distance >= 1:
print(f"Distance: {distance} in")
pixels = [100] * 4
distance_sensor.show(port.C, pixels)
await runloop.sleep_ms(1000)
else:
print("No object detected")
distance_sensor.clear(port.C)
await runloop.sleep_ms(1000)
runloop.run(read_distance())
Hub |
---|
<p align="center"> |
Code Description |
This code displays different images on a device’s light matrix based on button presses. If the left button is pressed, it shows a happy image, and if the right button is pressed, it shows a sad image. |
from hub import light_matrix
from hub import button
while True:
if button.pressed(button.LEFT):
light_matrix.show_image(light_matrix.IMAGE_HAPPY)
elif button.pressed(button.RIGHT):
light_matrix.show_image(light_matrix.IMAGE_SAD)
Motors |
---|
<p align="center"> |
Code Description |
This is code |
code?