My main idea is to make a temperature sensor change colors based on the following:
I want it to be green between 18 and 24 degrees C.
Once it is more than 24 degrees C, I want it to be red and fade to orange as it gets hotter.
Once it falls below 18 degrees C, I want it to be blue and fade to cyan/aqua as it gets colder.
I am relatively new to coding. It is a project that should help me learn a bit. If anyone could help me with the coding and the setup that would be great. I was also wondering if there was a way to record the data read by the temperature sensor.
Thanks! also, any tips with coding would be much appreciated or why you have to do the things that you do to make it!
You can use linear interpolation, example function:
uint8_t lerp( uint8_t start, uint8_t end, uint8_t step, uint8_t steps)
{
return (end - start) * step / steps + start;
}
At 24°C, the LED is red: R = 255, G = 0, B = 0
At 34°C, the LED is orange: R = 255, G = 165, B = 0
There is 10°C difference, let's say there are 10 steps, one for each degree.
Say you want to calculate the color of the LED at 28°C (the 4th step):
uint8_t r = lerp( 255, 255, 4, 10 );
uint8_t g = lerp( 0, 165, 4, 10 );
uint8_t b = lerp( 0, 0, 4, 10 );
The result is:
r = 255
g = 66
b = 0
But like how do you initially start doing the code? I have a lot of trouble understanding like how the code actually works that way
Start by writing or finding code that works with your temperature sensor and prints the temperature to the serial monitor. Similary, find or write code to understand how to change the colors of a LED. Then once you are confident in these techniques, combine the code. Instead of printing the temperature, you use the temperature value to color the LED.
It is always better to work on small bits of code if you can, to get the concept right. Once you have those, combining them becomes easier as you start from a base of something you know worked before. Otherwise, especially for a beginner, there is too much that could be wrong all at once.
You might find that this does not look as good as you hope. This is because the eye's perception of colour depends on the colours surround it. So a slow fade might look the same for a relatively large real colour swing.
Well what if I did something more like this?
Red: Room Temp + 6 degrees C
Orange: Room Temp + 4 degrees C
Yellow: Room Temp + 2 degrees C
Green: Room Temp
Blue: Room Temp - 2 degrees C
Purple: Room Temp - 4 degrees C
Pink: Room temp - 6 degrees C
The representation you are describing is called a "color map" which uses colors to represent different values of something you've measured. There are lots of approaches. Here is one overview: http://www.sandia.gov/~kmorel/documents/ColorMaps/
Rather than try to decide now which colors should represent what temperatures, I suggest that you first learn to control the RGB LED. This is much harder than it seems, because you have to figure out sets of 3 numbers, each in the range of 0-255, that give some desired color. That in turn depends on exactly how you have the RGB LED wired to the Arduino, in particular it matters what current flows through each individual LED in the package and that in turn depends on the PWM value and what values of voltage dropping resistors are used.
Then there is the problem that the eye responds to light intensity logarithmically, so not all combinations of those three numbers are useful. This means that what you have to do in a program to "smoothly" change from one color to another or from one intensity of one color to another intensity, is not at all obvious.
Then you need to decide what actually works, that is, what colors are meaningful to you and others to represent temperatures or temperature differences. This has more to do with human physiology than math or computer hardware.
Edit: Note that the link above provides some convenient spreadsheet tables with values of R,G,B that give smooth changes in color, for various different mappings.
With a finer scale you should see more obvious colour change.
I would make the comment that the selection is that reds are usually associated with warmer temperatures (Yellow, Orange, Red) while Green to Blue are associated with cooler temperatures (Green, Cyan, Blue). Going from blue to pink for the coldest temperature may cause some perceptual difficulties for someone who does not know the colour scheme you have chosen.
A neutral middle colour could be a white LED (ie all RGB on). The transition to the other colors is then easier on the RGB color chart.
If you have time, check out the RGB mood lamp colour transitions in my sketch found in the code reporitory linked below. The RGB color cube can be used to your advantage and you may see how reading the comments in that sketch.
Some examples of controlling RGB LED are on the ArduinoInfo.Info WIKI HERE:
An example that runs through all the RGB combinations is HERE:
Info/Example of temperature measurement HERE:
I was looking for some other info and noticed that Uno's pins 5 and 6 have some strange PWM behavior (duty cycle) than rest of analog PWM pins. It is in documentation, just FYI.