We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I would appreciate some help (or direction) converting this perfect Processing code to an Arduino Sketch? It is the color conversion math/function I cannot seem to get through.
I have been through a lot of permutations with Linux/Windows platforms and SBCs' but the Arduino IDE and like microcontrollers seems to be the best option for this application.
Any help would be appreciated.
Thanks in Advance,
Ken
void applyColor() { // Generate the heat map
pushStyle(); // Save current drawing style
// Set drawing mode to HSB instead of RGB
colorMode(HSB, 1, 1, 1);
loadPixels();
int p = 0;
for (int r = 0; r < height; r++) {
for (int c = 0; c < width; c++) {
// Get the heat map value
float value = interp_array[c][r];
// Constrain value to acceptable range.
value = constrain(value, mintemp, maxtemp);
// Map the value to the hue
// 0.2 blue
// 1.0 red
value = map(value, mintemp, maxtemp, 0.2, 1.0);
pixels[p++] = color(value, 1, 2);
}
}
updatePixels();
popStyle(); // Restore original drawing style
}
Answers
println(Thread.currentThread());
as the 1st statement for your function applyColor().Apologies Mr. GoToLoop . . I do not understand these questions!
println(Thread.currentThread());
inside your applyColor()!Ok, I think I understand. Will give it a try. Should I post the entire sketch for you to review?
Enough to know the context. And where variables are declared and initialized too.
But as I've warned already, I don't have the hardware and thus my help is very limited! 8-|
So here it is in all its Glory! . . . Let me know if you can help with the conversion.
Thanks,
Ken
import processing.io.*; I2C i2c;
Do you understand how color works in processing?
https://processing.org/reference/colorMode_.html
As you see, the color is being managed by the processing engine. However, arduino does not process color, because it doesn't support it, it doesn't need to draw anything as I know that you are aware. Any color interpretation is for the user to implement.
Looking at your code, you input data contains actual temperature data. You keep track of the min and max temperatures. Half part of the conversion done by looking at the code that your provided.
The question is, what do you want for the output? You can output an array where min temp maps to 0.2 and max temp maps to 1.0. In the code you provided:
Arduino does not have an equivalent for color. You need to come up with your own function. Notice you are using
colorMode(HSB, 1, 1, 1);
but you have a 2 in the previous line. Why 2? Your max range in your processing code for HSB color is 1 for each hue, saturation and brightness. In other words, your line in processing should readpixels[p++] = color(value, 1, 1);
The following links shows that most of the conversion is already done for you as arduino provides equivalent functions:
https://www.arduino.cc/en/Reference/Constrain
https://www.arduino.cc/en/Reference/Map
Next link is additional for you to consider, a way to manage the output array containing the heat map: http://arduino.stackexchange.com/questions/30205/returning-an-int-array-from-a-function
The function you asked for would resembled the code below:
Kf
Thank you for your responses and advice. I will look into the links you provide.
I think that the most important thing right now is how the data is being used. Can you shed some light on that?
The temperature data is used to generate a heatmap on a display screen . . . Is that not evident by the Processong sketch . . . Or am I misunderstanding your question?
No, how will the parsed data be used by the Arduino? As in, after converting your data on the Arduino into an array pix[] (using kfrajer's code), what do you plan to do with the array?
To me, this implies you are running everything on your arduino. Are you planning to send your data to Processing? Or are you planning to use your data to manage a grid of LEDs? It is not clear what you want to do with his information.
Kf
Oh, ok, I understand what your asking. As in the Processing sketch, which has a "size (420,420)" to display the heat map, I will display on an TFT LCD of similar size.
Also, I don't get that code posted by Kf . . . . Arduino IDE has a problem with it.
PS - all code will be run on the Arduino platform . . . No Processing!
More details please.
Kf
You'll have to remove the asterix from the the function name in kfrajer's code.
To do what you're doing, you'll need to set up the tft library. Do that, get some experience in using a TFT screen before attempting this larger project.
Ok, thanks to all your help and advice I have made good progress in running this sketch on the Arduino . . . . I now realize I am spoiled by the rich color functions available in Processing.
what I have to do now is convert the interpolated color, which was so conveniently done in Processing, and using what is available in C++ (Arduino IDE).
Anyone interested in a challenge? :)
Thanks Again to all who are responding . . . .
Notice, my function should return float and not an int array reference since pix is a float array. I will edit my post to reflect this change.
@Lord_of_the_Galaxy I have not tested the code but I think you need to keep the asterix as I am returning an array. I a following the reference from http://arduino.stackexchange.com/questions/30205/returning-an-int-array-from-a-function Check title Have the caller manage the allocation.
Kf
Oh, I didn't notice what you were rerurning.
You don't even need to return it - you're editing the original array itself (assuming Arduino is based on C++, if it's based on C, then the process is different).