Jump to content

Time to ask for help - data type issue/or bug

Recommended Posts

volatile byte revolutions;

unsigned int rpmilli;
float speed;
long gTest;

unsigned long timeold;

void setup()
  attachInterrupt(PUSH2, rpm_fun, FALLING);

  revolutions = 0;
  rpmilli = 0;
  timeold = 0;

void loop()
  if (revolutions >= 5) { 
    //Update RPM every 20 counts, increase this for better RPM resolution,
    //decrease for faster update

    // calculate the revolutions per milli(second)
    rpmilli = (revolutions/(millis()-timeold)); //** EDIT: it should be revolutions/(millis()-timeold)
    unsigned long test = millis()-timeold;
    gTest = 5/(timeold);

    // WHEELCIRC = 2 * PI * radius (in meters)
    // speed = rpmilli * WHEELCIRC * "milliseconds per hour" / "meters per kilometer"

    // simplify the equation to reduce the number of floating point operations
    // speed = rpmilli * WHEELCIRC * 3600000 / 1000
    // speed = rpmilli * WHEELCIRC * 3600

   // speed = rpmilli * 2.6958 * 3600;
    Serial.print(rpmilli * 60000,DEC);
    Serial.print(" Speed:");
    Serial.println(" kph");
    timeold = millis();
    revolutions = 0;
    gTest = 0;

void rpm_fun()

Ok, so I have been beating my head against a wall for hours on this.  This line:


gTest = 5/(timeold);


Shows a 0 on the serial monitor everytime.  Now I think I have tried just about every data type for gTest that I can think of and that doesn't seem to change anything.  If I multiply instead of dividing the two values, it works just fine...however dividing doesn't work.  Any ideas?


Obviously, test and gTest are for testing.  Using PUSH2 for the interrupt is just a test until my hall effect sensors get here (today).  I am working on a computer for my bicycle and need to determine speed. 


Neither value is negative, nor are they 0, so is it some kind of overflow?  If I have made some simple mistake, I would be glad to know what it is so that I don't do it again :-)







Link to post
Share on other sites

If that doesn't work use "double" instead although I'm fairly certain they're the same type in mspgcc (someone correct me if I'm wrong).  Either way, the code in Print.cpp in Energia's msp430 core references "double" for its floating point output functions.


I'm not 100% sure what mspgcc will do with "5 / var" when var is unsigned long and not casted... but using "5.0 / (float) var" explicitly casts the integer to float and using "5.0" implies a floating point constant too.

Link to post
Share on other sites

one trick I have used in past to get around integer limitations in formula is to premultiply the inputs by 100 or 1000 as long as the number stays within the 65535 limit of a 16 bit integer.  so you end up with 5000/7000 which integer math won't clip the numbers. 


so if you need to run the formula 5 / 7 * 3,  the fact that it dips to 0 once, ruins the rest of the calculation cause the 0 carries forward, and will remain 0 from there on out


you divide back down by 100 or 1000 whatever as the last step

Link to post
Share on other sites

@@spirilis and @@Rei Vilo - Thank you very much for all of your help.  Everything is working now.  I understand what I was doing wrong.  I really appreaciate it.


My hall effect sensors came today - I just hooked it up, put a magnet on a broken bit for my dremel, and started slow, and ended up fast...seemed to be pretty darn close on the RPM.  I am very impressed.  I think it will be able to handle the massive RPM on my bicycle wheels ;-) 


Thanks again!



Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...