Arduino , 如何让arduino在同时满足两个条件的时候做一件事情 5
我想让arduino在两个按键在同时(1/3秒内)按下时让蜂鸣器响。怎么做?intanalogPin5=12;intanalogPin1=4;intval1=0;intv...
我想让arduino在两个按键在同时(1/3秒内)按下时让蜂鸣器响。怎么做?
int analogPin5 = 12;
int analogPin1 = 4
;
int val1 = 0;
int val2 = 0;
void setup()
{
Serial.begin(9600);
// 把连接按钮的引脚作为输入:
pinMode(analogPin5, INPUT);
pinMode(analogPin1, INPUT); // setup serial
}
void loop()
{
analogWrite(1,255);
val1 = digitalRead(analogPin5);
val2 = digitalRead(analogPin1);
Serial.println(val1);
Serial.println();
Serial.println();
Serial.println();
Serial.println(val2);
analogWrite(3,0);
analogWrite(5,0);
tone(9,LOW);
//在1/3秒内同时==1后执行的代码
//下面是分开来的代码
if(val1==1)
{analogWrite(3,255);tone(9,500);delay(1000); }
else if(val1>0)
{analogWrite(3,0);analogWrite(5,0);}
if(val2==1)
{analogWrite(5,255);tone(9,500);delay(1000);}
else if(val2>0)
{analogWrite(3,0);analogWrite(5,0);}
} 展开
int analogPin5 = 12;
int analogPin1 = 4
;
int val1 = 0;
int val2 = 0;
void setup()
{
Serial.begin(9600);
// 把连接按钮的引脚作为输入:
pinMode(analogPin5, INPUT);
pinMode(analogPin1, INPUT); // setup serial
}
void loop()
{
analogWrite(1,255);
val1 = digitalRead(analogPin5);
val2 = digitalRead(analogPin1);
Serial.println(val1);
Serial.println();
Serial.println();
Serial.println();
Serial.println(val2);
analogWrite(3,0);
analogWrite(5,0);
tone(9,LOW);
//在1/3秒内同时==1后执行的代码
//下面是分开来的代码
if(val1==1)
{analogWrite(3,255);tone(9,500);delay(1000); }
else if(val1>0)
{analogWrite(3,0);analogWrite(5,0);}
if(val2==1)
{analogWrite(5,255);tone(9,500);delay(1000);}
else if(val2>0)
{analogWrite(3,0);analogWrite(5,0);}
} 展开
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励10(财富值+成长值)+提问者悬赏5(财富值+成长值)
1个回答
推荐于2017-06-06
展开全部
if ( (val1==1) && (val2==1) )
{analogWrite(3,255);tone(9,500);delay(1000); }
else if(val1>0)
{analogWrite(3,0);analogWrite(5,0);
analogWrite(5,255);tone(9,500);delay(1000);}
else if(val2>0)
{analogWrite(3,0);analogWrite(5,0);}
{analogWrite(3,255);tone(9,500);delay(1000); }
else if(val1>0)
{analogWrite(3,0);analogWrite(5,0);
analogWrite(5,255);tone(9,500);delay(1000);}
else if(val2>0)
{analogWrite(3,0);analogWrite(5,0);}
追问
但是我需要有1/3秒的容忍度,就是如果两个按钮分别在1/3秒内按下执行一个条件
追答
加个时间记录就好了 判断按键时间间隔
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
科博尔
2024-10-31 广告
2024-10-31 广告
QT400-18是一种球墨铸铁材料,QT代表“球铁”,400表示其抗拉强度为400MPa,18则表示延伸率为18%。该材料属于铁素体型球墨铸铁,具有良好的韧性和塑性,能够承受高冲击振动及扭转等动、静载荷,特别在低温工作条件下具有较好的冲击性...
点击进入详情页
本回答由科博尔提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询