0

我有一个简单的 RPM 模拟器(开源),它通过tone() 函数生成值。我需要通过 nrf24l01 将 RPM 无线发送到第二个使用 RPM 作为 shiftlight 的 Arduino,两者都来自 chippernut。

tone() 函数只发送到引脚,尝试读取值不起作用。

离开音调功能后如何获得 x (RPM) 的值?

我尝试通过analogRead、digitalRead、BitRead 读取它,尝试打印保持不变的x 值,但无济于事,如果它读取输出引脚,它的更新速度非常慢。

这是代码:

float RPM_MIN = 2500; 
float RPM_MAX = 8000; 
int Accel_Rate = 20; 
float pulses_per_rev = 2.0;  // make sure to keep the decimal

boolean accel = false; 
float x; 
long previousMillis = 0;
unsigned long currentMillis=0;
//float RPM_PRINT;  //my addition to get a value to print

void setup() { 
Serial.begin(57600);
pinMode(5, OUTPUT); 
RPM_MIN = (RPM_MIN / 60); 
RPM_MAX = (RPM_MAX / 60); 
x = RPM_MIN; 
} 

void loop() {
      while (accel==false){
        currentMillis = millis();
        if(currentMillis - previousMillis > Accel_Rate) {
        previousMillis = currentMillis;         
        x++; 
        tone(5,x*pulses_per_rev); 
        if (x>RPM_MAX){accel=true;} 
        }
      }

      while (accel==true){
         currentMillis = millis();
        if(currentMillis - previousMillis > Accel_Rate) {
        previousMillis = currentMillis;         
        x--; 
        tone(5,x*pulses_per_rev); 
        if (x<RPM_MIN){accel=false;} 
        }
      }
       //RPM_PRINT = x*pulses_per_rev;
       //RPM_PRINT = digitalRead(5);
       //RPM_PRINT = analogRead(5);

      //Serial.println(RPM_PRINT);
}

预期结果是 2000-8000 之间不断变化的值,实际结果是 1/0 或 81,33 或 4,1 或 900-980 每隔几秒更新一次。

我该如何解决?

4

0 回答 0