如何在时间范围内检查事件?
How to check for an event within a timeframe?
edit 我只想对无缘无故否决这个问题的人说声谢谢,祝你有美好的一天。
--
我有一个 Arduino Uno 并尝试使用敲击来编写锁定机制(根据示例书)。但是我想在检查爆震时插入一个时间限制,所以像这样:
void loop(){
if two knocks are sensed in less than a second
do something
else reset
}
或
void loop(){
if knock is sensed
reset value after 1 second // to eliminate error
else if two knocks are sensed in less than a second
do something
}
延迟在这里不起作用,我想到了可以在 if 循环中添加的 int 变量,但不确定如何准确地实现它。也许是嵌套的 for 循环?
有什么想法吗?
我添加了我的代码,以防有助于了解我在做什么
#include <Servo.h>
Servo myServo;
const int piezo = A4;
// boolean locked = false; //might remove later ?
int numberOfKnocks = 0;
void setup (){
myServo.attach(2);
Serial.begin(9600);
//myServo.write(10);
//delay(1000);
myServo.write(25);
numberOfKnocks = 0;
delay(2000);
Serial.println("done");
}
int knockVal = 0;
void loop(){
knockVal = analogRead(piezo);
Serial.print("Knock value is ");
Serial.println(knockVal);
if(numberOfKnocks < 2 && knockVal > 2){
// if(checkForKnock(knockVal) == true){
numberOfKnocks++;
Serial.print(2-numberOfKnocks);
Serial.println(" number of knocks to go");
// }
}
else if(numberOfKnocks >=2){
myServo.write(10);
delay(1000);
myServo.write(25);
//knockVal=0;
Serial.println("locking");
numberOfKnocks=0;
delay(2000);
}
}
感谢在下面回答我的用户,设法让它工作,代码变为:
int temp=0;
if (knockVal > 2){
elapsedTime = millis() - timestamp;
Serial.print( "Time since the last knock " );
Serial.print( elapsedTime );
Serial.println( " msec" );
timestamp = millis();
delay(200);
temp=1;
}
if (elapsedTime < 300 && temp==1){
Serial.println( "SUCCESS ");
myServo.write(10);
delay(1000);
myServo.write(25);
//knockVal=0;
Serial.println("locking");
numberOfKnocks=0;
delay(2000);
temp=0;
}
3 敲代码完成!
#include <Servo.h>
Servo myServo;
const int piezo = A4;
//int numberOfKnocks = 0;
void setup (){
myServo.attach(2);
Serial.begin(9600);
myServo.write(25);
// numberOfKnocks = 0;
delay(1000);
Serial.println("Initilization Complete");
}
unsigned long timestamp = 0; // FOR TIMER
unsigned long timestamp2 = 0;
int knockVal = 0;
unsigned long elapsedTime;
int elapsedTime2;
int temp = 0;
void loop(){
knockVal = analogRead(piezo);
// Serial.print("Knock value is ");
// Serial.println(knockVal);
if (knockVal > 2){
elapsedTime = millis() - timestamp;
Serial.print( "Time since the last knock " );
Serial.print( elapsedTime );
Serial.println( " ms" );
elapsedTime2 = (millis() - timestamp2);
timestamp = millis();
timestamp2= (millis() - elapsedTime) ;
delay(200);
temp=1;
// Serial.print( "elapsedTime2 " );
// Serial.print( elapsedTime2 );
// Serial.println( " ms" );
}
if (elapsedTime2 < 500 && temp==1){
Serial.println("Toggling Switch");
myServo.write(10);
delay(1000);
myServo.write(25);
//numberOfKnocks=0;
delay(500);
Serial.println( "SUCCESS ");
temp=0;
}
}
// if(numberOfKnocks < 2 && knockVal > 2){
// if(checkForKnock(knockVal) == true){
// numberOfKnocks++;
// Serial.print(2-numberOfKnocks);
// Serial.println(" number of knocks to go");
// }
// }
// else if(numberOfKnocks >=2){
// myServo.write(10);
// delay(1000);
// myServo.write(25);
// //knockVal=0;
// Serial.println("locking");
// numberOfKnocks=0;
// delay(2000);
// }
//}
//boolean checkForKnock(int value){ //function
// if(value >= 1){
// Serial.print("Check for Knock value is ");
// Serial.println(value);
// return true;
// }
// if(value <1){
// return false;
// }
//}
下面的代码展示了如何使用millis()
函数来确定两个事件之间的时间。每次压电 returns 一个大于 2 的值时,显示经过的时间,并更新时间戳。
unsigned long timestamp = 0;
void loop()
{
int piezoValue;
unsigned long elapsedTime;
piezoValue = analogRead(piezo);
if ( piezoValue > 2 )
{
// compute the time (in milliseconds) since the last knock
elapsedTime = millis() - timestamp;
Serial.print( "Time since the last knock " );
Serial.print( elapsedTime );
Serial.println( " msec" );
// store the current time
timestamp = millis();
}
}
edit 我只想对无缘无故否决这个问题的人说声谢谢,祝你有美好的一天。
--
我有一个 Arduino Uno 并尝试使用敲击来编写锁定机制(根据示例书)。但是我想在检查爆震时插入一个时间限制,所以像这样:
void loop(){
if two knocks are sensed in less than a second
do something
else reset
}
或
void loop(){
if knock is sensed
reset value after 1 second // to eliminate error
else if two knocks are sensed in less than a second
do something
}
延迟在这里不起作用,我想到了可以在 if 循环中添加的 int 变量,但不确定如何准确地实现它。也许是嵌套的 for 循环?
有什么想法吗?
我添加了我的代码,以防有助于了解我在做什么
#include <Servo.h>
Servo myServo;
const int piezo = A4;
// boolean locked = false; //might remove later ?
int numberOfKnocks = 0;
void setup (){
myServo.attach(2);
Serial.begin(9600);
//myServo.write(10);
//delay(1000);
myServo.write(25);
numberOfKnocks = 0;
delay(2000);
Serial.println("done");
}
int knockVal = 0;
void loop(){
knockVal = analogRead(piezo);
Serial.print("Knock value is ");
Serial.println(knockVal);
if(numberOfKnocks < 2 && knockVal > 2){
// if(checkForKnock(knockVal) == true){
numberOfKnocks++;
Serial.print(2-numberOfKnocks);
Serial.println(" number of knocks to go");
// }
}
else if(numberOfKnocks >=2){
myServo.write(10);
delay(1000);
myServo.write(25);
//knockVal=0;
Serial.println("locking");
numberOfKnocks=0;
delay(2000);
}
}
感谢在下面回答我的用户,设法让它工作,代码变为:
int temp=0;
if (knockVal > 2){
elapsedTime = millis() - timestamp;
Serial.print( "Time since the last knock " );
Serial.print( elapsedTime );
Serial.println( " msec" );
timestamp = millis();
delay(200);
temp=1;
}
if (elapsedTime < 300 && temp==1){
Serial.println( "SUCCESS ");
myServo.write(10);
delay(1000);
myServo.write(25);
//knockVal=0;
Serial.println("locking");
numberOfKnocks=0;
delay(2000);
temp=0;
}
3 敲代码完成!
#include <Servo.h>
Servo myServo;
const int piezo = A4;
//int numberOfKnocks = 0;
void setup (){
myServo.attach(2);
Serial.begin(9600);
myServo.write(25);
// numberOfKnocks = 0;
delay(1000);
Serial.println("Initilization Complete");
}
unsigned long timestamp = 0; // FOR TIMER
unsigned long timestamp2 = 0;
int knockVal = 0;
unsigned long elapsedTime;
int elapsedTime2;
int temp = 0;
void loop(){
knockVal = analogRead(piezo);
// Serial.print("Knock value is ");
// Serial.println(knockVal);
if (knockVal > 2){
elapsedTime = millis() - timestamp;
Serial.print( "Time since the last knock " );
Serial.print( elapsedTime );
Serial.println( " ms" );
elapsedTime2 = (millis() - timestamp2);
timestamp = millis();
timestamp2= (millis() - elapsedTime) ;
delay(200);
temp=1;
// Serial.print( "elapsedTime2 " );
// Serial.print( elapsedTime2 );
// Serial.println( " ms" );
}
if (elapsedTime2 < 500 && temp==1){
Serial.println("Toggling Switch");
myServo.write(10);
delay(1000);
myServo.write(25);
//numberOfKnocks=0;
delay(500);
Serial.println( "SUCCESS ");
temp=0;
}
}
// if(numberOfKnocks < 2 && knockVal > 2){
// if(checkForKnock(knockVal) == true){
// numberOfKnocks++;
// Serial.print(2-numberOfKnocks);
// Serial.println(" number of knocks to go");
// }
// }
// else if(numberOfKnocks >=2){
// myServo.write(10);
// delay(1000);
// myServo.write(25);
// //knockVal=0;
// Serial.println("locking");
// numberOfKnocks=0;
// delay(2000);
// }
//}
//boolean checkForKnock(int value){ //function
// if(value >= 1){
// Serial.print("Check for Knock value is ");
// Serial.println(value);
// return true;
// }
// if(value <1){
// return false;
// }
//}
下面的代码展示了如何使用millis()
函数来确定两个事件之间的时间。每次压电 returns 一个大于 2 的值时,显示经过的时间,并更新时间戳。
unsigned long timestamp = 0;
void loop()
{
int piezoValue;
unsigned long elapsedTime;
piezoValue = analogRead(piezo);
if ( piezoValue > 2 )
{
// compute the time (in milliseconds) since the last knock
elapsedTime = millis() - timestamp;
Serial.print( "Time since the last knock " );
Serial.print( elapsedTime );
Serial.println( " msec" );
// store the current time
timestamp = millis();
}
}