编辑我只想感谢你无缘无故地投票支持这个问题的人,祝你有个美好的一天。
-
我有一个Arduino Uno并尝试使用敲击编程锁定机制(根据示例书)。但是我想在检查敲击时插入一个时间限制,所以像这样:
void loop(){
if two knocks are sensed in less than a second
do something
else reset
}
OR
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;
// }
//}
答案 0 :(得分:2)
以下代码显示如何使用millis()
函数确定两个事件之间的时间。每次压电返回大于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();
}
}