I have a value for led brightness that is stored as an unsigned char
(0-255)
unsigned char* colors[3];
colors[0] = 255;
colors[1] = 0;
colors[2] = 0;
I want to multiply a brightness percentage to that value.
float percent = 0.5;
auto red_brightness = colors[0] * percent
Since colors[0]
is of type char
and percent
is of type float
. It gives error Invalid operands of types 'unsigned char*' and 'float' to binary 'operator*'
unsigned char brightness;
float percent;
unsigned char* colors[3];
void setup() {
Serial.begin(9600);
colors[0] = 255;
colors[1] = 0;
colors[2] = 0;
}
void loop() {
brightness = 127;
float percent = (float)brightness / 255;
float newRed = (float)colors[0] * percent;
Serial.println(percent);
Serial.print("red ");
Serial.print(newRed);
Serial.println("");
delay(1000);
}
How can I multiply colors[0]
(255) by 0.5
to get 127
?
What I've tried
- Cast
colors[0]
to a float or int
# invalid cast from type 'unsigned char' to type 'float'
float newRed = (float)colors[0] * percent;
- strtod
Using this conversion website, I've tried converting like so:
float tempColor = (float)strtod(colors[0],NULL);
float newRed = tempColor * percent;
While this compiles, it always returns 0
(not 127
like I expect)
unsigned char brightness;
float percent;
unsigned char* colors[3];
void setup() {
Serial.begin(9600);
colors[0] = 255;
}
void loop() {
brightness = 127;
float percent = (float)brightness / 255;
float tempColor = (float)strtod(colors[0],NULL); // <- returns 0.00
Serial.print("tempColor");
Serial.println(tempColor);
delay(1000);
}
How can I multiply 255
by 0.5
to get ~127
?
Solution
Thanks to the pointer by st2000 and this sparkfun article on datatypes
I avoided floating point and char arrays and converted everything to byte
and now my code works as expected. Thank you
byte brightness;
byte percent;
byte colors[3];
void setup() {
Serial.begin(9600);
colors[0] = 255;
}
void loop() {
percent = 50;
auto foobar = colors[0] * percent / 100;
Serial.print("foobar: ");
Serial.println(foobar);
11:12:19.833 -> foobar: 127