Go Down

Topic: Reading value for Switch Case statement and function once per boot (Read 483 times) previous topic - next topic

__Mitnick__

So, I'm making water controller for valve using two termistors with two types of work. On first work mode it's comparing two values from thermistors and controling valve. On second mode it's using only one thermistor and controling water flow. Swapping with one button.
My problem begins when I want to change work program (mode). I want it to change when I'm pressing button when board is booting. It does not change and always displaying value 1 (values are 0 and 1).
I don't know how to describe it better.
Here's complete code, because I cannot figure where the problem might exist, but as I think after void setup():
Code: [Select]

#include <math.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

#define ThermistorPIN A0                 // Analog Pin 0
#define ThermistorPIN1 A1
#define OutPin 7
#define OutPin1 8
#define OutPin2 9
#define Switch1 10
int startPool = 0;

float vcc = 5;                       // only used for display purposes, if used
                                        // set to the measured Vcc.
float pad = 9780;                       // balance/pad resistor value, set this to
                                        // the measured resistance of your pad resistor
float thermr = 10000;                   // thermistor nominal resistance

float Thermistor(int RawADC) {
  long Resistance; 
  float Temp;  // Dual-Purpose variable to save space.

  Resistance=pad*((1024.0 / RawADC) - 1);
  Temp = log(Resistance); // Saving the Log(resistance) so not to calculate  it 4 times later
  Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
  Temp = Temp - 273.15;  // Convert Kelvin to Celsius                     
  return Temp;                                      // Return the Temperature
}

float Thermistor1(int RawADC) {
  long Resistance; 
  float Temp;  // Dual-Purpose variable to save space.

  Resistance=pad*((1024.0 / RawADC) - 1);
  Temp = log(Resistance); // Saving the Log(resistance) so not to calculate  it 4 times later
  Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
  Temp = Temp - 274.15;  // Convert Kelvin to Celsius                     
  return Temp;                                      // Return the Temperature
}

void setup() {
  Serial.begin(115200);
  lcd.begin(16, 2);
  pinMode(OutPin, OUTPUT);
  pinMode(OutPin1, OUTPUT);
  pinMode(6, INPUT_PULLUP);
  pinMode(Switch1, OUTPUT);
  digitalWrite(OutPin, LOW);
  digitalWrite(OutPin1, HIGH);
  pinMode(13, INPUT_PULLUP);
}

int funcPool(int input13)  {
  if (input13 == LOW)  {
    int startPool = 1;
    return startPool;
    }
  }
 



void loop() {
  float temp;
  temp=Thermistor(analogRead(ThermistorPIN));       // read ADC and  convert it to Celsius
  Serial.print("Celsius: ");
  Serial.print(temp,1);                             // display Celsius
  Serial.println("");
  int input13 = digitalRead(13);
  static int setPool = funcPool(input13);
  switch (setPool) {
    case 0:
      float temp1;
      temp1=Thermistor1(analogRead(ThermistorPIN1));
      Serial.print(temp1,1);
      Serial.println("");
   
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print(temp,1);
      lcd.setCursor(4, 0);
      lcd.print((char)223);
      lcd.setCursor(5,0);
      lcd.print("C - Solar");
 
      lcd.setCursor(0,1);
      lcd.print(temp1,1);
      lcd.setCursor(4, 1);
      lcd.print((char)223);
      lcd.setCursor(5,1);
      lcd.print("C - Tank");
 
      if (temp >= temp1 + 10) {
        digitalWrite(OutPin, HIGH);
        digitalWrite(OutPin1, LOW);
        digitalWrite(OutPin2, LOW);
      }
 
      if (temp < temp1 + 6) {
        digitalWrite(OutPin, LOW);
        digitalWrite(OutPin1, HIGH);
        digitalWrite(OutPin2, HIGH);
      }
    break;
   
  case 1:
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print(temp,1);
    lcd.setCursor(4, 0);
    lcd.print((char)223);
    lcd.setCursor(5,0);
    lcd.print("C - Solar");
   
    if (temp >= 45)  {
      digitalWrite(OutPin, HIGH);
      digitalWrite(OutPin1, LOW);
      digitalWrite(OutPin2, LOW);
    }
    if (temp < 38) {
      digitalWrite(OutPin, LOW);
      digitalWrite(OutPin1, HIGH);
      digitalWrite(OutPin2, HIGH);
    }
    break;
  }
    int sw = digitalRead(6);
  if (sw == HIGH)  {
    digitalWrite(Switch1, LOW);
    lcd.noDisplay();
  } else { 
    digitalWrite(Switch1, HIGH);
    lcd.display();
    delay(2000);
  }
  delay(200);
  Serial.println(setPool);
}

PaulS

Exactly how do Thermistor() and Thermistor1() differ?

Code: [Select]
int funcPool(int input13)  {
  if (input13 == LOW)  {
    int startPool = 1;
    return startPool;
    }
  }

EVERY path needs to return a value.

The art of getting good answers lies in asking good questions.

aarg

It looks as if you are trying to do it here:
Code: [Select]
 int input13 = digitalRead(13);
  static int setPool = funcPool(input13);
  switch (setPool) {

That's in loop(). Didn't you want to check it at boot time? Then it should be in setup().
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

__Mitnick__

Exactly how do Thermistor() and Thermistor1() differ?
Thermistor() and Thermistor1() has difference in converting Kelvin to Celsius, because I have additional resistance on cable equal to 1 Celsius.

EVERY path needs to return a value.
There is
Code: [Select]

    return startPool;


It looks as if you are trying to do it here:
Code: [Select]
int input13 = digitalRead(13);
  static int setPool = funcPool(input13);
  switch (setPool) {

That's in loop(). Didn't you want to check it at boot time? Then it should be in setup().
Yes, I want to check it at boot time but this don't work. I need that Switch is in loop().

PaulS

Quote
There is
No there is not.

What does the function return if input13 is not LOW? Nothing, nada, zip, zilch.
The art of getting good answers lies in asking good questions.

__Mitnick__

Ahh, now I see. Silly mistake. Thanks a lot. Works like a charm.

Go Up