Difference between revisions of "Pin ball - Code"
From RoboWiki
					
										
					
					|  (Created page with "Return back to project page: Pin ball - Fedor Agarshev  Python code for the Spike Pong project:  <syntaxhighlight lang=python>  fro...") | |||
| (One intermediate revision by the same user not shown) | |||
| Line 1: | Line 1: | ||
| Return back to project page: [[Spike Prime - Pin ball - Fedor Agarshev|Pin ball - Fedor Agarshev]] | Return back to project page: [[Spike Prime - Pin ball - Fedor Agarshev|Pin ball - Fedor Agarshev]] | ||
| − | Python code for the  | + | Python code for the Pin ball project: | 
| <syntaxhighlight lang=python>   | <syntaxhighlight lang=python>   | ||
| − | from  | + | from hub import light_matrix, port, button, light, speaker | 
| − | + | import motor,time, color_sensor, color, distance_sensor | |
| − | |||
| − | |||
| − | + | ||
| − | colorSensor =  | + | colorSensor = port.D | 
| − | motorWithColorSensor =  | + | motorWithColorSensor = port.F | 
| − | timer = Timer() | + | # timer = Timer() | 
| − | two_seconds_timer = Timer() | + | # two_seconds_timer = Timer() | 
| − | distance =  | + | distance = port.A | 
| − | motorPointer =  | + | motorPointer = port.B | 
| − | motorPlunger =  | + | motorPlunger = port.E | 
| count = 0 | 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): | def start(withTimer = False): | ||
|      global count |      global count | ||
|      count = 0 |      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) | |
| − | + | ||
| − |      while True:  | + |     light_matrix.show_image(44) #image SQUARE | 
| − |          if withTimer == True: | + |     # 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() | |
| − |          if  | + |     # two_seconds_timer.reset() | 
| − |              motorPlunger. | + | |
| − | + |     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: | |
| − | |||
| − |          if distance | ||
|              print("distance") |              print("distance") | ||
|              count += 2 |              count += 2 | ||
| − | + |              speaker.beep(70, 100, 100) | |
| − |          p =  | + |          p = motor.relative_position(motorPointer) | 
| − |          if  | + |          if p < -2 or p > 2: | 
|              print("motor") |              print("motor") | ||
| − |              count +=  | + |              count += 10 | 
| − | + |              motor.reset_relative_position(motorPointer,0) | |
| − | + |              speaker.beep(65, 100, 100) | |
| − |          if withTimer == True: | + |          # if withTimer == True: | 
| − | + |         #     if timer.now() > 40: | |
| − | + |         #         break | |
| Line 77: | Line 85: | ||
| while True: | while True: | ||
| − | + |      while not button.pressed(button.LEFT): # for start you need hold button  | |
| − |      while not  | + |          light_matrix.write(str(count)) | 
| − | + |          time.sleep_ms(3000) | |
| − | + |      start(False) | |
| − |      start( | ||
| </syntaxhighlight> | </syntaxhighlight> | ||
Latest revision as of 17:51, 4 June 2023
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)
