当我的机器人向前行驶过一条黑色带后,我该如何让它停下来并大声说出数字4 ?


%%sim_magic_preloaded --background Coloured_bands -R
# Program to count the bands aloud
# Start the robot moving
tank_drive.on(SpeedPercent(15), SpeedPercent(15))
# Initial count value
count = 0
# Initial sensor reading
previous_value = colorLeft.reflected_light_intensity_pc
# Create a loop
while True:
# Check current sensor reading
current_value = colorLeft.reflected_light_intensity_pc
# Test when the robot has entered a band
if previous_value==100 and current_value < 100:
# When on a new band:
# - increase the count
count = count + 1
# - display the count in the output window
print(count)
# - say the count aloud
say(str(count))
# Update previous sensor reading
previous_value = current_value

需要一些编码方面的帮助,我试图让机器人在越过黑色带后停止,这是第四个带,而且一旦越过黑色带,它就不会大声说出第4个

import time
previous_value = colorLeft.color
tank_drive.on(SpeedPercent(15), SpeedPercent(15))
count = 1
while True:
current_value = colorLeft.color
print(current_value)
if previous_value==6 and current_value != 6:
print(count)
say(str(count))
count += 1
#Now it should stop at band 4
if count > 4 and current_value==6:
time.sleep(1)
tank_drive.off()
break
previous_value = current_value

最新更新