[SOLVED] LEGO-Color Sensor doesn't turn off

So I was trying to get the Lego Color Sensor to work using the default test file (Python). I noticed that the color sensor was not turning off after the code finished. Just as a refresher, the code is:

from BrickPi import *

BrickPiSetup()

Color_Sensor_Port = PORT_3
BrickPi.SensorType[Color_Sensor_Port] = TYPE_SENSOR_COLOR_RED

BrickPiSetupSensors()

while True:
    result = BrickPiUpdateValues()
    time.sleep(.1)

After I cut the code using “control C”, which is a Keyboard Interrupt, the light does not turn off like it is supposed to.
If anyone has something that I could try to fix this problem, I’m all ears.
Thank you.

You’ll need a way to catch the CTRL+C exit and reconfigure the sensor type as something like TYPE_SENSOR_RAW before actually terminating the program.

See here for an example of how to implement this method of program cleanup. Note that example is for the BrickPi3 and in this case it’s only for reference for the try: except KeyboardInterrupt: way of program cleanup.

So what does the TYPE_SENSOR_RAW do?

TYPE_SENSOR_RAW basically sets the sensor type to “none” (un-configure the color sensor).

So I just tried that and it did not work.
Just to be clear, the whole code line is

BrickPi.SensorType[PORT_3] = TYPE_SENSOR_RAW

Is this correct?

That is correct, but you also need BrickPiSetupSensors() to make the sensor type take effect. Also, rather than using TYPE_SENSOR_RAW, it might be better to use TYPE_SENSOR_COLOR_NONE (keep the sensor configured as NXT color sensor, but turn off the LED).

It worked.
Thank you very much, Matt.

This topic was automatically closed after 24 hours. New replies are no longer allowed.