我正在尝试使用neopixels来获取gemma的arduino代码,其中5310字节的内存更小,因此我可以在程序中获得更多内容。
目前我正在尝试删除浮动/缩小下面代码片段的大小:
void gradient(Color c1, Color c2, float time) {
for (float i = 0; i < time; i += 0.001) {
Color result(0, 0, 0);
result.Red = c1.Red * (1 - (i / time)) + c2.Red * (i / time);
result.Green = c1.Green * (1 - (i / time)) + c2.Green * (i / time);
result.Blue = c1.Blue * (1 - (i / time)) + c2.Blue * (i / time);
for (uint8_t x = 0; x < 20; x++)pixels.setPixelColor(x, result.Red, result.Green, result.Blue);
pixels.show();
delay(1);
}
}
我设法将它减少30个字节到:
void gradient(Color c1, Color c2, float time) {
float stepsize = 0.01; // Stepsize in seconds
float lambda;
int maxiter = (int) (time/ stepsize);
Color result(0, 0, 0);
for (int i = 0; i <= maxiter; i++) {
lambda = (float) i / maxiter;
result.Red = c1.Red * (1 - lambda) + c2.Red * (lambda);
result.Green = c1.Green * (1 - lambda) + c2.Green * (lambda);
result.Blue = c1.Blue * (1 - lambda) + c2.Blue * (lambda);
for (uint8_t x = 0; x < 20; x++)pixels.setPixelColor(x, result.Red, result.Green, result.Blue);
pixels.show();
delay(stepsize * 1000); // delay in milliseconds
}
}
但我还在努力让它变得更小 对于那些想知道Color对象只是一个有3个整数的对象,称为红色,绿色和蓝色。此代码的示例用法是:
gradient(Color(255, 0, 0), Color(0, 255, 0), 2);
在2秒内从红色到绿色的梯度 提前谢谢!
答案 0 :(得分:0)
- 首先,您的颜色对象应该采用3个无符号字符(0-255),没有理由在那里放入。 (arduino中的字节类型)
- 第二,我不确定你是如何实现时间的,但通常在arduino中你工作的时间是毫秒。此外,在没有看到你的其他实现的情况下,我猜测时间是一段时间,根据你的延迟,我猜你可以发短时间(如果需要可以增加 x1000 ) )(这将持续32秒,以毫秒为单位)
void gradient(Color c1, Color c2, short time) {
short maxiter = (short) (time/ 10);
Color result(0, 0, 0);
for (int i = 0; i <= maxiter; i++) {
result.Red = (c1.Red * (maxiter-i) + c2.Red * i)/maxiter;
result.Green = (c1.Green* (maxiter-i) + c2.Green* i)/maxiter;
result.Blue = (c1.Blue* (maxiter-i) + c2.Blue* i)/maxiter;
for (uint8_t x = 0; x < 20; x++)pixels.setPixelColor(x, result.Red, result.Green, result.Blue);
pixels.show();
delay(10); // delay in milliseconds
}
}
答案 1 :(得分:0)
如果你可以拉&#34;延迟()&#34;在你的所有代码中,它似乎避免包含一个100字节大小的库? idk tbh,但这是我建议的修改,在我的测试中保存了100个字节的内存:
void gradient(Color c1, Color c2, float time) {
float stepsize = 0.01; // Stepsize in seconds
float lambda;
int maxiter = (int) (time/ stepsize);
Color result(0, 0, 0);
for (int i = 0; i <= maxiter; i++) {
lambda = (float) i / maxiter;
result.Red = c1.Red * (1 - lambda) + c2.Red * (lambda);
result.Green = c1.Green * (1 - lambda) + c2.Green * (lambda);
result.Blue = c1.Blue * (1 - lambda) + c2.Blue * (lambda);
for (uint8_t x = 0; x < 20; x++)pixels.setPixelColor(x, result.Red, result.Green, result.Blue);
pixels.show();
//delay(stepsize * 1000); // delay in milliseconds
long lastTime=millis();
long delayTime = stepsize * 1000;
while(millis()-lastTime<delayTime){}
}
}