c - 如何检查某个时间范围内的事件?

标签 c loops time arduino

编辑我只想对无缘无故否决此问题的人表示感谢,祝你有美好的一天。

--

我有一个 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() 函数确定两个事件之间的时间。每次压电返回大于 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();
    }
}

关于c - 如何检查某个时间范围内的事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28448787/

相关文章:

java - SwingWorker(不工作),循环在第一次迭代后结束

java - 如何让我的程序将每个字符打印为 "Character #1: (character) , Character #2: (character), etc"?

Python 2.x - Windows 上毫秒级别的 sleep 调用

c++ - int 的大小是否取决于编译器和/或处理器?

将文件指针传递给方法时出现 C 段错误

java - 为什么这会导致无限循环?

Python 脚本执行时间在多次并行执行时增加

swift - 如何找到存储数据的最近日期

创建循环链接列表编译器停止工作

c - 不使用内置函数的字符串搜索 C 程序