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.
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).