Arduino 计算频率 - 我在这里做错了什么?

标签 arduino

我是电子产品和 Arduino 的新手 - 所以最好的方法就是玩弄它,对吧?

我已经开始了一个利用 LDR(光密度电阻器)的小项目,并想用它来计算光束被阻挡或关闭的频率。

出于调试目的,我设置了一个以定义频率(5 Hz 等)闪烁的小 LED,并使用 LCD 显示输出。

Display

我的右上角有问题...似乎它执行错误。它的意图是它应该显示注册的频率,但是在调试时我已将其设置为以 5 秒(5,000 毫秒)的间隔显示计数。但无论我设置什么频率,它似乎都是最大值(当我让它显示正确的数字 [5 秒 x 5 Hz = 25] 时,我将除以时间间隔并以 Hz 为单位得到结果)。它还显示 24.0 为 9 Hz 等。

我也有这个:YouTube video

...但是一开始的一些摸索导致 LED 移动了一点,所以它计算错了。但最终它“有效”..但 24.0 保持不变

这是我的代码:

#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 8, 9, 10, 11 , 12);
int booBlocked = 0;
int counter = 0;
int checkValue = counter + 1;

int ledPin = 3;                // LED connected to digital pin 3
int value = LOW;                // previous value of the LED
long previousMillis = 0;        // will store last time LED was updated
long freqency = 5; // Hz (1/sec)
long thousand = 1000;
long interval = thousand / freqency; // milliseconds
//long interval = 59;           // interval at which to blink (milliseconds)

int tValue = 0; // Threshold value used for counting (are calibrated in the beginning)
long pMillis = 0;
long inter = 5000;
int pCount = 0;
float freq = 0;  // Calculated blink frequency...

void setup() { 
  lcd.begin(16, 2);
  lcd.setCursor(0,1);  lcd.print(interval);
  lcd.setCursor(4,1);  lcd.print("ms");

  pinMode(ledPin, OUTPUT);      // sets the digital pin as output 

  lcd.setCursor(0,0);  lcd.print(freqency);
  lcd.setCursor(4,0);  lcd.print("Hz");
}

void loop() {
  // Print LDR sensor value to the display  
  int sensorValue = analogRead(A0);
  lcd.setCursor(7,1);
  lcd.print(sensorValue);
  delay(100);

  if (millis() > 5000){
    doCount(sensorValue);
    updateFreq();
    lcd.setCursor(7+5,0);
    lcd.print(freq);
  } else {
    setThresholdValue(sensorValue);
    lcd.setCursor(7+5,1);
    lcd.print(tValue);
  }

 // LED BLINK
  if (millis() - previousMillis > interval) {
    previousMillis = millis();   // remember the last time we blinked the LED
    // if the LED is off turn it on and vice-versa.
    if (value == LOW)
      value = HIGH;
    else
      value = LOW;
    digitalWrite(ledPin, value);
  }    
}

void updateFreq(){
 long now = millis();
 long t = now - pMillis;
 if (t >= 10000) {
   freq = (float) (counter - pCount);
   //freq = ((float) (counter - pCount)) / (float) 10.0;
   pMillis = now;   // remember the last time we blinked the LED
   pCount = counter;
  } 
}

void setThresholdValue(int sensorValue){
  if (sensorValue > int(tValue/0.90)){
    tValue = int (sensorValue*0.90);
  }
}

void doCount(int sensorValue){
    // Count stuff
  if (sensorValue < tValue){
    booBlocked = 1;
    //lcd.setCursor(0,0);
    //lcd.print("Blocked");
  } else {
    booBlocked = 0;
    //lcd.setCursor(0,0);
    //lcd.print("       ");    
  }

  if (booBlocked == 1) {
   if (counter != checkValue){
    counter = counter + 1;
    lcd.setCursor(7,0);
    lcd.print(counter);
   }   
  } else {
   if (counter == checkValue){
    checkValue = checkValue + 1;
     }
   }
}

更新

更“干净”的代码(请参阅我自己的答案)
#include <LiquidCrystal.h>
// Initiate the LCD display
LiquidCrystal lcd(7, 8, 9, 10, 11 , 12); // see setup at http://lassenorfeldt.weebly.com/1/post/2013/02/ardunio-lcd.html
long updateInterval = 150;                // ms
long updateTime = 0;

// Declare the pins
int ledPin = 3;                       // LED connected to digital pin 3

