极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10456|回复: 4

求解两个按键控制两盏灯的问题

[复制链接]
发表于 2015-4-26 09:33:27 | 显示全部楼层 |阅读模式

我想用两个按键控制两盏灯 能够实现任意的点亮熄灭灯 现在的情况是只能按照顺序点亮和熄灭灯 跪求

int redPin=11;
int yellowPin=10;
int redbuttonPin=7;
int yellowbuttonPin=6;
boolean redState=false;
boolean yellowState=false;
void setup()
{
pinMode(redPin,OUTPUT);
pinMode(yellowPin,OUTPUT);
pinMode(redbuttonPin,INPUT_PULLUP);
pinMode(yellowbuttonPin,INPUT_PULLUP);
}
void loop()
{
  while(digitalRead(redbuttonPin)==LOW){}
  if(redState==true)
  {digitalWrite(redPin,LOW);
  redState=!redState;
  }
  else
  {
    digitalWrite(redPin,HIGH);
    redState=!redState;
  }
  delay(500);
   while(digitalRead(yellowbuttonPin)==LOW){}
  if(yellowState==true)
  {digitalWrite(yellowPin,LOW);
  yellowState=!yellowState;
  }
  else
  {
    digitalWrite(yellowPin,HIGH);
    yellowState=!yellowState;
  }
  delay(500);
}
回复

使用道具 举报

发表于 2015-4-26 09:44:44 | 显示全部楼层
while(digitalRead(redbuttonPin)==LOW){} 意思是如果不是 Low 那么死循环
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-4-26 10:06:46 | 显示全部楼层
zoologist 发表于 2015-4-26 09:44
while(digitalRead(redbuttonPin)==LOW){} 意思是如果不是 Low 那么死循环

谢谢大神的指点啊
回复 支持 反对

使用道具 举报

发表于 2015-4-26 19:16:25 | 显示全部楼层
蓝色骨骨头 发表于 2015-4-26 10:06
谢谢大神的指点啊

奇怪 ..
你怎一个问题重复贴文来问 ?
还有, 因为你用 PULLUP 内部上拉电阻:
   pinMode(redbuttonPin,INPUT_PULLUP);
所以没有按是 HIGH
按钮按下会变 LOW
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-4-29 11:04:37 | 显示全部楼层
tsaiwn 发表于 2015-4-26 19:16
奇怪 ..
你怎一个问题重复贴文来问 ?
还有, 因为你用 PULLUP 内部上拉电阻:

谢谢指点哈
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-17 08:51 , Processed in 0.037716 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表