Arduino - Moisture Control

From Lumenita
Jump to: navigation, search

Note: The code works as coded for moisture control, but many of the comments and some of the code is not the most efficient.


#include <math.h>
#include <EEPROM.h>
 
const int CONFIG_DONE = 0;
const int MOIST_POLL_DELAY = 1; //moisterature polling delay. 1-254 seconds 
const int PUMP_MIN_ON_DELAY = 2; //Minimum time the pump will stay once it is turned on. 1-254 minutes
const int PUMP_HIGH_ON_DELAY = 3; //Delay to switch to high speed. 1-254 seconds
const int PUMP_HIGH_MODE = 4;  //0 - for low speed only : 1 - for high speed mode
const int TIMER_1_POLL_DELAY = 5; // Timer 1 delay. 1-254 seconds
const int COM_POLL_DELAY = 6; // COM delay. 10-2540 milliseconds
const int PUMP_MAX_ON_DELAY = 7; // delay. 10-2540 milliseconds
const int MOIST1_THRESHOLD = 11; //moisterature 1 threshold in voltage ticks/100
const int MOIST2_THRESHOLD = 12; //moisterature 2 threshold in voltage ticks/100
const int MOIST3_THRESHOLD = 13; //moisterature 3 threshold in voltage ticks/100
const int MOIST4_THRESHOLD = 14; //moisterature 4 threshold in voltage ticks/100
// Pin Constants
const int MOIST1PIN = 3;    // Analog input pin for the Thermistor
const int MOIST2PIN = 2;    // Analog input pin for the Thermistor
const int MOIST3PIN = 1;    // Analog input pin for the Thermistor
const int MOIST4PIN = 0;    // Analog input pin for the Thermistor
const int PUMP_LOW_ON = 4;   // Pin number for pump low
const int PUMP_HIGH_ON = 5;  // Pin number for pump high
const int MOIST1_SENSE_ON = 9;  // Pin number for moist1 over threshold led
const int MOIST2_SENSE_ON = 10; // Pin number for moist2 over threshold led
const int MOIST3_SENSE_ON = 11; // Pin number for moist3 over threshold led
const int MOIST4_SENSE_ON = 12; // Pin number for moist4 over threshold led
 
int val_cnt = 0;     // counter for the moist_val                   
int moist1_val[5] = {0,0,0,0,0};       // variable to store the value coming from the sensor
int moist2_val[5] = {0,0,0,0,0};       //           
int moist3_val[5] = {0,0,0,0,0};       //
int moist4_val[5] = {0,0,0,0,0};       //
int moist1_avg;                   // average over poll time of the moist values
int moist2_avg;
int moist3_avg;
int moist4_avg;
int pump_high_mode;              //Use high pump speed
int threshold1; 
int threshold2;
int threshold3;
int threshold4;
int command = 0;       // This is the command char, in ascii form, sent from the serial port     
int i;
int poll_moists = 0; //Send moists each poll cycle when true
int poll_status = 0; //Send status each poll cycle when true
unsigned long moist_poll_time;  // The time to Poll the moistPins
unsigned long timer_1_poll;  // The timer 1 poll time usually 1 sec
unsigned long com_poll;  // The com poll time usually 1 sec
unsigned long pump_max_on_delay;
unsigned long previousMillis = 0;        // will store last time moist was updated
unsigned long timer_1_millis = 0;        // will store last time moist was updated
unsigned long com_millis = 0;        // will store last time moist was updated
unsigned long pump_on_start = 0;        // Start time when pump comes on
unsigned long pump_high_on_delay;     // pump High on delay timer
unsigned long pump_min_on_delay;   // Time to wait before checking the ducts again
 
