# Initialize EV3 touch sensor and motors motorLeft = Motor(Port.A) motorRight = Motor(Port.B) infrared = InfraredSensor(Port.S1) # Create a loop to react to buttons while True: # Check for center button events if Button.CENTER in ev3.buttons.pressed(): motorLeft.stop() motorRight.stop() break # React to the left up and down buttons if Button.LEFT_DOWN in infrared.keypad(): motorLeft.dc(-50) elif Button.LEFT_UP in infrared.keypad(): motorLeft.dc(50) else: motorLeft.stop() # React to the right up and down buttons if Button.RIGHT_DOWN in infrared.keypad(): motorRight.dc(-50) elif Button.RIGHT_UP in infrared.keypad(): motorRight.dc(50) else: motorRight.stop() # Uncomment to display the current status of the remote buttons
from time import sleep # This program requires LEGO EV3 MicroPython v2.0 or higher. # Click "Open user guide" on the EV3 extension tab for more information. # Create your objects here. ev3 = EV3Brick() # Write your program here. ev3.speaker.beep() # Initialize the motors. left_motor = Motor(Port.A) right_motor = Motor(Port.B) # Initialize the drive base. robot = DriveBase(left_motor, right_motor, wheel_diameter=55.5, axle_track=104) ir_sensor = InfraredSensor(Port.S4) ir_sensor.beacon(1) while True: buttons = ir_sensor.keypad() if len(buttons) == 1: print(buttons[0]) if (buttons[0] == Button.LEFT_UP): print("###") robot.straight(-30) elif (buttons[0] == Button.RIGHT_UP): pass elif (buttons[0] == Button.LEFT_DOWN): pass elif (buttons[0] == Button.RIGHT_DOWN): pass elif len(buttons) == 2: print(str(buttons[0]) + " and " + str(buttons[1])) else: continue
ev3.screen.print('Distance: ', dist, 'mm') ev3.speaker.say('come to me on channel 1') dist = 100 angle = 0 while dist > 10: wait(10) dist, angle = ir.beacon(1) ev3.screen.print('Distance: ', dist*2, 'mm') ev3.screen.print('Angle: ', angle, 'deg') ev3.speaker.say('last try, use channel 1') while True: buttons = ir.keypad() wait(10) if Button.LEFT_UP in buttons and Button.RIGHT_UP in buttons and Button.RIGHT_DOWN in buttons: ev3.speaker.play_file(Sound.GOODBYE) break elif Button.LEFT_UP in buttons and Button.RIGHT_UP in buttons: ev3.speaker.play_file(Sound.UP) elif Button.LEFT_DOWN in buttons and Button.RIGHT_DOWN in buttons: ev3.speaker.play_file(Sound.DOWN) elif Button.LEFT_UP in buttons and Button.LEFT_DOWN in buttons: ev3.speaker.play_file(Sound.LEFT) elif Button.RIGHT_UP in buttons and Button.RIGHT_DOWN in buttons: ev3.speaker.play_file(Sound.RIGHT) elif Button.LEFT_UP in buttons: