Sensor Readings not working in full code but works in individual function

I’ve encountered a frustrating problem that I have worked on for hours and can’t seem to figure out. I have three sensors that are all read, and based on the values that are returned determines that the code does. The code operates a water filtration system.

One of the meters, and electrical conductivity meter, will not produce values when it is run in the full code, but the function I wrote to have it read values and average them works perfectly on it’s own and in conjunction with everything else. It only fails when the full code is run. It’s function for taking the average of a number of values is the same as the other two sensors and they work perfectly, even in the full code.

I can even read all three sensors and have them display the values in the serial monitor, but as soon as I try to run the entire code, the value for conductivity produces zeroes. I had it display the values as it calculated the average, and it reads only zeroes, thus making the average of the samples 0.

Any help would be greatly appreciated, I am unbelievably baffled as to why it won’t work.

Attached is the full code, I apologize as it is quite lengthy and ugly. The problem as I mentioned is with the “float conductivity” function and where it runs at the top of the void loop. If I comment out everything below line 175 it will display all sensor data, but as soon as I try to run beyond that it will display only zeroes.

July_2016_code_for_pump_operation_rev4.ino (8.03 KB)

float conductivity (int elecCondSensor)
{
  uint8_t k;
  float average3;
  for (k = 0; k < numSamples; k++)
    {
      samples[k] = analogRead(elecCondSensor);
      delay(10);
    }
    average3 = 0;

    for (k = 0; k < numSamples; k++)
    {
      average3 += samples[k];
    }
    average3 /= numSamples;
    return average3;
}

There is no reason to do this with 2 for loops.

float conductivity (int elecCondSensor)
{
  uint8_t k;
  float average3 = 0;
  for (k = 0; k < numSamples; k++)
    {
      samples[k] = analogRead(elecCondSensor);
      average3 += samples[k];
      delay(10);
    }
    average3 /= numSamples;
    return average3;
}

From this, it is easy to see that the array of values is not needed.

float conductivity (int elecCondSensor)
{
  float average3 = 0;
  for (uint8_t k = 0; k < numSamples; k++)
    {
      average3 += analogRead(elecCondSensor);
      delay(10);
    }
    average3 /= numSamples;
    return average3;
}
  tempReading = readTemps(thermistorPin, seriesResistor, temperatureNominal, thermistorNominal, bCoefficient);

Why do you need to pass global values to this function?

What does your serial output look like? How do you know that you are getting wrong results? Where is the code that gets right results?

I’m extremely new to coding, thus, the way that I have done everything is the only way that I could self learn how to do it. Prior to this project I had ZERO experience in both coding and Arduino. Hence why my code most likely makes experienced coders cringe.

As I mentioned, the code will run perfectly if I comment out from line 175 down, I get values that are non zero, obviously the conductivity of the water is none zero. As soon as I uncomment from line 175 down, the values for the conductivity function become zero.

The serial output looks exactly as it should, excepting that the value of the conductivity is zero when it most definitely should not be, and definitely is not when the full code is not run.

I simply cannot get the value of conductivity to be non-zero when the full code is run, but it is non-zero when running only the section of code to line 175.

This code works perfectly.

[code]
float conductValue;
float level;

#define thermistorPin A4
#define numSamples 5
#define seriesResistor 10000
#define temperatureNominal 25
#define thermistorNominal 10000
#define bCoefficient 3950
int samples[numSamples];
float tempReading;
int elecCondSensor = A2;            //Electrical Conductivity Sensor in outflow
int sedFreeTankSensor = A0;


float conductivity (int elecCondSensor)
{
  uint8_t m;
  float average3;
  for (m = 0; m < numSamples; m++)
    {
      samples[m] = analogRead(elecCondSensor);
      delay(10);
    }
    average3 = 0;

    for (m = 0; m < numSamples; m++)
    {
      average3 += samples[m];
      Serial.println(average3);
    }
    average3 /= numSamples;
    Serial.println(average3);
    return average3;
}

float fillTank (int sedFreeTankSensor)
{
  uint8_t j;
  float average2;
  for (j = 0; j < numSamples; j++)
    {
      samples[j] = analogRead(sedFreeTankSensor);
      delay(10);
    }
    average2 = 0;

    for (j = 0; j < numSamples; j++)
    {
      average2 += samples[j];
    }
    average2 /= numSamples;
    return average2;
}


  
float readTemps(int thermistorpin, int seriesresistor, int temperaturenominal, int thermistornominal, int bcoefficient)
{
  uint8_t i;
  float average;
  for (i = 0; i < numSamples; i++)
  {
    samples[i] = analogRead(thermistorpin);
    delay(10);
  }
  average = 0;

  for (i = 0; i < numSamples; i++)
  {
    average += samples[i];
  }
  average /= numSamples;
  average = 1023 / average - 1;
  average = seriesresistor / average;
  float steinhart;
  steinhart = average / thermistornominal;
  steinhart = log(steinhart);
  steinhart /= bcoefficient;
  steinhart += 1.0 / (temperaturenominal + 273.15);
  steinhart = 1.0 / steinhart;
  steinhart -= 273.15;
  return steinhart;
}
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(relay1, OUTPUT);      //Sets Relay1 as Output
pinMode(relay2, OUTPUT);      //Sets Relay2 as Output
pinMode(relay3, OUTPUT);      //Sets Relay3 as Output
pinMode(relay4, OUTPUT);      //Sets Relay4 as Output
pinMode(relay5, OUTPUT);      //Sets Relay5 as Output
pinMode(relay6, OUTPUT);      //Sets Relay6 as Output
pinMode(relay7, OUTPUT);      //Sets Relay7 as Output
pinMode(relay8, OUTPUT);      //Sets Relay8 as Output
pinMode(PWMA, OUTPUT);
pinMode(Ain1, OUTPUT);
pinMode(elecCondSensor, INPUT);
}

