Arduino初学IO控制,流水灯实验是很好的学习对象。分两个进程学习。
一、假流水灯,即基础效果实现
二、真流水灯,即采用PWM模拟真实流水渐变效果
我们设立5盏灯,正极分别连接数字口(Digital Pin)3 5 9 10 11口 ,负极共地(GND)
假流水灯
int LED1 = 3;
int LED2 = 5;
int LED3 = 9;
int LED4 = 10;
int LED5 = 11;
void setup() {
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
}
void loop() {
//状态1
digitalWrite(LED1, 1); //1 代表 HIGH 0 代表 LOW
digitalWrite(LED2, 0);
digitalWrite(LED3, 0);
digitalWrite(LED4, 0);
digitalWrite(LED5, 0);
delay(1000);
//状态2
digitalWrite(LED1, 0);
digitalWrite(LED2, 1);
digitalWrite(LED3, 0);
digitalWrite(LED4, 0);
digitalWrite(LED5, 0);
delay(1000);
//状态3
digitalWrite(LED1, 0);
digitalWrite(LED2, 0);
digitalWrite(LED3, 1);
digitalWrite(LED4, 0);
digitalWrite(LED5, 0);
delay(1000);
//状态4
digitalWrite(LED1, 0);
digitalWrite(LED2, 0);
digitalWrite(LED3, 0);
digitalWrite(LED4, 1);
digitalWrite(LED5, 0);
delay(1000);
//状态5
digitalWrite(LED1, 0);
digitalWrite(LED2, 0);
digitalWrite(LED3, 0);
digitalWrite(LED4, 0);
digitalWrite(LED5, 1);
delay(1000);
}
使用状态控制法去实现,方便理解和修改,每个状态持续一段时间,即使用delay()
。【Arduino程序的运行机制和Processing不太一样,P5是等待一次draw函数执行完才刷新画布,得到结果,而Arduino是即时,也就是执行一条语句后立马能反馈到结果!!】
当然有人用循环语句来搭建,可以,但是对初学者真是不友好。首先循环语句本来就不熟悉,而且还有delay延迟函数
参与,不好理解其运行逻辑的,所以不推荐。
真流水灯
因为要模拟过渡效果,因此简单的数字式读写没法满足要求,使用PWM调节机制能模拟出来。
int LED1 = 3;
int LED2 = 5;
int LED3 = 9;
int LED4 = 10;
int LED5 = 11;
int fadeValue1 = 0; //定义亮度
int fadeValue2 = 60;
int fadeValue3 = 120;
int fadeValue4 = 180;
int fadeValue5 = 240;
int state1 = 0;
int state2 = 0;
int state3 = 0;
int state4 = 0;
int state5 = 0;
void setup() {
}
void loop() {
//更新
//1
if (state1 == 0)
{
fadeValue1 += 1;
}
else if(state1 == 1)
{
fadeValue1 -= 1;
}
if (fadeValue1 > 254)
{
state1 = 1;
}
else if (fadeValue1 < 1)
{
state1 = 0;
}
////////////////2/////////////////////////
if (state2 == 0)
{
fadeValue2 += 1;
}
else if(state2 == 1)
{
fadeValue2 -= 1;
}
if (fadeValue2 > 254)
{
state2 = 1;
}
else if (fadeValue2 < 1)
{
state2 = 0;
}
////////////////3/////////////////////////
if (state3 == 0)
{
fadeValue3 += 1;
}
else if(state3 == 1)
{
fadeValue3 -= 1;
}
if (fadeValue3 > 254)
{
state3 = 1;
}
else if (fadeValue3 < 1)
{
state3 = 0;
}
////////////////4/////////////////////////
if (state4 == 0)
{
fadeValue4 += 1;
}
else if(state4 == 1)
{
fadeValue4 -= 1;
}
if (fadeValue4 > 254)
{
state4 = 1;
}
else if (fadeValue4 < 1)
{
state4 = 0;
}
////////////////5/////////////////////////
if (state5 == 0)
{
fadeValue5 += 1;
}
else if(state5 == 1)
{
fadeValue5 -= 1;
}
if (fadeValue5 > 254)
{
state5 = 1;
}
else if (fadeValue5 < 1)
{
state5 = 0;
}
//执行 相当于 P5绘画
analogWrite(LED1, fadeValue1);
analogWrite(LED2, fadeValue2);
analogWrite(LED3, fadeValue3);
analogWrite(LED4, fadeValue4);
analogWrite(LED5, fadeValue5);
delay(4);
}
沿用P5的更新-绘画
原理,慢慢地刷新亮度值,最后统一执行analogWrite()
。
不过还能简化一些操作,使用sin函数
来更新数值,如:
int LED1 = 3;
int LED2 = 5;
int LED3 = 9;
int LED4 = 10;
int LED5 = 11;
int fadeValue1 = 0;
int fadeValue2 = 60;
int fadeValue3 = 120;
int fadeValue4 = 180;
int fadeValue5 = 240;
//为了方便 偏移 sin函数的相位,设置一个不同的初相
int state1 = 0;
int state2 = 10;
int state3 = 20;
int state4 = 30;
int state5 = 40;
void setup() {
// put your setup code here, to run once:
// analogWrite 函数不用pinMode定义
}
void loop() {
fadeValue1 = abs(cos(millis()/200.0 + state1)) * 250 + 1;
fadeValue2 = abs(cos(millis()/200.0 + state2)) * 250 + 1;
fadeValue3 = abs(cos(millis()/200.0 + state3)) * 250 + 1;
fadeValue4 = abs(cos(millis()/200.0 + state4)) * 250 + 1;
fadeValue5 = abs(cos(millis()/200.0 + state5)) * 250 + 1;
// analogWrite(LED1, fadeValue1);
// analogWrite(LED2, fadeValue2);
// analogWrite(LED3, fadeValue3);
// analogWrite(LED4, fadeValue4);
// analogWrite(LED5, fadeValue5);
analogWrite(LED1, 255-fadeValue1);
analogWrite(LED2, 255-fadeValue2);
analogWrite(LED3, 255-fadeValue3);
analogWrite(LED4, 255-fadeValue4);
analogWrite(LED5, 255-fadeValue5);
delay(4);
}
因为sin函数的数值变化本就带缓动,因此效果很理想。
上述代码只是模型参考,任意数值都能自行修改。其实这里应该贴上实验结果来完善这篇博文,但是么。。。为了保有读者的好奇心,鼓励读者自行实操看效果,这也是学习硬件,学习Arduino另外的乐趣和意义,祝好运 ! @~