所以我重写了我的代码。当我按下连接在引脚 2 上的按钮时,它使引脚 13 变为高电平,并通过收发器向接收器发送信号(收发器的类型和接收器无关)。我将一根电线从接收器(引脚 13 使其变为高电平)连接到 arduino 上的引脚 7。我还将一个 LED 连接到引脚 8 以指示引脚 7 何时为高电平。
我的主要重点是计算从按下按钮到 Arduino 上的引脚 7 变为高电平所花费的时间。我正在使用 Arduino Leonardo(也是不相关的信息)。
这是我的代码:
int buttonState;
int buttonPin = 2;
int LbuttonState; // last button state
int pin13 = 13;
int pin7state;
int pin7 = 7;
int Lpin7state; // last pin 7 state
int pin8 = 8;
long startTimeKeeper;
long endTimeKeeper;
long elapsedTime;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
pinMode(pin13, OUTPUT);
pinMode(pin7, INPUT);
pinMode(pin8, OUTPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if(buttonState == HIGH && LbuttonState == LOW) {
startTime(); // start the time
digitalWrite(pin13, HIGH);
LbuttonState = buttonState;
} else if(buttonState == HIGH && LbuttonState == LOW) {
digitalWrite(pin13, LOW);
} else if(buttonState == LOW && LbuttonState == HIGH) {
digitalWrite(pin13, LOW);
LbuttonState = buttonState;
} else//(buttonState == LOW && LbuttonState == LOW)
digitalWrite(pin13, LOW);
pin7state = digitalRead(pin7);
if(pin7state == HIGH && Lpin7state == LOW) {
stopTime(); // stop the time
digitalWrite(pin8, HIGH);
Lpin7state = pin7state;
} else if(pin7state == HIGH && Lpin7state == HIGH) {
digitalWrite(pin8, HIGH);
} else if(pin7state == LOW && Lpin7state == HIGH) {
digitalWrite(pin8, LOW);
Lpin7state = pin7state;
} else//(pin7state == LOW && Lpin7state == LOW)
digitalWrite(pin8, LOW);
}
void startTime() {
startTimeKeeper = millis();
}
void stopTime() {
endTimeKeeper = millis();`enter code here`
elapsedTime = endTimeKeeper - startTimeKeeper;
Serial.print(elapsedTime);
}