Difference between revisions of "Pin ball - Code"

From RoboWiki
Jump to: navigation, search
(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 Spike Pong project:
+
Python code for the Pin ball project:
  
 
<syntaxhighlight lang=python>  
 
<syntaxhighlight lang=python>  
from spike import PrimeHub, LightMatrix, Button, StatusLight, ForceSensor, MotionSensor, Speaker, ColorSensor, App, DistanceSensor, Motor, MotorPair
+
from hub import light_matrix, port, button, light, speaker
from spike.control import wait_for_seconds, wait_until, Timer
+
import motor,time, color_sensor, color, distance_sensor
from spike.operator import equal_to
 
from math import *
 
  
hub = PrimeHub()
+
 
colorSensor = ColorSensor("D")
+
colorSensor = port.D
motorWithColorSensor = Motor("F")
+
motorWithColorSensor = port.F
timer = Timer()
+
# timer = Timer()
two_seconds_timer = Timer()
+
# two_seconds_timer = Timer()
distance = DistanceSensor("A")
+
distance = port.A
motorPointer = Motor("B")
+
motorPointer = port.B
motorPlunger = Motor("E")
+
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()
  
def show_timer(pixels_list):
+
#     return pixels_list
     if two_seconds_timer.now() > 2:
 
        pixel = pixels_list.pop()
 
        hub.light_matrix.set_pixel(pixel[0], pixel[1],0)
 
        for pixel in pixels_list:
 
            hub.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
     motorWithColorSensor.run_to_position(240)
+
     motor.run_to_relative_position(motorWithColorSensor,0,1000)
    motorWithColorSensor.run_to_position(0)
+
     motor.run_to_relative_position(motorPlunger,20,1000)
     motorPlunger.run_to_position(290)
+
     time.sleep_ms(600)
    motorPlunger.run_to_position(20)
+
     motor.run_for_degrees(motorWithColorSensor,-100,1000)
     hub.light_matrix.show_image('SQUARE')
+
    motor.run_for_degrees(motorPlunger,270,1000)
     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)]
+
     time.sleep_ms(600)
     timer.reset()
+
     motor.run_for_degrees(motorWithColorSensor,100,1000)
     two_seconds_timer.reset()
+
     motor.run_for_degrees(motorPlunger,-270,1000)
     pos = motorPointer.get_position()
+
 
 
      
 
      
     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)]
            pixels_list = show_timer(pixels_list)
+
    # timer.reset()
         if hub.left_button.was_pressed():
+
    # two_seconds_timer.reset()
             motorPlunger.run_to_position(290)
+
 
             motorPlunger.run_to_position(20)
+
    motor.reset_relative_position(motorPointer,0)
             hub.light_matrix.write(count)
+
 
 +
     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
 
              
 
              
        if colorSensor.get_color() == "green":
+
            motor.run_for_degrees(motorWithColorSensor,-110,1000)
             print("color")
+
             time.sleep_ms(600)
             count += 20
+
             motor.run_for_degrees(motorWithColorSensor,110,1000)
            motorWithColorSensor.run_for_degrees(-120)
+
             speaker.beep(60, 100, 100)
            motorWithColorSensor.run_for_degrees(120)
+
         if distance_sensor.distance(distance) > 100:
             hub.speaker.beep(60, 0.5)
 
         if distance.get_distance_cm(True) is None:
 
 
             print("distance")
 
             print("distance")
 
             count += 2
 
             count += 2
             hub.speaker.beep(70, 0.5)
+
             speaker.beep(70, 100, 100)
         p = motorPointer.get_position()
+
         p = motor.relative_position(motorPointer)
         if p < pos -2 or p > pos + 2:
+
         if p < -2 or p > 2:
 
             print("motor")
 
             print("motor")
             count += 1
+
             count += 10
             pos = p
+
             motor.reset_relative_position(motorPointer,0)
             hub.speaker.beep(65, 0.5)
+
             speaker.beep(65, 100, 100)
         if withTimer == True:
+
         # if withTimer == True:
            if timer.now() > 40:
+
        #    if timer.now() > 40:
                break
+
        #        break
  
  
Line 77: Line 85:
  
 
while True:
 
while True:
    hub.left_button.was_pressed()
+
     while not button.pressed(button.LEFT): # for start you need hold button
     while not hub.left_button.was_pressed():
+
         light_matrix.write(str(count))
         hub.light_matrix.write(count)
+
         time.sleep_ms(3000)
         wait_for_seconds(3)
+
     start(False)
     start(True)
 
  
 
</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)