"nan" and "inf" returning to serial monitor from velocity calculation

Hello all, I am a new Arduino user. I am creating a portable weather station that calculates wind speed through a rotary encoder (Nidec RES20D50-201-1), tells the temperature, and tells the UV level. The problem I am having is when I try to calculate the positional data from the rotary encoder into velocity of the wind, it outputs nan until I start spinning the encoder, to which inf displays. This could be an error in the formula I used, but any help would be much appreciated. This is my code.

/**************************************************************************/
/*!
This is a demo for the Adafruit MCP9808 breakout
----> http://www.adafruit.com/products/1782
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
*/
/**************************************************************************/

#include <Wire.h>
#include "Adafruit_MCP9808.h"

// Create the MCP9808 temperature sensor object
Adafruit_MCP9808 tempsensor = Adafruit_MCP9808();

#include <Wire.h>
#include "Adafruit_SI1145.h"

Adafruit_SI1145 uv = Adafruit_SI1145();

// include the library code:
#include <LiquidCrystal.h>

#include <Encoder.h>

Encoder myEnc(2, 3);

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 7, en = 8, d4 = 9, d5 = 10, d6 = 11, d7 = 12;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

unsigned long previousMillisEN = 0;
unsigned long previousMillisTemp = 0;
unsigned long previousMillisUV = 0;

#define temp 1000
#define UV 1000
#define ending 5000
#define R_C 0.031416

float AnSpeed;
float Speed;

void setup() 
{
  Serial.begin(9600);
  while (!Serial); //waits for serial terminal to be open, necessary in newer arduino boards.
  Serial.println("Adafruit MCP9808");
  
  // Make sure the sensor is found, you can also pass in a different i2c
  // address with tempsensor.begin(0x19) for example, also can be left in blank for default address use
  // Also there is a table with all addres possible for this sensor, you can connect multiple sensors
  // to the same i2c bus, just configure each sensor with a different address and define multiple objects for that
  //  A2 A1 A0 address
  //  0  0  0   0x18  this is the default address
  //  0  0  1   0x19
  //  0  1  0   0x1A
  //  0  1  1   0x1B
  //  1  0  0   0x1C
  //  1  0  1   0x1D
  //  1  1  0   0x1E
  //  1  1  1   0x1F
  if (!tempsensor.begin(0x18)) {
    Serial.println("Couldn't find MCP9808! Check your connections and verify the address is correct.");
    while (1);
  }
  tempsensor.setResolution(0); // sets the resolution mode of reading, the modes are defined in the table bellow:
  // Mode Resolution SampleTime
  //  0    0.5°C       30 ms
  //  1    0.25°C      65 ms
  //  2    0.125°C     130 ms
  //  3    0.0625°C    250 ms


  Serial.begin(9600);
  
  Serial.println("Adafruit SI1145");
  
  if (! uv.begin(0x60)) {
    Serial.println("Didn't find Si1145");
    while (1);
  

  Serial.println("OK!");
  }
  Serial.begin(9600);
}

long oldPosition = -999;

void loop() 
{
  unsigned long currentMillisEN = millis();
  
  tempsensor.wake();   // wake up, ready to read!
  // Read and print out the temperature, also shows the resolution mode used for reading.
  Serial.println (tempsensor.getResolution());
  float c = tempsensor.readTempC();
  Serial.print("Temp: "); 
  Serial.print(c, 1); Serial.print("*C");   
  
  tempsensor.shutdown_wake(1); // shutdown MSP9808 - power consumption ~0.1 mikro Ampere, stops temperature sampling
  
  // Uncomment if you have an IR LED attached to LED pin!
  //Serial.print("Prox: "); Serial.println(uv.readProx());
  float UVindex = uv.readUV();
  // the index is multiplied by 100 so to get the
  // integer index, divide by 100!
  UVindex /= 100.0;  
  Serial.print("UV: ");  Serial.println(UVindex);
  long newPosition = myEnc.read();

  AnSpeed = (R_C*(newPosition - oldPosition))/((currentMillisEN - previousMillisEN)/1000);
  Speed = AnSpeed*(0.01);
  previousMillisEN = currentMillisEN;
  Serial.print(Speed);
  
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
   }
  
    // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("T: ");
  lcd.print(c, 1); lcd.print("*C");
   
  lcd.setCursor(0, 1);
  lcd.print("UV: ");
  lcd.print(UVindex);
    
  lcd.setCursor(9, 1);
  lcd.print("V: "); lcd.print(Speed);
}

You're likely getting division by zero. Print out your current and previous millis readings. If they're not at least a second apart, then that integer division by 1000 will produce zero. Try 1000.0 instead.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.