/*-----------------------------------------------------------------------------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------变量声明-----------------------------------------------------------------------*/
/******************************引脚声明******************************/
const int Button_Pin =2; // 开关按钮的信号脚接arduino的D2
const int Button_Pin1 =3;
/******************************常量声明******************************/
#define LOW_OR_HIGH 0 // 如果是上拉电阻模式,则输入0,低电平有效
/******************************变量声明******************************/
class ChanState {
private:
unsigned long State_Duration; // 保存状态持续时间
bool State; // 判断是进入还是没进入状态
bool Last_boolValue; // 储存上一次输入的布尔值
public:
bool Parity_State;
int Enter_Time = 0;
/*
函数:获取进入状态的时间(Input_State为1的持续时间)
参数:
- Input_State:代表进入某一种状态的布尔值
- Switch:1记录进入状态后的持续时间;0记录跳出状态后的持续时间
返回值:
- 进入状态的时间
*/
unsigned long stateDuration(bool Input_State, bool Switch = 1) {
if (Input_State != State) { // 刚开始Input_State为1 ,State为0 就会计时;
State = !State;
State_Duration = millis();
}
return (State == Switch) ? (millis() - State_Duration) : 0;
}
/*
函数:判断是否刚进入某一状态
参数:
- Input_boolValue:代表进入某一种状态的布尔值
返回值:
- 刚进入一瞬间,返回一个字符‘1’
- 跳出的一瞬间,返回一个字符‘0’
- 其他情况返回 空字符
*/
char onceEnter(bool Input_boolValue){
char a = ' ';
if(Input_boolValue==1 && Last_boolValue==0){
a='1';
Parity_State = !Parity_State;
Enter_Time++;
}
if(Input_boolValue==0 && Last_boolValue==1){
a='0';
}
Last_boolValue = Input_boolValue;
return a;
}
/*
函数:将数值和阈值的比较转换为布尔值
参数:
- Value:输入的数值
- Thresholds:判断的阈值
返回值:
- 如果输入的数值大于阈值,返回1
- 如果输入的数值小于阈值,返回0
*/
bool valueToState(float Value, float Thresholds){
if(Value >= Thresholds) {
return 1;
}else{
return 0;
}
}
};
ChanState chanstate1;
ChanState chanstate2;
/*-----------------------------------------------------------------------------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------setup()初始化配置------------------------------------------------------------------*/
void setup() {
/******************************启动串口通讯******************************/
Serial.begin(9600); // 设置串口通讯率为9600
/******************************设置引脚模式******************************/
pinMode(Button_Pin, INPUT_PULLUP);
pinMode(Button_Pin1, INPUT_PULLUP);
/****************************中断:控制开关按键**************************/
attachInterrupt(digitalPinToInterrupt(Button_Pin), doInter, CHANGE);
}
/*-----------------------------------------------------------------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------loop()基础循环体------------------------------------------------------------------*/
void loop() {
while(chanstate1.Parity_State){
Serial.println(1);
}
delay(50);
}
void doInter() {
chanstate1.onceEnter(debounce(Button_Pin));
}
boolean debounce(int pin) {
bool state;
bool previousState = digitalRead(pin);
for (int counter = 0; counter < 50; counter++)
//如果在10ms内状态不变,则确定状态
{
delay(1);
state = digitalRead(pin);
if (state != previousState) //如果前后不一致
{
counter = 0; //for循环重新开始
previousState = state;
}
}
return (LOW_OR_HIGH) ? state : !state;
}