Help with VSCode and optical sensor

Needing help, trying to see if I could use a function (define) something in my python code using Visual Studio Code and optical sensor.

This is what I got, trying to remember and translate from my C++ time:

rollerw = Motor(Ports.PORT13, GearSetting.RATIO_18_1, True)
colorsense = Optical(Ports.PORT4)

def redUp(speed):
    rollerw.spin(FORWARD, speed, VelocityUnits::PERCENT, False)
     while not colorsense == Color.RED:
        pass
    rollerw.stop

Now of course this doesnt work, but I’m going off limited knowledge and just trying to see what I can do…

@jpearman sorry to keep asking for help, wondering if I’m missing anything…

you would want something like this.

# Brain should be defined by default
brain=Brain()

rollerw=Motor(Ports.PORT1)
colorsense=Optical(Ports.PORT20)

def redUp(speed):
    rollerw.spin(FORWARD, speed, VelocityUnits.PERCENT)
    while not colorsense.is_near_object() or colorsense.color() != Color.RED:
        sleep(50)
    rollerw.stop()

In practice the optical sensor may still give Color.RED when it doesn’t really see any color (it has to return something) so it may be better to say while color is blue.

3 Likes