// LED setup
int value = LOW;                      // previous value of the LED
long previousMillis = 0;              // will store last time LED was updated 
long freqency = 16;                   // Hz (1/sec)
long thousand = 1000;
long blinkInterval = thousand / freqency;  // milliseconds

//// LDR counter variables ////
// Counting vars
static int counter = 0;
int booBlocked = 0;
int checkValue = counter + 1;
// Calibration vars
long onBootCalibrationTime = 5000;     // time [time] to use for calibration when the system is booted
static int threshold = 0;              // Value used for counting (calibrated in the beginning)
float cutValue = 0.90;                 // Procent value used to allow jitting in the max signal without counting.

// Frequency vars
float freq = 0;                        // Calculated blink frequency...
long frequencyInterval = 5000;        // time [ms] 
long pMillis = 0;
int pCount = 0;



void setup() {
  // Setup the pins
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output 

  // display static values
  lcd.begin(16, 2);
  lcd.setCursor(0,0);  lcd.print(freqency);
  lcd.setCursor(4,0);  lcd.print("Hz");
  lcd.setCursor(0,1);  lcd.print(blinkInterval);
  lcd.setCursor(4,1);  lcd.print("ms");

  // Setup that allows loggin
  Serial.begin(9600);  // Allows to get a readout from Putty (windows 7)
}

void loop() {  
  long time = millis();
  int sensorValue = analogRead(A0);

  // Blink the LED
  blinkLED(time);

  // Calibrate or Count (AND calculate the frequency) via the LDR
  if (time < onBootCalibrationTime){
    setThresholdValue(sensorValue);
  } else {    
    doCount(sensorValue);
    updateFreq(time);
  }


  // Update the LCD
  if (time > updateTime){
    updateTime += updateInterval;  // set the next time to update the LCD

   // Display the sensor value
    lcd.setCursor(7,1);  lcd.print(sensorValue);
   // Display the threshold value used to determined if blocked or not
    lcd.setCursor(7+5,1);  lcd.print(threshold);
   // Display the count
    lcd.setCursor(7,0);
    lcd.print(counter);
   // Display the calculated frequency
    lcd.setCursor(7+5,0);  lcd.print(freq);   
  }  
}

void blinkLED(long t){
  if (t - previousMillis > blinkInterval) {
    previousMillis = t;   // remember the last time we blinked the LED
    // if the LED is off turn it on and vice-versa.
    if (value == LOW)
      value = HIGH;
    else
      value = LOW;
    digitalWrite(ledPin, value);
  }
}

void setThresholdValue(int sValue){
  if (sValue > int(threshold/cutValue)){
    threshold = int (sValue*cutValue);
  }
}

void doCount(int sValue){
  if (sValue < threshold){
    booBlocked = 1;
  } else {
    booBlocked = 0;  
  }

  if (booBlocked == 1) {
   if (counter != checkValue){
    counter = counter + 1;
   }   
  } else {
   if (counter == checkValue){
    checkValue = checkValue + 1;
     }
   }
}

void updateFreq(long t){
 long inter = t - pMillis;
 if (inter >= frequencyInterval) {
   freq = (counter - pCount) / (float) (inter/1000);
   pMillis = t;           // remember the last time we blinked the LED
   pCount = counter;
  } 
}

这段代码不能解决我的问题,但更容易阅读。

最佳答案

您的计划的问题在于,光密度电阻器将吸收周围的所有环境光,因此对环境完全敏感。

还有其他项目希望吗?这似乎是一种工程学习体验,而不是编码体验。

你有没有想过汽车项目?就我个人而言,我更喜欢家庭自动化,但汽车项目几乎立即得到返回。

关于Arduino 计算频率 - 我在这里做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14789574/

相关文章:

c++ - Arduino 从 'char' 到 'char*' 的无效转换

c++ - 为什么这个按位运算是 "narrowing conversion from ' int' 到 'byte'“? - Arduino

c++ - Arduino - 在 sketch 中使用的 C 库中包含 SD.h

python - 一对一串行通信

java - 以编程方式扫描蓝牙设备在 Android 6+ 中找不到任何内容

c - 更改多维字符数组的值时“崩溃”

android - 如何知道 ESP8266 是否在 Android 应用程序的 Firebase 中连接/断开连接?

c - 为 Pro Micro 编写 Windows 驱动程序

c - 在C中迭代二维数组

java - 如何将 GPS 设备与 Android 应用程序连接