double Thermister(int RawADC) {  // Nice little Function found on the Arduino Playground to convert a Thermistor voltage to a Temp in F
 double Temp;
 Temp = RawADC;
// Temp = log(((10240000/RawADC) - 10000));
// Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
// Temp = Temp - 273.15;            // Convert Kelvin to Celcius
// Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
 return Temp;
}
void save_config() {
  if (moist_poll_time != EEPROM.read(MOIST_POLL_DELAY)*1000) {EEPROM.write(MOIST_POLL_DELAY, moist_poll_time/1000);}
  if (timer_1_poll != EEPROM.read(TIMER_1_POLL_DELAY)*1000) {EEPROM.write(TIMER_1_POLL_DELAY, timer_1_poll/1000);}
  if (com_poll != EEPROM.read(COM_POLL_DELAY)*10) {EEPROM.write(COM_POLL_DELAY, com_poll/10);}
  if (pump_min_on_delay != EEPROM.read(PUMP_MIN_ON_DELAY)) {EEPROM.write(PUMP_MIN_ON_DELAY, pump_min_on_delay);}
  if (pump_high_on_delay != EEPROM.read(PUMP_HIGH_ON_DELAY)) {EEPROM.write(PUMP_HIGH_ON_DELAY, pump_high_on_delay);}
  if (pump_max_on_delay != EEPROM.read(PUMP_MAX_ON_DELAY)) {EEPROM.write(PUMP_MAX_ON_DELAY, pump_max_on_delay);}
  if (pump_high_mode != EEPROM.read(PUMP_HIGH_MODE)) {EEPROM.write(PUMP_HIGH_MODE, pump_high_mode);}
  if (threshold1 != EEPROM.read(MOIST1_THRESHOLD)*10) {EEPROM.write(MOIST1_THRESHOLD, threshold1/10);}
  if (threshold2 != EEPROM.read(MOIST2_THRESHOLD)*10) {EEPROM.write(MOIST2_THRESHOLD, threshold2/10);}
  if (threshold3 != EEPROM.read(MOIST3_THRESHOLD)*10) {EEPROM.write(MOIST3_THRESHOLD, threshold3/10);}
  if (threshold4 != EEPROM.read(MOIST4_THRESHOLD)*10) {EEPROM.write(MOIST4_THRESHOLD, threshold4/10);}
}
 
void init_config(){
  if (EEPROM.read(CONFIG_DONE) != 1){ //
    EEPROM.write(CONFIG_DONE, 1);
    EEPROM.write(MOIST_POLL_DELAY, 1);
    EEPROM.write(TIMER_1_POLL_DELAY, 1);
    EEPROM.write(COM_POLL_DELAY, 100);
    EEPROM.write(PUMP_MIN_ON_DELAY, 5);
    EEPROM.write(PUMP_HIGH_ON_DELAY, 60);
    EEPROM.write(PUMP_MAX_ON_DELAY, 10);
    EEPROM.write(PUMP_HIGH_MODE, 0);
    EEPROM.write(MOIST1_THRESHOLD, 60);
    EEPROM.write(MOIST2_THRESHOLD, 60);
    EEPROM.write(MOIST3_THRESHOLD, 60);
    EEPROM.write(MOIST4_THRESHOLD, 60);
  }
  moist_poll_time = EEPROM.read(MOIST_POLL_DELAY)*1000;
  timer_1_poll = EEPROM.read(TIMER_1_POLL_DELAY)*1000;
  com_poll = EEPROM.read(COM_POLL_DELAY)*10;
  pump_min_on_delay = EEPROM.read(PUMP_MIN_ON_DELAY);
  pump_high_on_delay = EEPROM.read(PUMP_HIGH_ON_DELAY);
  pump_max_on_delay = EEPROM.read(PUMP_MAX_ON_DELAY);
  pump_high_mode = EEPROM.read(PUMP_HIGH_MODE);
  threshold1 = EEPROM.read(MOIST1_THRESHOLD)*10;
  threshold2 = EEPROM.read(MOIST2_THRESHOLD)*10;
  threshold3 = EEPROM.read(MOIST3_THRESHOLD)*10;
  threshold4 = EEPROM.read(MOIST4_THRESHOLD)*10;
}
byte print_config(){
  int address = 0;
  byte value;
  do {
    value = EEPROM.read(address);
    Serial.print(address);
    Serial.print("\t");
    Serial.print(value, DEC);
    Serial.print(" - ");
    if(address == CONFIG_DONE) {Serial.println("CONFIG_DONE");}
    else if(address == MOIST_POLL_DELAY) {Serial.println("MOIST_POLL_DELAY");}
    else if(address == PUMP_MIN_ON_DELAY) {Serial.println("PUMP_MIN_ON_DELAY");}
    else if(address == PUMP_HIGH_ON_DELAY) {Serial.println("PUMP_HIGH_ON_DELAY");}
    else if(address == PUMP_MAX_ON_DELAY) {Serial.println("PUMP_MAX_ON_DELAY");}
    else if(address == PUMP_HIGH_MODE) {Serial.println("PUMP_HIGH_MODE");}
    else if(address == TIMER_1_POLL_DELAY) {Serial.println("TIMER_1_POLL_DELAY");}
    else if(address == COM_POLL_DELAY) {Serial.println("COM_POLL_DELAY");}
    else if(address == MOIST1_THRESHOLD) {Serial.println("MOIST1_THRESHOLD");}
    else if(address == MOIST2_THRESHOLD) {Serial.println("MOIST2_THRESHOLD");}
    else if(address == MOIST3_THRESHOLD) {Serial.println("MOIST3_THRESHOLD");}
    else if(address == MOIST4_THRESHOLD) {Serial.println("MOIST4_THRESHOLD");}
  // Use line below to add to the config list 
  //   else if(address == ) {Serial.println("");} 
    else{Serial.println("NOT ASSIGNED");}
    // advance to the next address of the EEPROM
    address = address + 1;
    // there are only 512 bytes of EEPROM, from 0 to 511, so if we're
    // on address 512, exit function
    if (address == 20)
      return 1;
     delay(5);
  } while (true);
}
void print_moists() {   // Print moists
        Serial.println(" ");
        Serial.println(" ");
        Serial.print("moist1 = ");
        Serial.println(int(Thermister(moist1_avg)));   
        Serial.print("moist2 = ");
        Serial.println(int(Thermister(moist2_avg)));
        Serial.print("moist3 = ");
        Serial.println(int(Thermister(moist3_avg)));   
        Serial.print("moist4 = ");
        Serial.println(int(Thermister(moist4_avg)));   
        Serial.println(" ");
}
 
