Move To Color
Introduction
This tutorial shows how to move the EV3 driving base until the color sensor detects a color.
Here are the building instructions for the robot:
Step 1 Run code on button pressed
Drag ||brick:on button pressed||
block so that your code starts when the enter button is pressed (and not at the start). We are also using the ||brick:show string||
message easily diagnose if the program does not work.
brick.buttonEnter.onEvent(ButtonEvent.Pressed, function () {
brick.showString("started", 1)
})
Step 2 Turn on the motors
Drag a ||motors:steer motors B+C||
block under the button pressed event. This will turn on both motors.
brick.buttonEnter.onEvent(ButtonEvent.Pressed, function () {
brick.showString("started", 1)
motors.largeBC.steer(0, 50)
})
Step 3 Pause until color
Drag a ||sensors:pause until color detected||
block after the steer and select the color you want to detect. This block will stop the program until the color is detected.
brick.buttonEnter.onEvent(ButtonEvent.Pressed, function () {
brick.showString("started", 1)
motors.largeBC.steer(0, 50)
brick.showString("looking for red", 1)
sensors.color3.pauseUntilColorDetected(ColorSensorColor.Red)
})
Step 4 Stop the motors!
Once the color is detected, the program will continue to run blocks. Drag a ||motors:stop B+C motor||
so that both motors stop.
brick.buttonEnter.onEvent(ButtonEvent.Pressed, function () {
brick.showString("started", 1)
motors.largeBC.steer(0, 50)
brick.showString("looking for red", 1)
sensors.color3.pauseUntilColorDetected(ColorSensorColor.Red)
brick.showString("stop", 1)
motors.largeBC.stop()
})
Step 5
Download your program to your brick and try it out!