Concept
Using both digital and analog switches, I came up with a scheme that allows to change the colour and intensity of the RGB LED depending on the state of the button and values captured by the temperature sensor.
When the button is pressed, the RGB LED turns off, otherwise it is on – this is an example of working with digital switch, which works with high-low states. When the temperature sensor captures a value higher than 25C, the RGB LED lights up with red, and the light becomes more intense the higher the temperature increases. When the sensor captures a value lower than 25C, the RGB lights up with blue, and the light becomes more intense the lower the temperature decreases – this is an example of working with an analog switch, which allows to manipulate a spectrum of values.
Highlight of the code
It took me a long time to come up with the baseline temperature that would make the switch from red to blue light in room temperature conditions possible. In the end, I could use my own hands to make the sensor feel warmth, and then I used a frozen thermos wrapped in tissues to make the value decrease below 25 degrees.
if (switchState == LOW) { if (temperature > baselineTemp) { int redBrightness = map(temperature, baselineTemp, 30, 0, 255); redBrightness = constrain(redBrightness, 0, 0); analogWrite(redPin, redBrightness); analogWrite(greenPin, 0); analogWrite(bluePin, 0); } else { int blueBrightness = map(temperature, baselineTemp, 25, 0, 255); blueBrightness = constrain(blueBrightness, 0, 0); analogWrite(redPin, 0); analogWrite(greenPin, 0); analogWrite(bluePin, blueBrightness); } }
Demonstration
Reflection
For future improvements of this idea, I could play with more shades of RGB LED light that would be associated with different values on the spectrum, since this electrical component allows to work with almost any colour of the light.