segichen 发表于 2013-7-28 01:02
响了没?哈哈
void setup ()
{
pinMode(3,INPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
}
void loop()
{
if(digitalRead(3)==LOW)
{
for(int t=0;t<255;t++)
{
analogWrite(5,t);
delay(10);
digitalWrite(6,LOW);
{
digitalWrite(1,HIGH);
delay(1000);
}
for(int t=255;t<255;t--)
{
analogWrite(5,t);
delay(10);
}
}
}
else
{
digitalWrite(5,LOW) ;
{
digitalWrite(6,HIGH);
delay(1000);
}
digitalWrite(1,LOW);
delay(1000);
}
}
爷灬淡定 发表于 2013-8-12 18:55
//这是我写的一个警报器,可是为什么如果 pinMode(1)就是蜂鸣器只有在一开始才亮
CCFIVE 发表于 2013-8-12 20:24
请问你是如何接线的? D3,D5,D6是分别接什么?
爷灬淡定 发表于 2013-8-13 10:33
谢谢啦,一开始的pinMode确实粗心
不过后面的 t >0确实没考虑
不过我昨晚写了个警报器的代码,已经觉得 ...
CCFIVE 发表于 2013-8-13 13:07
对应改改你接线,代码还可以优化下,自己尝试优化啦
void setup ()
{
pinMode(3,INPUT);//Check
pinMode(5,OUTPUT);//Warn Led
pinMode(6,OUTPUT);//Safe Led
pinMode(1,OUTPUT);//Bee
}
void loop()
{
if(digitalRead(3)==LOW)
{
digitalWrite(6,LOW);
digitalWrite(5,HIGH);
delay(150);
digitalWrite(1,HIGH);
delay(150);
digitalWrite(5,LOW);
delay(200);
digitalWrite(1,LOW);
delay(200);
digitalWrite(5,HIGH);
delay(150);
digitalWrite(1,HIGH);
delay(150);
digitalWrite(5,LOW);
delay(200);
digitalWrite(1,LOW);
delay(200);
digitalWrite(5,HIGH);
delay(150);
digitalWrite(1,HIGH);
delay(150);
digitalWrite(5,LOW);
delay(200);
digitalWrite(1,LOW);
delay(200);
digitalWrite(5,HIGH);
delay(150);
digitalWrite(1,HIGH);
delay(150);
digitalWrite(5,LOW);
delay(200);
digitalWrite(1,LOW);
delay(200);
digitalWrite(5,HIGH);
delay(150);
digitalWrite(1,HIGH);
delay(150);
digitalWrite(5,LOW);
delay(200);
digitalWrite(1,LOW);
delay(200);
}
else
{
digitalWrite(5,LOW) ;
digitalWrite(6,HIGH);
digitalWrite(1,LOW);
}
}
欢迎光临 创客联盟网 (https://immaker.org/) | Powered by Discuz! X3.3 |