我正在用rgb制作arduino控制的灯。我正在尝试在每种颜色之间增加平滑的过渡,这会从led发光。我不希望单独对频道进行动画处理。目前,它一次只能使用一个通道,而晶体管只能在亮度增加时使用。
const int redpin = 9;
const int greenpin = 10;
const int bluepin = 11;
int currRed = 0;
int currGreen = 0;
int currBlue = 0;
int dur = 1000;
void setup() {
pinMode(redpin, OUTPUT);
pinMode(greenpin, OUTPUT);
pinMode(bluepin, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
int red = Serial.parseInt();
int green = Serial.parseInt();
int blue = Serial.parseInt();
if (Serial.read() == '\n') {
red = constrain(red, 0, 255);
green = constrain(green, 0, 255);
blue = constrain(blue, 0, 255);
}
updateRGBfade(redpin, red, dur, currRed);
updateRGBfade(greenpin, green, dur, currGreen);
updateRGBfade(bluepin, blue, dur, currBlue);
}
}
void updateRGBfade(int pin, int col, int animdur, int currcol) {
if (col > currcol) {
for (int setto = currcol; setto < col; setto++) {
analogWrite(pin, setto);
delay(5);
}
}
if (col < currcol) {
for (int setto = currcol; setto > col; setto=setto-1) {
analogWrite(pin, setto);
delay(5);
}
}
}
答案 0 :(得分:0)
Currcolors(currRed,currGreen和currBlue)被视为恒定且等于零。因此,updateRGBfade函数仅在发生阻塞时才首先运行,因为currcol始终为零,我认为col通常大于零。因此该功能仅运行增加过程。如果阻止波纹管延迟(5)s
,则可以在函数中添加跟随行if(pin == redpin){
currRed = currcol;
}else if(pin == greenpin){
currGreen = currcol;
}else if(pin == bluepin ){
currBlue = currcol;
}
对不起,我的英语不好。