我刚刚做了完整的代码,上传主板串口监测了一下数据,变量数据都正常。我没有搭建电路,你可以试试把程序上传到主板看看效果咋样
********************************************************************************************
#define led_red 9 //Pin 9 连接上 led的红色引脚
#define led_green 10
#define led_blue 11
int d=0;
int e=0;
int f=0;
int a;
int b;
int c;
int Rr;
int Gg;
int Bb;
//变量d,e,f储存灯的亮度
void setup() {
Serial.begin(9600);
pinMode(led_red, OUTPUT);
pinMode(led_green, OUTPUT);
pinMode(led_blue, OUTPUT);
analogWrite(led_red, 0);
analogWrite(led_green, 0);
analogWrite(led_blue, 0);
}
void loop() {
if(d==0)
{
Rr = random(200);//随机产生红色
Gg = random(200);//随机产生绿色
Bb = random(200);//随机产生蓝色
constrain(Rr, 20, 255);
constrain(Gg, 20, 255);
constrain(Bb, 20, 255);
a=Rr/20;
b=Gg/20;
c=Bb/20;
//约束随机产生不小于20,避免商小于1
Serial.print("Rr==");
Serial.print(Rr);
Serial.print("\n");
Serial.print("Gg==");
Serial.print(Gg);
Serial.print("\n");
Serial.print("Bb==");
Serial.print(Bb);
Serial.print("\n");
Serial.print("a==");
Serial.print(a);
Serial.print("\n");
Serial.print("b==");
Serial.print(b);
Serial.print("\n");
Serial.print("c==");
Serial.print(c);
Serial.print("\n");
Serial.print("************************");
Serial.print("\n");
}
d=d+a;
f=f+b;
e=e+c;
Serial.println(d);
Serial.println(f);
Serial.println(e);
if(d>=Rr || d==0 || f>=Gg || e>=Bb)//如果有一个灯颜色的灯加到最亮或者亮度达到最低,将自加值进行正负切换
{
a=-a;
b=-b;
c=-c;
}
analogWrite(led_red, a);
analogWrite(led_green, b);
analogWrite(led_blue, c);
delay(10);
} |