|
本帖最后由 kururu12112 于 2021-12-30 13:33 编辑
這個程式要的功能是測風扇的轉速先設100%duty cycle測4秒再測20%duty cycle10秒總共14秒,檢測第4秒跟第14秒的轉速有沒有到設定的範圍值
有到就亮綠燈,沒有到就亮紅燈。
我目前是分開測可以,但是合在一起測就只能運行20%,而且也不知該如何結合起來,請教大家我應該如何改這個程式。
我知道這個程式碼很爛QAQ但是先求有再求好了 謝謝各位QAQQ
#include "Timer.h"
#define pwm 5 //pwm輸出
#define fan_speed 9 //測速
const int red = 10;//紅燈
const int green = 11;//綠燈
int PotBuffer = 0;
int Speed_PWM = 0;
unsigned long duration = 0;
Timer t1; //建立Timer物件
Timer t2; //建立Timer物件
void setup() {
Serial.begin(115200);
pinMode(pwm, OUTPUT);
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(fan_speed, INPUT);
t1.after(4000,hundred);
t2.after(14000,twenty);
for(int counter = 0; counter < 4; counter++) {
Speed_PWM = 0;
Serial.print("Speed_PWM=");
Serial.print(Speed_PWM);
analogWrite(pwm, Speed_PWM );
duration = 0;
if (Speed_PWM < 1023) {
for (int i = 0; i < 5; i++) {
duration += pulseIn(fan_speed, HIGH);
duration += pulseIn(fan_speed, LOW);
}
} else {
duration = 4294967295;
}
duration = duration / 5;
duration = 1000000 / duration;
Serial.print(" f is ");
Serial.print(duration);
Serial.print(" HZ");
duration = duration * 30;
Serial.print(" v is ");
Serial.print(duration);
Serial.println(" r/min");
if(duration >= 14900){
Serial.println("100%PASS");
}
else{
Serial.println("FAIL");
}
delay(1000);
}
t1.stop(hundred);
for(int counter = 0; counter < 10; counter++) {
Speed_PWM = 194;
Serial.print("Speed_PWM=");
Serial.print(Speed_PWM);
analogWrite(pwm, Speed_PWM );
duration = 0;
if (Speed_PWM < 1023) {
for (int i = 0; i < 5; i++) {
duration += pulseIn(fan_speed, HIGH);
duration += pulseIn(fan_speed, LOW);
}
} else {
duration = 4294967295;
}
duration = duration / 5;
duration = 1000000 / duration;
Serial.print(" f is ");
Serial.print(duration);
Serial.print(" HZ");
duration = duration * 30;
Serial.print(" v is ");
Serial.print(duration);
Serial.println(" r/min");
if((duration <= 4700)&&(duration >= 2500)){
Serial.println("20%PASS");
} else {
Serial.println("FAIL");
}
delay(1000);
}
}
void hundred(){
if(duration >= 14900){
digitalWrite(green, HIGH);
digitalWrite(red, LOW);
} else {
digitalWrite(green, LOW);
digitalWrite(red, HIGH);
}
}
void twenty(){
if((duration <= 4700)&&(duration >= 2500)){
digitalWrite(green, HIGH);
digitalWrite(red, LOW);;
} else {
digitalWrite(green, LOW);
digitalWrite(red, HIGH);
}
}
void loop() {
t1.update(); //更新t
t2.update(); //更新t
}
|
|