Arduino计算频率-我做错了什么?
说到电子和Arduino,我是个新手,所以最好的方法就是玩它,对吗 我已经开始了一个利用和LDR(光密度电阻器)的小项目,并想用它来计算光束被阻挡或关闭的频率 为了调试的目的,我设置了一个小LED,它以规定的频率(5赫兹等)闪烁,并使用LCD显示输出 我的右上角有问题。。。它似乎表现得不好。它的目的是显示注册的频率,但在调试时,我已将其设置为以5秒(5000毫秒)的间隔显示计数数。但无论我设置什么频率,24都是最大值(当我让它显示正确的数字[5秒x 5赫兹=25]时,我将除以时间间隔,得到以赫兹为单位的结果)。它还显示9 Hz等的24.0 我还有这个: …但开始时的一些失误导致LED移动了一点,因此计数错误。但最终它“起作用了”。。但24.0一直保持不变 这是我的代码:Arduino计算频率-我做错了什么?,arduino,Arduino,说到电子和Arduino,我是个新手,所以最好的方法就是玩它,对吗 我已经开始了一个利用和LDR(光密度电阻器)的小项目,并想用它来计算光束被阻挡或关闭的频率 为了调试的目的,我设置了一个小LED,它以规定的频率(5赫兹等)闪烁,并使用LCD显示输出 我的右上角有问题。。。它似乎表现得不好。它的目的是显示注册的频率,但在调试时,我已将其设置为以5秒(5000毫秒)的间隔显示计数数。但无论我设置什么频率,24都是最大值(当我让它显示正确的数字[5秒x 5赫兹=25]时,我将除以时间间隔,得到以赫
#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 <FreqPeriodCounter.h>
#include <LiquidCrystal.h>
// FrequencyCounter vars
const byte counterPin = 3; // Pin connected to the LDR
const byte counterInterrupt = 1; // = pin 3
FreqPeriodCounter counter(counterPin, micros, 0);
// LCD vars
LiquidCrystal lcd(7, 8, 9, 10, 11 , 12); // see setup at http://lassenorfeldt.weebly.com/1/post/2013/02/ardunio-lcd.html
long updateInterval = 200; // ms
long updateTime = 0;
// LED vars
int ledPin = 5; // LED connected to digital pin 3
int value = LOW; // previous value of the LED
float previousMillis = 0; // will store last time LED was updated
static float freqency; // Hz (1/sec)
static float pfreqency;
static float blinkInterval; // milliseconds
boolean logging = true; // Logging by sending to serial
// Use potentiometer to control LED frequency
int potPin = 5; // select the input pin for the potentiometer
int val = 0; // variable to store the value coming from the sensor
void setup(void){
// Setup the pins
pinMode(ledPin, OUTPUT); // sets the digital pin as output
val = analogRead(potPin);
freqency = map(val, 0, 1023, 0, 25); // Hz (1/sec)
pfreqency = freqency;
blinkInterval = 1000 / (freqency*2); // milliseconds
// LCD display static values
lcd.begin(16, 2);
lcd.setCursor(0,0); lcd.print(freqency);
lcd.setCursor(4,0); lcd.print("Hz");
lcd.setCursor(14,0); lcd.print("Hz");
lcd.setCursor(0,1); lcd.print(blinkInterval);
lcd.setCursor(4,1); lcd.print("ms");
//
attachInterrupt(counterInterrupt, counterISR, CHANGE);
// Logging
if (logging) {Serial.begin(9600);}
}
void loop(void){
// Loop vars
float time = (float) millis();
float freq = (float) counter.hertz(10)/10.0;
// Blink the LED
blinkLED(time);
if (logging) {
if(counter.ready()) Serial.println(counter.hertz(100));
}
// Update the LCD
if (time > updateTime){
updateTime += updateInterval; // set the next time to update the LCD
lcdNicePrint(7+3, 0, freq); lcd.setCursor(14,0); lcd.print("Hz");
val = analogRead(potPin);
freqency = map(val, 0, 1023, 1, 30);
if (freqency != pfreqency){
pfreqency = freqency;
blinkInterval = 1000 / (freqency*2); // milliseconds
lcdNicePrint(0,0, freqency); lcd.setCursor(4,0); lcd.print("Hz");
lcd.setCursor(0,1); lcd.print(blinkInterval);
lcd.setCursor(4,1); lcd.print("ms");
}
}
}
void lcdNicePrint(int column, int row, float value){
lcd.setCursor(column, row); lcd.print("00");
if (value < 10) {lcd.setCursor(column+1, row); lcd.print(value);}
else {lcd.setCursor(column, row); lcd.print(value);}
}
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 counterISR()
{ counter.poll();
}
#包括
液晶液晶显示器(7,8,9,10,11,12);
int=0;
int计数器=0;
int checkValue=计数器+1;
int-ledPin=3;//LED连接到数字管脚3
int值=低;//LED的先前值
long-previousMillis=0;//将存储上次更新LED的时间
长频率=5;//赫兹(1/秒)
长千=1000;
长间隔=千次/频率;//毫秒
//长间隔=59;//闪烁的间隔(毫秒)
int tValue=0;//用于计数的阈值(开始时已校准)
长pMillis=0;
长间隔=5000;
int pCount=0;
浮动频率=0;//计算的闪烁频率。。。
无效设置(){
lcd.begin(16,2);
lcd.setCursor(0,1);lcd.print(间隔);
lcd.setCursor(4,1);lcd.print(“ms”);
pinMode(LED引脚,输出);//将数字引脚设置为输出
lcd.setCursor(0,0);lcd.print(频率);
lcd.setCursor(4,0);lcd.print(“Hz”);
}
void循环(){
//将LDR传感器值打印到显示屏上
int传感器值=模拟读数(A0);
lcd.setCursor(7,1);
lcd.打印(传感器值);
延迟(100);
如果(毫秒()>5000){
doCount(传感器值);
updateFreq();
lcd.setCursor(7+5,0);
lcd.打印(freq);
}否则{
设置阈值(传感器值);
lcd.setCursor(7+5,1);
lcd.打印(tValue);
}
//LED闪烁
如果(毫秒()-previousMillis>间隔){
previousMillis=millis();//还记得上次我们闪烁LED的时候吗
//如果LED关闭,则将其打开,反之亦然。
如果(值==低)
价值=高;
其他的
价值=低;
数字写入(ledPin,值);
}
}
void updateFreq(){
long now=millis();
长t=现在-pMillis;
如果(t>=10000){
频率=(浮动)(计数器-计数);
//频率=((浮动)(计数器-计数))/(浮动)10.0;
pMillis=now;//还记得上次我们闪烁LED的时候吗
pCount=计数器;
}
}
void setThresholdValue(int sensorValue){
如果(传感器值>整数(tValue/0.90)){
tValue=int(传感器值*0.90);
}
}
无效数据计数(int传感器值){
//数东西
if(传感器值
更新
一个更“干净”的代码(请看我自己的答案)
#包括
//启动LCD显示器
液晶液晶显示器(7,8,9,10,11,12);//请参阅设置,网址为http://lassenorfeldt.weebly.com/1/post/2013/02/ardunio-lcd.html
长更新间隔=150;//太太
长更新时间=0;
//申报密码
int-ledPin=3;//LED连接到数字管脚3
//LED设置
int值=低;//LED的先前值
long-previousMillis=0;//将存储上次更新LED的时间
长频率=16;//赫兹(1/秒)
长千=1000;
长闪烁间隔=千次/频率;//毫秒
////LDR计数器变量////
//计数变量
静态整数计数器=0;
int=0;
int checkValue=计数器+1;
//校准变量
长onBootCalibrationTime=5000;//系统启动时用于校准的时间[时间]
静态整数阈值=0;//用于计数的值(开始时已校准)
浮动切割值=0.90;//Procent值,用于允许在不计数的情况下在max信号中进行抖动。
//频率变量
浮动频率=0;//计算的闪烁频率。。。
长频率间隔=5000;//时间[毫秒]
长pMillis=0;
int pCount=0;
无效设置(){
//设置引脚
pinMode(LED引脚,输出);//将数字引脚设置为输出
//显示静态值
lcd.begin(16,2);
lcd.setCursor(0,0);lcd.print(频率);
lcd.setCursor(4,0);lcd.print(“Hz”);
lcd.setCursor(0,1);lcd.print(闪烁间隔);
lcd.setCursor(4,1);lcd.print(“ms”);
//允许登录的设置
Serial.begin(9600);//允许从Putty(windows 7)获取读数
}
void loop(){
长时间=毫秒();
int传感器值=模拟读数(A0);
//闪烁发光二极管
闪烁(时间);
//通过LDR校准或计数(并计算频率)
if(时间更新时间){
updateTime+=updateInterval;//设置下次更新LCD的时间
//显示传感器值
lcd.setCursor(7,1);lcd.print(sensorValue);
//显示使用的阈值
void doCount(int sensorValue){
static int previousState;
int currentState;
if ( previousState == 0 ) {
currentState = sensorValue > upperThreshold;
} else {
currentState = sensorValue > lowerThreshold;
}
if ( previousState != 0 ) {
if ( currentState == 0 ) {
counter++;
}
}
previousState = currentState;
}
#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;
}
}
#include <FreqPeriodCounter.h>
#include <LiquidCrystal.h>
// FrequencyCounter vars
const byte counterPin = 3; // Pin connected to the LDR
const byte counterInterrupt = 1; // = pin 3
FreqPeriodCounter counter(counterPin, micros, 0);
// LCD vars
LiquidCrystal lcd(7, 8, 9, 10, 11 , 12); // see setup at http://lassenorfeldt.weebly.com/1/post/2013/02/ardunio-lcd.html
long updateInterval = 200; // ms
long updateTime = 0;
// LED vars
int ledPin = 5; // LED connected to digital pin 3
int value = LOW; // previous value of the LED
float previousMillis = 0; // will store last time LED was updated
static float freqency; // Hz (1/sec)
static float pfreqency;
static float blinkInterval; // milliseconds
boolean logging = true; // Logging by sending to serial
// Use potentiometer to control LED frequency
int potPin = 5; // select the input pin for the potentiometer
int val = 0; // variable to store the value coming from the sensor
void setup(void){
// Setup the pins
pinMode(ledPin, OUTPUT); // sets the digital pin as output
val = analogRead(potPin);
freqency = map(val, 0, 1023, 0, 25); // Hz (1/sec)
pfreqency = freqency;
blinkInterval = 1000 / (freqency*2); // milliseconds
// LCD display static values
lcd.begin(16, 2);
lcd.setCursor(0,0); lcd.print(freqency);
lcd.setCursor(4,0); lcd.print("Hz");
lcd.setCursor(14,0); lcd.print("Hz");
lcd.setCursor(0,1); lcd.print(blinkInterval);
lcd.setCursor(4,1); lcd.print("ms");
//
attachInterrupt(counterInterrupt, counterISR, CHANGE);
// Logging
if (logging) {Serial.begin(9600);}
}
void loop(void){
// Loop vars
float time = (float) millis();
float freq = (float) counter.hertz(10)/10.0;
// Blink the LED
blinkLED(time);
if (logging) {
if(counter.ready()) Serial.println(counter.hertz(100));
}
// Update the LCD
if (time > updateTime){
updateTime += updateInterval; // set the next time to update the LCD
lcdNicePrint(7+3, 0, freq); lcd.setCursor(14,0); lcd.print("Hz");
val = analogRead(potPin);
freqency = map(val, 0, 1023, 1, 30);
if (freqency != pfreqency){
pfreqency = freqency;
blinkInterval = 1000 / (freqency*2); // milliseconds
lcdNicePrint(0,0, freqency); lcd.setCursor(4,0); lcd.print("Hz");
lcd.setCursor(0,1); lcd.print(blinkInterval);
lcd.setCursor(4,1); lcd.print("ms");
}
}
}
void lcdNicePrint(int column, int row, float value){
lcd.setCursor(column, row); lcd.print("00");
if (value < 10) {lcd.setCursor(column+1, row); lcd.print(value);}
else {lcd.setCursor(column, row); lcd.print(value);}
}
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 counterISR()
{ counter.poll();
}