void loop() 
{
  // put your main code here, to run repeatedly:
  level = fillTank(sedFreeTankSensor);
  Serial.print("Water Level: ");
  Serial.println(level);
  delay(150);
  conductValue = conductivity(elecCondSensor);
  Serial.print("Arbitrary conductivity Value ");
  Serial.println(conductValue);
  delay(150);
  tempReading = readTemps(thermistorPin, seriesResistor, temperatureNominal, thermistorNominal, bCoefficient);
  Serial.print("The Water Temperature is (this is wrong): ");
  Serial.println(tempReading);
  delay(5000);


}

[/code]

As I mentioned, the code will run perfectly if I comment out from line 175 down

You implied that you had a different sketch that worked.

Commenting out code after calculating a value should have no affect on the calculated value, unless the code that you comment out uses more memory than you have available.

You have a lot of string literals that get copied into SRAM uselessly. Use the F() macro to stop that from happening:
Serial.print(F("The Water Temperature is (this is wrong): "));

Ok, I will definitely do that! I'm unsure what that means or does but as long as the end goal is the same I'm on board!

Quick edit on memory: I'm using an Arduino Yun, and when I upload the code I am below 50% of memory usage if that makes a difference or not to what you suggest I do not know.

I do not know.

Keep the string literals out of SRAM, and try the changed code. Then you'll know.

Sorry for the delayed reply, the filtration system is no where near my house so it’s quite the round trip drive to upload and try new code.

I did as you suggested and made all necessary changes, it seems to have worked for the conductivity sensor, it now produces values that are non-zero. Thank you so much for your help!

I have another issue I hope you can help me with.

I have one last sensor to try to get online, it is a hall sensor flow meter.

I use this code to test the meter and it operates correctly.

[code]
#define flowPin 3   //This is the input pin on the Arduino
float flowRATE;
float flowrate;
volatile int count; //This integer needs to be set as volatile to ensure it updates correctly during the interrupt process.  

void setup() {
  // put your setup code here, to run once:
  pinMode(flowPin, INPUT);           //Sets the pin as an input
  attachInterrupt(digitalPinToInterrupt(3), Flow, RISING);  //Configures interrupt 0 (pin 2 on the Arduino Uno) to run the function "Flow"  
  Serial.begin(9600);  //Start Serial

}

void loop() 
{
flowRATE = flowRead(flowPin);
delay(5000);

}

void Flow()
{
   count++; //Every time this function is called, increment "count" by 1
}

float flowRead(int flowpin)
{
  count = 0;      // Reset the counter so we start counting from 0 again
  interrupts();   //Enables interrupts on the Arduino
  delay (1000);   //Wait 1 second 
  noInterrupts(); //Disable the interrupts on the Arduino
  
  //Start the math
  flowrate = (count * 0.135);        //Coverts pulses to Liters Per Minute (2.25*60/1000=0.135)
  Serial.println(flowrate);
  return flowrate;
}

[/code]

But when I run it in the overall code nothing at all happens, this code is attached below. It seems to upload correctly to the arduino but then it almost seems as if the arduino freezes and nothing will work. It won’t even let me upload a different sketch unless I unplug the usb and replug it in.

Any help or suggestions on this matter would be greatly appreciated.

OCT_2016_Code_for_pump_operation.ino (8.55 KB)

  attachInterrupt(digitalPinToInterrupt(3), Flow, RISING);  //Configures interrupt 0 (pin 2 on the Arduino Uno) to run the function "Flow"

No, it does not!

Any help or suggestions on this matter would be greatly appreciated.

Fire up the IDE. Open that sketch. Select all the code. Hit delete. Start over. That code is crap. Absolute crap. The ONLY thing that happens with interrupts enabled is delay(). Everything else that happens in the code happens with interrupts disabled.

The ONLY thing that should happen with interrupts disabled is to copy/reset volatile variables.

Nothing ruins a forums helpfulness like a jackass such as your self ripping on people for a typo. Your knowledge is completely irrelevant if you need to be an asshole to communicate and are only marginally helpful. Especially when outside of stating that it’s crap, you offer zero direction on structured learning or explanation of why it is poor in quality.

Get off your high horse and learn some basic respect.

Have a wonderful day!

Have a wonderful day!

You, too.

Interrupt / delay. No good

FWIW, I've seen PaulS pronounce code stupid many times on this forum, but I don't believe I've ever seen him call a poster stupid. A significant distinction to my mind.

regards,
Michael