/*内容:以按键单击(表示为0),⻓长击(表示为1) 为输⼊入,统计连续1(包括单个1)出现的状态总共的次 数。
要求 必须包含fsm,中断,不不可以使⽤用轮询!!! 每一次按键,led亮一次 需要进⾏行行消抖*/
int last_state=0;
int current_state=0;
int count=0;
int ledPin=13;
int buttonPin=2;
unsigned long duration;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode( ledPin , OUTPUT);
pinMode( buttonPin , INPUT);
attachInterrupt(0, counter, CHANGE);
}
void loop() {
// put your main code here, to run repeatedly:
switch(current_state){
case 1:
if(last_state==0){
count++;
Serial.print("count:");
Serial.println(count);
}
last_state=current_state;
break;
case 0:
//digitalWrite( ledPin , LOW );
last_state=current_state;
break;
}
}
void counter(){
if (!( digitalRead( buttonPin) )){
delay( 50 );
if (!( digitalRead( buttonPin) )){
digitalWrite( ledPin , HIGH );
duration=pulseIn(buttonPin,LOW,60000000);
if(duration/1000000.0>1){
current_state=1;
//Serial.begin(9600);
Serial.print("time:");
Serial.println("long");
}else{
current_state=0;
//Serial.begin(9600);
Serial.print("time:");
Serial.println("short");
}
digitalWrite( ledPin , LOW );
}
}
}
嵌入式arduino计数小程序
猜你喜欢
转载自blog.csdn.net/The_Legend_of_1900/article/details/86418669
今日推荐
周排行