NaN Error

So have an Arduino hooked up to my computer... it works perfect... no problems, no errors. I unplug my computer, plug the Arduino directly into the power supply and then I start getting the NaN error and it won't operate as it's intended to. This is not a code problem... this is a hardware problem.

I'm using Wind Sensor Rev C from Modern Device (Wind Sensor Rev. C – Modern Device) and it's worked just fine in the past, but now it seems as though there is some sort of interference when the Arduino is being powered with something other than my computer. The wind sensor is plugged into the Arduino for 5v and GND and the power supply is grounded to the Arduino, which is a set-up I'm currently using else where, which adds to my confusion.

I'm not sure if my description is clear enough to warrant any sort of advice, but if there is any information on the wiring aspects of ground and power in relation to the loop between sensors, Arduino, and actual power supplies, that might be a place to start.

Thank you.

I guess there's something wrong with your power supply.

And I guess this error shows up on the computer? What is the computer doing when this happens? Are you downloading to the Arduino? Reading from the serial monitor? Is there a special application running on the computer?

This is not a code problem... this is a hardware problem.

It's certainly related to software. You won't get that error running the Blink LED example...

If you don't know I - It means "not a number", so it could be a divide by zero error or something like that. It's usually the result of an expression/operation because everything in computer/Arduino memory (or in a file) is a binary number.

The reason I say it's a hardware problem is because it was doing the same thing on the computer, then I checked my grounds and found a couple of problems. After I fixed them, everything started working perfectly. When I went to plug the Arduino into the power supply, I started have the exact same problems (physically, as I'm not connected to the Arduino when it's plugged into the power supply, so I can't read the monitor). Since it's exhibiting that exact same physical errors, I assume it's same problem I was facing before, as I'm seeing the same characteristics. I assume it has to be some sort of problem with my grounds, but everything looks clean to me.

This is a setup that we've been using for quite some time, but instead of using a microphone to sense wind (really, really, inefficient, but it worked...) we've upgraded to the sensor I linked in my OP.

Plugged into my laptop, there's no problem -- serial monitor is clear, I'm getting the responses I expect, code is executing exactly as it's supposed to with out fail.

Coming in this morning, I realized the sensor was touching the metal if the fireplace it's in (I'll describe it in detail, if you believe it's necessary), so I made some adjustments to make sure it wasn't touching any metal, which I thought might have been the culprit... but just like yesterday, it works perfectly on my laptop, but not when plugged into the power supply.

Also, I've been using these wind sensors for a while, so they're nothing new to me, but this problem just stumps me.

deavonwalden:
This is not a code problem... this is a hardware problem.

NaN = Not a Number.

  1. Code problem.
    or
  2. Stupid input which is beyond the capacity of the code to handle.

I can't see it being power, as DV suggests, but I guess there could be a problem with power to the sensor.

I've somewhat isolated the problem now...

When I have the Arduino plugged into my computer and the power supply off, everything works fine.
When I have the Arduino plugged into the power supply, it bugs out and does stupid things.
When I have the Arduino plugged into my computer and the power supply on, it bugs out and does stupid things.

My analog read does goes from steady to crazy. Below is a copy/paste of the moment the power supply off, then I switch it on, and it does weird things. You can see that it's pretty steady, then all hell breaks loose:
0 0 | WS1 MPH |2.86
0 0 | WS1 MPH |2.99
0 0 | WS1 MPH |2.93
0 0 | WS1 MPH |2.91
0 0 | WS1 MPH |3.04
0 0 | WS1 MPH |18.02
0 1 | WS1 MPH |1.17
0 1 | WS1 MPH |0.09
0 1 | WS1 MPH |41.57
0 1 | WS1 MPH |nan
0 1 | WS1 MPH |6.60
0 1 | WS1 MPH |14.23
0 1 | WS1 MPH |nan
0 1 | WS1 MPH |26.59

The variables on the left side are of no consequence. They are simply reacting to what the wind sensor is doing, as they are intended to. Hopefully this may help to shed some light on the nature of the situation.

My analog read does goes from steady to crazy.

OK, I believe you have a hardware problem. I have no idea what that hardware problem is.

What power supply are you using? Is it plugged into the barrel jack, Vin, or 5V?

But, you've also got a software bug. The raw analog reading IS a valid number (from 0-1023 on a "regular" Arduino). And somehow your software is not able to convert that number to wind speed.

Maybe your software should report an error if the wind speed is over 500MPH (or something like that) but 'Nan' is the result of a bug! You shouldn't get that kind of error even if the raw reading from the windspeed indicator is "unreasonable".

It wouldn't hurt to "print" the raw ADC reading, and it wouldn't hurt to monitor/measure the analog input with a multimeter so you can "see" what's happening.

If you post your code maybe we can help with the software error.

Some of this code is copied/pasted from other programs and the math that translates the temp reading to wind speed is above my pay-grade... luckily, Modern Device had something cooked up already for the numbers.

I believe it should be fairly self-explanatory. I've learned everything I know online and from mentors with dozens of years of experience in coding and electronics... so forgive me if there is a bit of mess and weirdness.