void print_status() {  // Print status of controller
        Serial.println(" ");
        Serial.println(" ");
        Serial.print("moist1 = ");
        Serial.println(int(Thermister(moist1_avg)));   
        Serial.print("moist2 = ");
        Serial.println(int(Thermister(moist2_avg)));
        Serial.print("moist3 = ");
        Serial.println(int(Thermister(moist3_avg)));   
        Serial.print("moist4 = ");
        Serial.println(int(Thermister(moist4_avg)));   
        Serial.println(" ");
        Serial.print("threshold1 = ");
        Serial.println(int(Thermister(threshold1)));   
        Serial.print("threshold2 = ");
        Serial.println(int(Thermister(threshold2))); 
        Serial.print("threshold3 = ");
        Serial.println(int(Thermister(threshold3))); 
        Serial.print("threshold4 = ");
        Serial.println(int(Thermister(threshold4)));
        Serial.println(" ");
        Serial.println(" ");
        if (pump_high_mode) {Serial.println("Pump is in High Speed Mode");}
        else{Serial.println("Pump is in Low Speed Mode");}
        if (digitalRead(PUMP_LOW_ON) == HIGH) {Serial.println("Pump Low ON");}
        else{Serial.println("Pump low OFF");}
        if (digitalRead(PUMP_HIGH_ON) == HIGH) {Serial.println("Pump High ON");}
        else{Serial.println("Pump high OFF");}
        Serial.print("Pump Min On Delay (min) = ");
        Serial.println(pump_min_on_delay);
        Serial.print("Pump Max On Delay (min) = ");
        Serial.println(pump_max_on_delay);
        Serial.print("moist Polling (millisec) = ");
        Serial.println(moist_poll_time);          
        Serial.print("Timer 1 Polling (millisec) = ");
        Serial.println(timer_1_poll);          
        Serial.print("Com Polling (millisec) = ");
        Serial.println(com_poll);          
        Serial.println(" ");
}
 
void setup() {
  Serial.begin(115200);
  pinMode(MOIST1_SENSE_ON, OUTPUT);
  pinMode(MOIST2_SENSE_ON, OUTPUT);
  pinMode(MOIST3_SENSE_ON, OUTPUT);
  pinMode(MOIST4_SENSE_ON, OUTPUT);
  pinMode(PUMP_HIGH_ON, OUTPUT);
  pinMode(PUMP_LOW_ON, OUTPUT);
  init_config();
}
 
