Pin ball - Code

From RoboWiki
Revision as of 17:51, 4 June 2023 by Robot (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Return back to project page: Pin ball - Fedor Agarshev

Python code for the Pin ball project:

 
from hub import light_matrix, port, button, light, speaker
import motor,time, color_sensor, color, distance_sensor


colorSensor = port.D
motorWithColorSensor = port.F
# timer = Timer()
# two_seconds_timer = Timer()
distance = port.A
motorPointer = port.B
motorPlunger = port.E
count = 0


##In version 3, I could not make a timer
# def show_timer(pixels_list):
#     if two_seconds_timer.now() > 2:
#         pixel = pixels_list.pop()
#         light_matrix.set_pixel(pixel[0], pixel[1],0)
#         for pixel in pixels_list:
#             light_matrix.set_pixel(pixel[0], pixel[1],100)
#         two_seconds_timer.reset()

#     return pixels_list


def start(withTimer = False):
    global count
    count = 0
    motor.run_to_relative_position(motorWithColorSensor,0,1000)
    motor.run_to_relative_position(motorPlunger,20,1000)
    time.sleep_ms(600)
    motor.run_for_degrees(motorWithColorSensor,-100,1000)
    motor.run_for_degrees(motorPlunger,270,1000)
    time.sleep_ms(600)
    motor.run_for_degrees(motorWithColorSensor,100,1000)
    motor.run_for_degrees(motorPlunger,-270,1000)

    
    light_matrix.show_image(44) #image SQUARE
    # pixels_list = [(2,0),(3,0),(4,0),(4,1),(4,2),(4,3),(4,4),(3,4),(2,4),(1,4),(0,4),(0,3),(0,2),(0,1),(0,0),(1,0)]
    # timer.reset()
    # two_seconds_timer.reset()

    motor.reset_relative_position(motorPointer,0)

    while True:
        # if withTimer == True:
        #     pixels_list = show_timer(pixels_list)
        if button.pressed(button.LEFT):
            motor.run_for_degrees(motorPlunger,270,1000)
            time.sleep_ms(600)
            motor.run_for_degrees(motorPlunger,-270,1000)
            light_matrix.write(str(count))

        if color_sensor.color(colorSensor) == color.GREEN: #which color must see color sensor for scoring
            print("color")
            count += 200
            
            motor.run_for_degrees(motorWithColorSensor,-110,1000)
            time.sleep_ms(600)
            motor.run_for_degrees(motorWithColorSensor,110,1000)
            speaker.beep(60, 100, 100)
        if distance_sensor.distance(distance) > 100:
            print("distance")
            count += 2
            speaker.beep(70, 100, 100)
        p = motor.relative_position(motorPointer)
        if p < -2 or p > 2:
            print("motor")
            count += 10
            motor.reset_relative_position(motorPointer,0)
            speaker.beep(65, 100, 100)
        # if withTimer == True:
        #     if timer.now() > 40:
        #         break




while True:
    while not button.pressed(button.LEFT): # for start you need hold button 
        light_matrix.write(str(count))
        time.sleep_ms(3000)
    start(False)