#define analogPinForRV1    8
#define analogPinForTMP1   9

// to calibrate your sensor, put a glass over it, but the sensor should not be
// touching the desktop surface however.
// adjust the zeroWindAdjustment until your sensor reads about zero with the glass over it.

const float zeroWindAdjustment =  .2; // negative numbers yield smaller wind speeds and vice versa.

int TMP_Therm_ADunits1;  //temp termistor value from wind sensor
float RV_Wind_ADunits1;    //RV output from wind sensor
float RV_Wind_Volts1;
int TempCtimes1001;
float zeroWind_ADunits1;
float zeroWind_volts1;
float WindSpeed_MPH1;

const int fire = 22; // light from the fire place
const int heat = 23; // heat from the fire place
const int scent = 24; // smell from scent machine

int tPassed = 0; //if thresholdPass has reached a certain amount
int ON = 0;
int OFF = 1;
int fireComplete = 0;

int candlesActive;
int startFireStuff = 0; 

unsigned long previousMillis;
unsigned long previousMillis2;
unsigned long previousMillis3;
const long heatTimeOn = 120000; // how long the heat blows from the fireplace
const long scentTimeOn = 20000; //timer for scent machine. TOO STRONG if on for too long

void setup() {
  // put your setup code here, to run once:

  pinMode(fire, OUTPUT);
  pinMode(heat, OUTPUT);
  pinMode(scent, OUTPUT);
  pinMode(candlesActive, OUTPUT);

  digitalWrite(fire, OFF);
  digitalWrite(heat, OFF);
  digitalWrite(scent, OFF);
  digitalWrite(candlesActive, OFF);

  Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:


  //pin #21
  TMP_Therm_ADunits1 = analogRead(analogPinForTMP1);
  RV_Wind_ADunits1 = analogRead(analogPinForRV1);
  RV_Wind_Volts1 = (RV_Wind_ADunits1 *  0.0048828125);

  // these are all derived from regressions from raw data as such they depend on a lot of experimental factors
  // such as accuracy of temp sensors, and voltage at the actual wind sensor, (wire losses) which were unaccouted for.

  //pin #21
  TempCtimes1001 = (0.005 * ((float)TMP_Therm_ADunits1 * (float)TMP_Therm_ADunits1)) - (16.862 * (float)TMP_Therm_ADunits1) + 9075.4;

  zeroWind_ADunits1 = -0.0006 * ((float)TMP_Therm_ADunits1 * (float)TMP_Therm_ADunits1) + 1.0727 * (float)TMP_Therm_ADunits1 + 47.172; //  13.0C  553  482.39

  zeroWind_volts1 = (zeroWind_ADunits1 * 0.0048828125) - zeroWindAdjustment;

  // This from a regression from data in the form of
  // Vraw = V0 + b * WindSpeed ^ c
  // V0 is zero wind at a particular temperature
  // The constants b and c were determined by some Excel wrangling with the solver.

  WindSpeed_MPH1 =  pow(((RV_Wind_Volts1 - zeroWind_volts1) / .2300) , 2.7265);
  /*
    Serial.print(fireComplete);

      Serial.print("  TMP volts1 |");
      Serial.print(TMP_Therm_ADunits1 * 0.0048828125);

      Serial.print("|  RV volts1 |");
      Serial.print((float)RV_Wind_Volts1);

      Serial.print("| TempC*1001 |");
      Serial.print(TempCtimes1001 );

      Serial.print("|  ZeroWind volts1 |");
      Serial.print(zeroWind_volts1);
  */
  Serial.print(fireComplete);
  Serial.print(" ");
  Serial.print(startFireStuff);
  Serial.print(" ");
  Serial.print("|  WS1 MPH |");
  Serial.println((float)WindSpeed_MPH1);
  /*
    Serial.print(digitalRead(fire));
    Serial.print(" ");
    Serial.print(digitalRead(heat));
    Serial.print(" ");
    Serial.print(digitalRead(scent));
    Serial.print(" ");
    Serial.print(tPassed);
    Serial.print(" ");
    Serial.print(fireComplete);
    Serial.println(" ");*/

  if (fireComplete == 0) {
    if (WindSpeed_MPH1 >= 10.00) { //reads input from a windsensor to see when air is blown on it
      startFireStuff += 1;
    }
    if (startFireStuff >= 10) {
      digitalWrite(fire, ON);
      digitalWrite(heat, ON);
      digitalWrite(scent, ON);
      digitalWrite(candlesActive, ON);
      if (tPassed == 0) {
        previousMillis = millis(); // initialize heat timer
        previousMillis3 = millis(); // intialize scent timer
        tPassed = 1;
      } // end tPassed
    } // end startFireStuff
    if (tPassed == 1) {
      if (millis() - previousMillis3 >= scentTimeOn) {
        digitalWrite(scent, OFF);
      }
      if (millis() - previousMillis >= heatTimeOn) { // check to see if time has expired
        digitalWrite(heat, OFF);
        fireComplete = 1;
        tPassed = 2;
      } // end timer
    } // end if tPassed
  } // end fireComplete
} // end loop