0

I have a simple RPM Simulator (opensource) that generates values through the tone() function. I need to send the RPM wirelessly through an nrf24l01 to a second Arduino that uses the RPM as a shiftlight, both come from chippernut.

The tone() function only sends to the pin, trying to read the values did not work.

How can i get the value of x (RPM) after it leaves the tone function?

I have tried reading it through analogRead, digitalRead, BitRead, tried printing the value of x which stays constant, to no avail, and it updates very slowly if it reads the output pin.

This is the code:

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);
}

Expected Result is a Value between 2000-8000 that is constantly changing actual result is 1/0 or 81,33 or 4,1 or 900-980 updating once every few seconds.

How can I solve?

Cristiano Casciotti
  • 1,016
  • 10
  • 21
  • I suggest posting this at https://arduino.stackexchange.com/ – EvilTeach Aug 23 '19 at 15:15
  • You have a loop that continually increases `x` until it reaches `RPM_MAX`, followed by a loop that decreases `x` until it reaches `RPM_MIN`. Your printing is *not inside either of those loops`, so it doesn't get executed until a complete cycle has been completed. The RPM will always be at minimum at that point. – jasonharper Aug 23 '19 at 16:34
  • @jasonharper Yes! thank you! missed that, that is why x is basically constant, but if it is in two loops, should i then just print x within the loops or go for x and y? or would you have a different suggestion on how to reliably print the value? – Tarek Knanneh Aug 23 '19 at 23:40
  • So i added the `Serial.println()` into both loops, now it is printing accordingly quickly, but the values of RPM_PRINT are between 89 and 267 if i remove the `RPM_MIN / 60` or make it `RPM_MIN / 1` it starts happily counting from 6500 and while writing it is at 15000 , it is probably basic Math that i am missing, but somehow the values are off – Tarek Knanneh Aug 23 '19 at 23:48
  • That is about the range you'd expect for `x*pulses_per_rev`, given the min/max RPM values you've set - what do you think is wrong with those numbers? – jasonharper Aug 23 '19 at 23:57
  • @jasonharper that `RPM_MIN` should start at 2500 and MAX end at 8000.. not 15.000, so it is completely out of that range – Tarek Knanneh Aug 24 '19 at 19:05
  • Perhaps your actual code has diverged, but in the code you posted `RPM_MIN` is *not* 2500 at any point that matters - you divide it by 60 in `start()`. Its actual value during `loop()` is about 41.7, `RPM_MAX` is about 133.3, you're multiplying by `pulses_per_rev` (2) so the output range is 83.3 thru 266.7. – jasonharper Aug 24 '19 at 21:35

0 Answers0