void loop() {
  if (abs(millis() - timer_1_millis) > timer_1_poll) { // Timer 1
   timer_1_millis = millis();   // remember the last time
    if (digitalRead(13) != HIGH){ //toggle LED to show that the program is running
      digitalWrite(13, HIGH);   // set the LED on
    }
    else { 
      digitalWrite(13, LOW);    // set the LED off
    }
    // Initialize measurement time
    if (val_cnt == 0) {digitalWrite(MOIST1_SENSE_ON, HIGH);digitalWrite(MOIST2_SENSE_ON, HIGH);digitalWrite(MOIST3_SENSE_ON, HIGH);digitalWrite(MOIST4_SENSE_ON, HIGH);}
    if (val_cnt == 6) {val_cnt ++; digitalWrite(MOIST1_SENSE_ON, LOW);digitalWrite(MOIST2_SENSE_ON, LOW);digitalWrite(MOIST3_SENSE_ON, LOW);digitalWrite(MOIST4_SENSE_ON, LOW);}
    if (val_cnt < 6) {
      moist1_val[val_cnt] = analogRead(MOIST1PIN);    // read the value from the sensors
      moist2_val[val_cnt] = analogRead(MOIST2PIN);
      moist3_val[val_cnt] = analogRead(MOIST3PIN);
      moist4_val[val_cnt] = analogRead(MOIST4PIN);
      val_cnt ++;
      moist1_avg = (moist1_val[0] + moist1_val[1] + moist1_val[2] + moist1_val[3] + moist1_val[4])/5; // Take 5 sec average of moisterature
      moist2_avg = (moist2_val[0] + moist2_val[1] + moist2_val[2] + moist2_val[3] + moist2_val[4])/5;
      moist3_avg = (moist3_val[0] + moist3_val[1] + moist3_val[2] + moist3_val[3] + moist3_val[4])/5;
      moist4_avg = (moist4_val[0] + moist4_val[1] + moist4_val[2] + moist4_val[3] + moist4_val[4])/5;
    }
  }
  if (abs(millis() - previousMillis) > moist_poll_time) {
    previousMillis = millis();   // remember the last time
    if (poll_status) {  //print controller status every poll cycle
       print_status();
    }
    if (poll_moists) {  //print controller moists every poll cycle
       print_moists();
    }
    val_cnt = 0; // Reset Moist measurement trigger
    if (abs(millis() - pump_on_start) > pump_max_on_delay * 60000 && digitalRead(PUMP_LOW_ON) != LOW) { 
     digitalWrite(PUMP_LOW_ON,LOW);
      Serial.println("Pump OFF");
    }
    if (abs(millis() - pump_on_start) > pump_min_on_delay*60000) { 
       if (moist1_avg >= threshold1 || moist2_avg >= threshold2 || moist3_avg >= threshold3 || moist4_avg >= threshold4){ 
       if (digitalRead(PUMP_LOW_ON) != HIGH) { // Check PUMP_HIGH_ON state
        digitalWrite(PUMP_LOW_ON,HIGH);
        Serial.println("Pump LOW ON");
       }
       pump_on_start = millis();
      } 
      else if (digitalRead(PUMP_LOW_ON) != LOW || digitalRead(PUMP_HIGH_ON) != LOW){ //Check PUMP_HIGH_ON state
       digitalWrite(PUMP_LOW_ON,LOW);
       digitalWrite(PUMP_HIGH_ON,LOW);
       Serial.println("Pump OFF");
      }
    }
  }
    if (abs(millis() - com_millis) > com_poll) {
    com_millis = millis();
     if (Serial.available()) {      // Look for char in serial que and process if found
      command = Serial.read();
      if (command == 84) {          // If command = "T" 
        command = Serial.read();
        if (command == 49) {          // If command = "1" print the MOIST1
         Serial.print("MOIST1 = ");
         Serial.print(int(Thermister(moist1_avg)));           // 
         Serial.println(" ");
        }
        else if (command == 50) {      // If command = "2" print the MOIST2
         Serial.print("MOIST2 = ");
         Serial.print(int(Thermister(moist2_avg)));           // 
         Serial.println(" ");
        }
        else if (command == 51) {      // If command = "3" print the MOIST3
         Serial.print("MOIST3 = ");
         Serial.print(int(Thermister(moist3_avg)));           // 
         Serial.println(" ");
        }
        else if (command == 52) {      // If command = "4" print the MOIST4
         Serial.print("MOIST4 = ");
         Serial.print(int(Thermister(moist4_avg)));           // 
         Serial.println(" ");
        }
      }
      else if (command == 72) {      //If command = "H" Print Controller Status
        Serial.println("Moisture Control v.1");
        Serial.println("A(0-1) - Manual Pump Low on command A1 is Pump Low on, A0 is Pump Low off");
        Serial.println("K(P,W,S) - Config Variables - P - Print, W - Write/Save Config, S(1-7,11-14) - Set Config Value");
        Serial.println("P(S,T,O) - Set constant poll for S - Controller Status, T - Moist Status, O - Poll off");
        Serial.println("S - System Status");
        Serial.println("T(1-4) - Moist1-4 Status ex. T1, T2, etc");
        Serial.println(" ");
      }
      else if (command == 83) {      //If command = "S" Print Controller Status
         print_status();
      }
            else if (command == 75) {      //If command = "K" Config operations
        command = Serial.read();
        if (command == 80) {           // If command = "P" Print Config
          print_config();
        }
        else if (command == 87) {           // If command = "W" save config
          save_config();
          Serial.println("Config Saved");
        }
        else if (command == 83) {           // If command = "S" save config
          command = Serial.parseInt();
            if(command == CONFIG_DONE) {Serial.read(); Serial.parseInt(); Serial.print(command); Serial.print(" CONFIG_DONE");}
            else if(command == MOIST_POLL_DELAY) {Serial.read();  moist_poll_time = Serial.parseInt(); Serial.print(command); Serial.print(" MOIST_POLL_DELAY");}
            else if(command == PUMP_MIN_ON_DELAY) {Serial.read(); pump_min_on_delay = Serial.parseInt(); Serial.print(command); Serial.print(" PUMP_MIN_ON_DELAY");}
            else if(command == PUMP_HIGH_ON_DELAY) {Serial.read(); pump_high_on_delay = Serial.parseInt(); Serial.print(command); Serial.print(" PUMP_HIGH_ON_DELAY");}
            else if(command == PUMP_MAX_ON_DELAY) {Serial.read(); pump_max_on_delay = Serial.parseInt(); Serial.print(command); Serial.print(" PUMP_MAX_ON_DELAY");}
            else if(command == PUMP_HIGH_MODE) {Serial.read(); pump_high_mode = Serial.parseInt(); Serial.print(command); Serial.print(" PUMP_HIGH_MODE");}
            else if(command == TIMER_1_POLL_DELAY) {Serial.read(); timer_1_poll = Serial.parseInt(); Serial.print(command); Serial.print(" TIMER_1_POLL_DELAY");}
            else if(command == COM_POLL_DELAY) {Serial.read(); com_poll = Serial.parseInt(); Serial.print(command); Serial.print(" COM_POLL_DELAY");}
            else if(command == MOIST1_THRESHOLD) {Serial.read(); threshold1 = Serial.parseInt(); Serial.print(command); Serial.print(" MOIST1_THRESHOLD");}
            else if(command == MOIST2_THRESHOLD) {Serial.read(); threshold2 = Serial.parseInt(); Serial.print(command); Serial.print(" MOIST2_THRESHOLD");}
            else if(command == MOIST3_THRESHOLD) {Serial.read(); threshold3 = Serial.parseInt(); Serial.print(command); Serial.print(" MOIST3_THRESHOLD");}
            else if(command == MOIST4_THRESHOLD) {Serial.read(); threshold4 = Serial.parseInt(); Serial.print(command); Serial.print(" MOIST4_THRESHOLD");}
          // Use line below to add to the config list threshold4
          //   else if(address == ) {Serial.println("");} 
            else{Serial.print(command); Serial.print("NOT ASSIGNED");}
            Serial.println(" - Config Set");
        }
      }
      else if (command == 80) {      //If command = "P" Poll commands
        command = Serial.read();
        if (command == 83) {           // If command = "S" set Status poll
            poll_status = 1;
            poll_moists = 0;
        }
        else if (command == 84) {           // If command = "T" set moist poll
            poll_status = 0;
            poll_moists = 1;
        }
        else if (command == 79) {           // If command = "O" Turn off poll
            poll_status = 0;
            poll_moists = 0;
        }
      }
 
// INSERT COMMAND ABOVE THIS LINE
     }
    }
  }
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox