arduino 按钮读取_arduino Uno3中的按钮开关问题

// 按钮开关:  GND ---按钮--- Pin 1

///

int bounce = 88; // bounce time for 抖动判断; you can change it

int btn = 1;  // you can use pin 1, pin 0

int state = 0; // 0, 1 两个状态

///

int bt00 = 0; // pin0 if you hava a button on it

int state00 = 0; // for Button on pin0 if have

void setup( ) {

pinMode (btn, INPUT_PULLUP); // pull High

// ...

//...

} // setup(

void loop( ) {

//...

if( click( ) ) {   // 按一下 Button on Pin 1

state = 1 - state;  // 切换状态

}

// check state to do some thing

// if(state == 1) ...;

// else ...;

//...

} // loop(

//

int highLow = HIGH;  //没按下是 HIGH (因INPUT_PULLUP)

int highLow00 = HIGH;  // for example a Button on pin0

unsigned long lastHit01 = 0;

unsigned long lastHit00 = 0;  // for Button on pin 0

bool clickCheck(int b, int& status, unsigned long& lastTime); // declare

bool click( ) {

return clickCheck(btn, highLow, lastHit01);

}

bool click00( ) {

return clickCheck(bt00, highLow00, lastHit00);

}

//

bool clickCheck(int bt, int& xxStat, unsigned long& lastHit) {

int a = digitalRead(bt);

if(a == xxStat) return false;  // no change

unsigned long m = millis( );

if(a == LOW) {   // 压下 !

if(m - lastHit  < bounce) return false; // ignore

lastHit = m;  // record the time

xxStat = a; // LOW

return true;   // Yes, you hit it

}

// released button  ( Low --> HIGH)

xxStat = a; // HIGH

return false;

}

/// End of the Program

// by tsaiwn@ydu.edu.tw 育达科大 多媒体与游戏系; 苗栗; 台湾

//==========================================================

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值