Temperature Controller

From Combustory
Revision as of 15:49, 6 February 2009 by Jvaughters (Talk | contribs)
Jump to: navigation, search

Welcome to Combustory


Any questions or comments:

  • Send them to - combustor@combustory.com

Contents

Warning

This page is in progress and none of the code can be considered good or complete. I am just using this as an alternate storage of the code for right now.

Summary

Functional Description of the Method

Requirements

Example of Method

Quick Guide:

Detailed Guide:

I2C

X9241A - Digital Potentiometer

Detail on this page X9241A - Digital Potentiometer

There are basically only two functions that I am after right now which are both three byte instructions:

  • Read WCR(Wiper Control Register) for the four potentiometers 0-3 (So far this is failing miserably)
    • Send 1st byte - Address (my circuit - 01011010 or 0x2D)(binary - 0101 A3 A2 A1 A0) A values are actual pins set to high or low for an address.
    • Send 2nd byte - Instruction (binary - 1001 (1/0)(1/0) 00) - The two bits are used to choose the potentiometer
    • Read WCR requested Byte
  • Write WCR for the four potentiometers 0-3 (This works)
    • Send 1st byte - Address (my circuit - 01011010 or 0x2D)(binary - 0101 A3 A2 A1 A0) A values are actual pins set to high or low for an address.
    • Send 2nd byte - Instruction (binary - 1010 (1/0)(1/0) 00) - The two bits are used to choose the potentiometer
    • Send WCR Byte - Potentiometer value (binary - 00 D5 D4 D3 D2 D1 D0) - D values are the 0-63 potentiometer positions

Sample Code

       Wire.beginTransmission(X9241_I2C_ADDRESS);
       Wire.send(0xa0); //Instruction to write WCR-00
       Wire.send(0x20); //Send a D value of 32
       Wire.endTransmission();

DS1307 - Real Time Clock

The code for this very useful chip came from the [Glacial Wanderer]

A/C_Control_v.01 code for Arduino

While this says v.01, don't count on it being a released v.01, I am still working on v.01, this is a back up

//*
 * A/C Control v.01
 * by <http://www.combustory.com> John Vaughters
 * Credit to:
 * Maurice Ribble - http://www.glacialwanderer.com/hobbyrobotics for RTC DS1307 code
 *
 * Turns on an LED for temperatures from analog pins 1-5 on
 * digital pins 2-6 when the temperature rises above the THRESHOLDS 1-5.
 * The program also implements a 
 * Serial Communication method that utilizes a leading CHAR for each command Described below. 
 * Commands:
 * T(1-4) - Temp1-5 Status ex. T1, T2, etc
 * C(1-4)(0-9) - Increment THRESHOLD1-4 by (1-9) ex. C15 increments THRESHOLD1 BY 5 (Note: C40 will give you a status of THRESHOLD4)
 * D(1-4)(0-9) - Decrement THRESHOLD1-4 by (1-9) ex. D59 decrements THRESHOLD5 BY 9 (Note: D10 will give you a status of THRESHOLD1)
 * A(0-1) - Manual AC on command A1 is AC on, A0 is AC off
 * F(0-1) - Manual AC on command A1 is FAN on, A0 is FAN off 
 * B(00-59)(00-59)(00-23)(1-7)(01-31)(01-12)(00-99) - B(sec)(min)(hour)(dayOfWeek)(dayOfMonth)(month)(year) - B Sets the date of the RTC DS1307 Chip. ex. B1157193040209
 * R(0-4) - Need to Define this better it is for transferring X9241 registers
 * S - System Status
 * Q(1-2) - (1) Memory initialize  (2) RTC - Memory Dump
 */

#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68
#define X9241_I2C_ADDRESS 0x2D

// Global Variables
int val_cnt = 0;               // counter for the temp_val                   
int temp1Pin = 0;    // select the input pin for the Thermistor
int temp2Pin = 1;    // select the input pin for the Thermistor
int temp3Pin = 2;    // select the input pin for the Thermistor
int temp4Pin = 3;    // select the input pin for the Thermistor
int temp1_val[5] = {0,0,0,0,0};       // variable to store the value coming from the sensor
int temp2_val[5] = {0,0,0,0,0};       //           
int temp3_val[5] = {0,0,0,0,0};       //
int temp4_val[5] = {0,0,0,0,0};       //
int temp1_avg;                   // average over poll time of the temp values
int temp2_avg;
int temp3_avg;
int temp4_avg;
int duct1 = 2;                   // Ducts open or close using a digital output
int duct2 = 3;
int duct3 = 4;
int duct4 = 5;
int THRESHOLD1 = 580;    // Default theshold values
int THRESHOLD2 = 580;
int THRESHOLD3 = 580;
int THRESHOLD4 = 580;
int ac_on = 13;
int fan_on = 12;
int command = 0;       // This is the command char, in ascii form, sent from the serial port     
int i;
long polltime = 1000;  // The time to Poll the tempPins
long previousMillis = 0;        // will store last time Temp was updated
long ac_on_start = 0;     // Start A/C delay timer
long ac_on_delay = 10000;   // Time to wait before checking the ducts again
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
byte test; 
  
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return ( (val/16*10) + (val%16) );
}

// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers
// Watch for scope issues with Global Variables
// This function is ripe for bugs. Probably need to put in checks for valid numbers or the RTC will get hung up.
void setDateDs1307()                
{

   second = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); // Use of (byte) type casting and ascii math to achieve result.  
   minute = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
   hour  = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
   dayOfWeek = (byte) (Serial.read() - 48);
   dayOfMonth = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
   month = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
   year= (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
   Wire.beginTransmission(DS1307_I2C_ADDRESS);
   Wire.send(0x00);
   Wire.send(decToBcd(second));    // 0 to bit 7 starts the clock
   Wire.send(decToBcd(minute));
   Wire.send(decToBcd(hour));      // If you want 12 hour am/pm you need to set
                                   // bit 6 (also need to change readDateDs1307)
   Wire.send(decToBcd(dayOfWeek));
   Wire.send(decToBcd(dayOfMonth));
   Wire.send(decToBcd(month));
   Wire.send(decToBcd(year));
   Wire.endTransmission();
}

// Gets the date and time from the ds1307 and prints result
// Watch for scope issues with Global Variables
void getDateDs1307()
{
  // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0x00);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  // A few of these need masks because certain bits are control bits
  second     = bcdToDec(Wire.receive() & 0x7f);
  minute     = bcdToDec(Wire.receive());
  hour       = bcdToDec(Wire.receive() & 0x3f);  // Need to change this if 12 hour am/pm
  dayOfWeek  = bcdToDec(Wire.receive());
  dayOfMonth = bcdToDec(Wire.receive());
  month      = bcdToDec(Wire.receive());
  year       = bcdToDec(Wire.receive());
  
  Serial.print(hour, DEC);
  Serial.print(":");
  Serial.print(minute, DEC);
  Serial.print(":");
  Serial.print(second, DEC);
  Serial.print("  ");
  Serial.print(month, DEC);
  Serial.print("/");
  Serial.print(dayOfMonth, DEC);
  Serial.print("/");
  Serial.print(year, DEC);

}


void setup() {
  Wire.begin();
  Serial.begin(57600);
  pinMode(duct1, OUTPUT);
  pinMode(duct2, OUTPUT);
  pinMode(duct3, OUTPUT);
  pinMode(duct4, OUTPUT);
  pinMode(ac_on, OUTPUT);
  pinMode(fan_on, OUTPUT);

}

void loop() {
  if (millis() - previousMillis > polltime) {
    previousMillis = millis();   // remember the last time
    if (millis() - ac_on_start > ac_on_delay) { 
      if (digitalRead(duct1) || digitalRead(duct2) || digitalRead(duct3) || digitalRead(duct4)){  // If any ducts are turned on turn on the A/C
       if (digitalRead(ac_on) != HIGH) { // Check ac_on state
        digitalWrite(ac_on,HIGH);
        getDateDs1307();
        Serial.println(" - AC ON");
       }
       ac_on_start = millis();
      } 
      else if (digitalRead(ac_on) != LOW){ //Check ac_on state
       digitalWrite(ac_on,LOW);
       getDateDs1307();
       Serial.println(" - AC OFF");
      }
    }
    temp1_val[val_cnt] = analogRead(temp1Pin);    // read the value from the sensors
    temp2_val[val_cnt] = analogRead(temp2Pin);
    temp3_val[val_cnt] = analogRead(temp3Pin);
    temp4_val[val_cnt] = analogRead(temp4Pin);
    val_cnt ++;
    if (val_cnt == 5) {
      val_cnt = 0;
    }
    temp1_avg = (temp1_val[0] + temp1_val[1] + temp1_val[2] + temp1_val[3] + temp1_val[4])/5; // Take 5 sec average of temperature
    temp2_avg = (temp2_val[0] + temp2_val[1] + temp2_val[2] + temp2_val[3] + temp2_val[4])/5;
    temp3_avg = (temp3_val[0] + temp3_val[1] + temp3_val[2] + temp3_val[3] + temp3_val[4])/5;
    temp4_avg = (temp4_val[0] + temp4_val[1] + temp4_val[2] + temp4_val[3] + temp4_val[4])/5;
// Check Thresholds against the Temperatures and set the ducts HIGH or LOW    
    if (temp1_avg >= THRESHOLD1) {digitalWrite(duct1, HIGH);}
    else {digitalWrite(duct1, LOW);}
    if (temp2_avg >= THRESHOLD2) {digitalWrite(duct2, HIGH);}
    else {digitalWrite(duct2, LOW);}
    if (temp3_avg >= THRESHOLD3) {digitalWrite(duct3, HIGH);}
    else {digitalWrite(duct3, LOW);}
    if (temp4_avg >= THRESHOLD4) {digitalWrite(duct4, HIGH);}
    else {digitalWrite(duct4, LOW);}
    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 Temp1
         Serial.print("Temp1 = ");
         Serial.print(temp1_avg);           // 
         Serial.print(" ");
        }
        else if (command == 50) {      // If command = "2" print the Temp2
         Serial.print("Temp2 = ");
         Serial.print(temp2_avg);           // 
         Serial.print(" ");
        }
        else if (command == 51) {      // If command = "3" print the Temp3
         Serial.print("Temp3 = ");
         Serial.print(temp3_avg);           // 
         Serial.print(" ");
        }
        else if (command == 52) {      // If command = "4" print the Temp4
         Serial.print("Temp4 = ");
         Serial.print(temp4_avg);           // 
         Serial.print(" ");
         }
       }
      else if (command == 67) {      //If command = "C" Change Temp Threshhold
        if (Serial.available()) {
         command = Serial.read();
         if (command == 49) {        // If command = "1" print the THRESHOLD1
           command = Serial.read();
           if (command > 47 && command < 58) {          // If command is between 0-9 Increment the Threshold by number sent
             THRESHOLD1 += command - 48;                 // ASCII math to get value sent   
                  Serial.print("THRESHOLD1 = ");
                  Serial.print(THRESHOLD1);           // 
                  Serial.print(" ");
           }
         }  
         else if (command == 50) {     // If command = "2" print the THRESHOLD2
           command = Serial.read();
           if (command > 47 && command < 58) {          // If command is between 0-9 Increment the Threshold by number sent
             THRESHOLD2 += command - 48;                 // ASCII math to get value sent   
                  Serial.print("THRESHOLD2 = ");
                  Serial.print(THRESHOLD2);           // 
                  Serial.print(" ");
           }
         }
         else if (command == 51) {             // If command = "3" print the THRESHOLD3
           command = Serial.read();
           if (command > 47 && command < 58) {          // If command is between 0-9 Increment the Threshold by number sent
             THRESHOLD3 += command - 48;                 // ASCII math to get value sent   
                  Serial.print("THRESHOLD3 = ");
                  Serial.print(THRESHOLD3);           // 
                  Serial.print(" ");
           }
         }
         else if (command == 52) {             // If command = "4" print the THRESHOLD4
           command = Serial.read();
           if (command > 47 && command < 58) {          // If command is between 0-9 Increment the Threshold by number sent
             THRESHOLD4 += command - 48;                 // ASCII math to get value sent   
                  Serial.print("THRESHOLD4 = ");
                  Serial.print(THRESHOLD4);           // 
                  Serial.print(" ");
           }
         }          
        }  
      }
      else if (command == 68) {      //If command = "D" Change Temp Threshhold
        if (Serial.available()) {
         command = Serial.read();
         if (command == 49) {           // If command = "1" print the THRESHOLD1
           command = Serial.read();
           if (command > 47 && command < 58) {          // If command is between 0-9 Decrement the Threshold by number sent
             THRESHOLD1 -= command - 48;                 // ASCII math to get value sent   
                  Serial.print("THRESHOLD1 = ");
                  Serial.print(THRESHOLD1);           // 
                  Serial.print(" ");
           }
         }  
         else if (command == 50) {               // If command = "2" print the THRESHOLD2
           command = Serial.read();
           if (command > 47 && command < 58) {          // If command is between 0-9 Decrement the Threshold by number sent
             THRESHOLD2 -= command - 48;                 // ASCII math to get value sent   
                  Serial.print("THRESHOLD2 = ");
                  Serial.print(THRESHOLD2);           // 
                  Serial.print(" ");
           }
         }
         else if (command == 51) {              // If command = "3" print the THRESHOLD3
           command = Serial.read();
           if (command > 47 && command < 58) {          // If command is between 0-9 Decrement the Threshold by number sent
             THRESHOLD3 -= command - 48;                 // ASCII math to get value sent   
                  Serial.print("THRESHOLD3 = ");
                  Serial.print(THRESHOLD3);           // 
                  Serial.print(" ");
           }
         }
         else if (command == 52) {                    // If command = "4" print the THRESHOLD4
           command = Serial.read();
           if (command > 47 && command < 58) {          // If command is between 0-9 Decrement the Threshold by number sent
             THRESHOLD4 -= command - 48;                 // ASCII math to get value sent   
                  Serial.print("THRESHOLD4 = ");
                  Serial.print(THRESHOLD4);           // 
                  Serial.print(" ");
           }
         }           
        }  
      }
//****************  Warning - This is a potential for problem - Consider a manual lock out feature to lock out manual commands 
//****************  Possibly create a command to open up manual commands for a certian time period then shut them off again automatically
      else if (command == 65) {      //If command = "A" Change Temp Threshhold
       if (Serial.available()) {
        command = Serial.read();
        if (command == 49) {        // If command = "1" print the AC ON message
         getDateDs1307();
         Serial.print(" - Manual AC ON ");
         ac_on_start = millis();   // Set the AC to a delay before it can be turned off again
         digitalWrite(ac_on,HIGH);
        }
        else if (command == 48) {        // If command = "0" print the AC OFF message
         getDateDs1307();
         Serial.print(" - Manual AC OFF ");
         digitalWrite(ac_on,LOW);
        }
       } 
      } 
      else if (command == 70) {      //If command = "F" Change Temp Threshhold
       if (Serial.available()) {
        command = Serial.read();
        if (command == 49) {        // If command = "1" print the FAN ON message
         getDateDs1307();
         Serial.print(" - Manual FAN ON ");
         ac_on_start = millis();
         digitalWrite(fan_on,HIGH);
        }
        else if (command == 48) {        // If command = "0" print the FAN OFF message
         getDateDs1307();
         Serial.print(" - Manual FAN OFF ");
         digitalWrite(fan_on,LOW);
        }
       } 
      }
//********************** End of Warning Zone *******************************

// ***************  This Section Will list the Staus of the Controller
      else if (command == 83) {      //If command = "S" Print Controller Status
        getDateDs1307();
        Serial.println(" ");
        Serial.println(" ");
        Serial.print("Temp1 = ");
        Serial.println(temp1_avg);   
        Serial.print("Temp2 = ");
        Serial.println(temp2_avg);
        Serial.print("Temp3 = ");
        Serial.println(temp3_avg);   
        Serial.print("Temp4 = ");
        Serial.println(temp4_avg);   
        Serial.println(" ");
        Serial.print("THRESHOLD1 = ");
        Serial.println(THRESHOLD1);   
        Serial.print("THRESHOLD2 = ");
        Serial.println(THRESHOLD2); 
        Serial.print("THRESHOLD3 = ");
        Serial.println(THRESHOLD3); 
        Serial.print("THRESHOLD4 = ");
        Serial.println(THRESHOLD4);
        Serial.println(" ");
        if (digitalRead(duct1) == HIGH) {Serial.println("duct1 ON");}
        else{Serial.println("duct1 OFF");}
        if (digitalRead(duct2) == HIGH) {Serial.println("duct2 ON");}
        else{Serial.println("duct2 OFF");}
        if (digitalRead(duct3) == HIGH) {Serial.println("duct3 ON");}
        else{Serial.println("duct3 OFF");}
        if (digitalRead(duct4) == HIGH) {Serial.println("duct4 ON");}
        else{Serial.println("duct4 OFF");}
        Serial.println(" ");
        if (digitalRead(fan_on) == HIGH) {Serial.println("Fan ON");}
        else{Serial.println("Fan OFF");}
        if (digitalRead(ac_on) == HIGH) {Serial.println("AC  ON");}
        else{Serial.println("AC  OFF");}
        Serial.print("A/C Delay (millisec) = ");
        Serial.println(ac_on_delay);
        Serial.print("Temp Polling (millisec) = ");
        Serial.println(polltime);          
        Serial.println(" ");
        
      }
      else if (command == 66) {      //If command = "B" Set Date
       setDateDs1307();
       getDateDs1307();
       Serial.println(" ");
      }
       else if (command == 81) {      //If command = "Q" RTC1307 Functions
       
        // Wire.beginTransmission(DS1307_I2C_ADDRESS);
       // Wire.send(0x0f);
       // Wire.send(0xff);
       // Wire.endTransmission();
        if (Serial.available()) {
         command = Serial.read(); 
         if (command == 49) {      //If command = "1" RTC1307 Initialize Memory - All Data will be set to 1.  Therefore 1 or 0 will not be a valid value.  
          Wire.beginTransmission(DS1307_I2C_ADDRESS); // 1 will be the init value and 0 will be cosidered an error that occurs when the RTC is in Battery mode.
          Wire.send(0x08);
         for (i = 1; i <= 27; i++) {
             Wire.send(0xff);
            delay(100);
         }   
         Wire.endTransmission();
         getDateDs1307();
         Serial.println(": RTC1307 Initialized Memory");
         }
         else if (command == 50) {      //If command = "2" RTC1307 Memory Dump
          getDateDs1307();
          Serial.println(": RTC 1307 Dump Begin");
          Wire.beginTransmission(DS1307_I2C_ADDRESS);
          Wire.send(0x00);
          Wire.endTransmission();
          Wire.requestFrom(DS1307_I2C_ADDRESS, 65);
          for (i = 1; i <= 65; i++) {
             test = Wire.receive();
             Serial.print(i);
             Serial.print(":");
             Serial.println(test, DEC);
          }
          Wire.endTransmission();
          Serial.println(" RTC1307 Dump end");
         } 
        }  
      }
      else if (command == 82) {      //If command = "R" Set Duct Position
       if (Serial.available()) {
        command = Serial.read(); 
        // This code sets up the Persistent Registers and is not necessarily needed all the time, so can be removed from program eventually
        if (command == 49) {        // If command = "1" 
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xc0);  // This is the set register command c and the 16 registers are 0-f
          Wire.send(0x0a);  // This line is the value to set the register
          Wire.endTransmission(); 
          delay(100);
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xc1);
          Wire.send(0x14);
          Wire.endTransmission();
          delay(100);
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xc2);
          Wire.send(0x20);
          Wire.endTransmission();
          delay(100);
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xc3);
          Wire.send(0x3c);
          Wire.endTransmission();
          delay(100);
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xc4);
          Wire.send(0x0a);
          Wire.endTransmission(); 
          delay(100);
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xc5);
          Wire.send(0x14);
          Wire.endTransmission();
          delay(100);
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xc6);
          Wire.send(0x20);
          Wire.endTransmission();
          delay(100);
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xc7);
          Wire.send(0x3c);
          delay(100);          
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xc8);
          Wire.send(0x0a);
          Wire.endTransmission(); 
          delay(100);
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xc9);
          Wire.send(0x14);
          Wire.endTransmission();
          delay(100);
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xca);
          Wire.send(0x20);
          Wire.endTransmission();
          delay(100);
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xcb);
          Wire.send(0x3c);
          Wire.endTransmission();
          delay(100);
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xcc);
          Wire.send(0x0a);
          Wire.endTransmission(); 
          delay(100);
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xcd);
          Wire.send(0x14);
          Wire.endTransmission();
          delay(100);
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xce);
          Wire.send(0x20);
          Wire.endTransmission();
          delay(100);
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xcf);
          Wire.send(0x3c);
          Wire.endTransmission();
         } 
         else if (command == 50) {        // If command = "2" 
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0x10); // Global Load WCR's with Reg-00
          Wire.endTransmission();
         }
         else if (command == 51) {        // If command = "3" 
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0x11); // Global Load WCR's with Reg-01
          Wire.endTransmission();
         }
         else if (command == 52) {        // If command = "4" 
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0x12); // Global Load WCR's with Reg-10
          Wire.endTransmission();
         }
         else if (command == 53) {        // If command = "5" 
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0x13); // Global Load WCR's with Reg-11
          Wire.endTransmission();
         }
       }
     
       //***********  Read operation just not working :~(
       //Wire.beginTransmission(X9241_I2C_ADDRESS);
       //Wire.send(0x90);
       //Serial.println("made it 1");
      // Wire.endTransmission();
       //Serial.println(test, DEC);
       //Wire.beginTransmission(X9241_I2C_ADDRESS);
       //Wire.requestFrom(X9241_I2C_ADDRESS, 1); 
       //Wire.send(0x90);
       //Serial.println("made it 2");
       //test = bcdToDec(Wire.receive());
       //test = Wire.receive();
       //Serial.println(test, DEC);
       //Serial.println(Wire.available());
       //Wire.endTransmission();
       //Serial.println(test, DEC);
       

      }
      Serial.println(command);     // Echo command char found in serial que
      command = 0;                 // reset command 
    }
  }
}
//*****************************************************The End***********************


A/C_Control_v.02 code for AutoIT

While this says v.02, don't count on it being a released v.02, I am still working on v.02, this is a back up

; Arduino Communications and Control
; 2 Sep 2008 - John Vaughters <http://www.combustory.com>
; This is a Utility to Communicate with the Arduino Control board for managing the Home Environment
#include <WindowsConstants.au3>
#include <GuiConstants.au3>
#include <GuiEdit.au3> 
#include <file.au3>
#include <Date.au3>
#include <IE.au3> 
 ;Initialize Variable Defaults
$working_dir = @DesktopDir & "\"
$working_file = "arduino_log"
$poll_delay = 5000                             ; This is the delay the program waits before collecting the command  results
;$network_dir = "P:\sketchbook\ArduinoSerial\" 
$network_dir = "S:\"                          ; Change this to default before you release the SW
$process_results = False ;Processing Flag
$command_get_begin = TimerInit() ;Initialize Timer
; GUI
GuiCreate(" A/C Control", 700, 600)
; MENU 
$filemenu = GuiCtrlCreateMenu("&File")
$fileitem = GUICtrlCreateMenuitem ("Open",$filemenu)
GUICtrlSetState(-1,$GUI_DEFBUTTON)
$exititem = GUICtrlCreateMenuitem ("Exit",$filemenu)
$helpmenu = GuiCtrlCreateMenu("Help")
$infoitem = GUICtrlCreateMenuitem ("Info",$helpmenu)
; LOGO PIC
GuiCtrlCreatePic("logo.jpg",0,0, 100,140)
; AVI for letting the user know the system is processing
$processing = GuiCtrlCreateAvi("sampleAVI.avi",0, 405, 140, 32, 32)
; Tabbed Result Window
$tab_result_start_x = 20
$tab_result_start_y = 175
$tab_result_size_x = 650
$tab_result_size_y = 400
$tab_result_title_2 = "Command Results"
$tab_result_title_3 = "Status"
$tab_result_title_4 = "Sys Info"
GuiCtrlCreateTab($tab_result_start_x, $tab_result_start_y, $tab_result_size_x , $tab_result_size_y)
GuiCtrlCreateTabItem($tab_result_title_2)
$edit_ctl_tab2 = GuiCtrlCreateEdit(@CRLF & "", $tab_result_start_x + 10 , $tab_result_start_y + 40, $tab_result_size_x - 20, $tab_result_size_y - 50)
GuiCtrlCreateTabItem($tab_result_title_3)
$edit_ctl_tab3 = GuiCtrlCreateEdit(@CRLF & "", $tab_result_start_x + 10 , $tab_result_start_y + 40, $tab_result_size_x - 20, $tab_result_size_y - 50)
GuiCtrlCreateTabItem($tab_result_title_4)
$edit_ctl_tab4 = GuiCtrlCreateEdit(@CRLF & "", $tab_result_start_x + 10 , $tab_result_start_y + 40, $tab_result_size_x - 20, $tab_result_size_y - 50)
GuiCtrlCreateTabItem("")
; Combo Arduino Command File Type
$combo_ctl_search_file = GuiCtrlCreatecombo("C10", 240, 145, 120, 100)
GUICtrlSetData(-1,"C15|D15|T1|","C10") ; add other item snd set a new default
GuiCtrlCreateLabel("Custom Command", 245, 170, 150, 20)
; Temp Labels
$ctl_temp1_lbl = GuiCtrlCreateLabel("", 180, 25, 100, 22,$WS_DLGFRAME)
GUICtrlSetBkColor(-1,0xffffff)
$ctl_temp2_lbl = GuiCtrlCreateLabel("", 180, 50, 100, 22,$WS_DLGFRAME)
GUICtrlSetBkColor(-1,0xffffff)
$ctl_temp3_lbl = GuiCtrlCreateLabel("", 180, 75, 100, 22,$WS_DLGFRAME)
GUICtrlSetBkColor(-1,0xffffff)
$ctl_temp4_lbl = GuiCtrlCreateLabel("", 180, 100, 100, 22,$WS_DLGFRAME)
GUICtrlSetBkColor(-1,0xffffff)
$ctl_thresh1_lbl = GuiCtrlCreateLabel("", 300, 25, 150, 22,$WS_DLGFRAME)
GUICtrlSetBkColor(-1,0xffffff)
$ctl_thresh2_lbl = GuiCtrlCreateLabel("", 300, 50, 150, 22,$WS_DLGFRAME)
GUICtrlSetBkColor(-1,0xffffff)
$ctl_thresh3_lbl = GuiCtrlCreateLabel("", 300, 75, 150, 22,$WS_DLGFRAME)
GUICtrlSetBkColor(-1,0xffffff)
$ctl_thresh4_lbl = GuiCtrlCreateLabel("", 300, 100, 150, 22,$WS_DLGFRAME)
GUICtrlSetBkColor(-1,0xffffff)
; Current Network Directory Label
;$combo_ctl_network_dir = GuiCtrlCreateLabel($network_dir, 240, 20, 450, 22,$WS_DLGFRAME)
;GUICtrlSetBkColor(-1,0xffffff)
;GuiCtrlCreateLabel("Network Directory", 245, 45, 200, 20)
; BUTTON
$search_btn = GuiCtrlCreateButton("Go", 370, 143, 25, 25)
;$file_btn = GuiCtrlCreateButton("Working Dir", 125, 77, 100, 25)
;$network_btn = GuiCtrlCreateButton("Network Dir", 125, 18, 100, 25)
$site_btn = GuiCtrlCreateButton("www.combustory.com", 0, 145, 135, 22)
$clear_btn = GuiCtrlCreateButton("Clear", 620, 170, 50, 22)
$temp1_btn = GuiCtrlCreateButton("Temp 1", 120, 25, 50, 22)
$temp2_btn = GuiCtrlCreateButton("Temp 2", 120, 50, 50, 22)
$temp3_btn = GuiCtrlCreateButton("Temp 3", 120, 75, 50, 22)
$temp4_btn = GuiCtrlCreateButton("Temp 4", 120, 100, 50, 22)
$poll_btn = GuiCtrlCreateButton("Poll", 120, 0, 50, 22)
$ac_btn = GuiCtrlCreateButton("A/C ON", 500, 75, 60, 22)
$fan_btn = GuiCtrlCreateButton("Fan ON", 500, 100, 60, 22)
$status_btn = GuiCtrlCreateButton("Status", 500, 125, 60, 22)
$set_time_btn = GuiCtrlCreateButton("Set Time", 500, 150, 60, 22)
; List System Info
GuiCtrlSetData($edit_ctl_tab4, "Computer:             " & @CRLF & _
								"----------------------------------------------------------" & @CRLF & _
								"IP Address:             " & @IPAddress1 & @CRLF & _
								"Computer Name:     " & @ComputerName & @CRLF & _
								"OS:                         " & @OSVersion & @CRLF & _
								"Sys Dir:                   " & @SystemDir & @CRLF & @CRLF)
;*************
;  _Load_Results() loads the temp file into the desired control then deletes the temp file
; $w_dir is the working directory where the file exists
; $ctl_to_load is the place to load the file contents
; $temp_file is the temporary file to load the data from
; The function returns the number of lines loaded from the file or a zero for a failure
Func _Load_Results ($w_dir, $ctl_load_to, $temp_file)
	Dim $aRecords
	If Not _FileReadToArray($w_dir & $temp_file,$aRecords) Then
		 Return (0) ; Failure to read a file
	EndIf
	For $x = 1 to $aRecords[0]
		if StringLen($aRecords[$x]) > 0 then GuiCtrlSetData($ctl_load_to, $aRecords[$x] & @CRLF, 1)
	Next
		;Delete the temporary file
	RunWait(@ComSpec & " /c " & "del " & $temp_file, $w_dir & "",@SW_HIDE)
	Return ($aRecords[0]-1)
EndFunc
;***********************
; _Load_Status_Messages() loads the messages that were logged by the control board into the command window
; The variables seem to be global at this point, that should be changed though to make it a self contained func
Func _Load_Status_Messages ()
		RunWait(@ComSpec & " /c " & "type arduino_log>results.txt", $network_dir,@SW_HIDE)
		; Load command results into Command Results tab
		GuiCtrlSetData($edit_ctl_tab2, "------------------------------------------------------------------" & @CRLF,1)
		GuiCtrlSetData($edit_ctl_tab2, "Status Messages:  " & @CRLF,1)
		If Not _Load_Results ($network_dir, $edit_ctl_tab2, "results.txt") Then
			GuiCtrlSetData($edit_ctl_tab2, "No Status Messages!  " & @CRLF,1)
		EndIf	
		GuiCtrlSetData($edit_ctl_tab2, "******************************************************************" & @CRLF,1)
		RunWait(@ComSpec & " /c " & "echo>clear_log", $network_dir,@SW_HIDE) ; Send message to Linux polling script to clear the log
		Sleep (1000)
EndFunc
;*************
;  _Send_Command() Sends a command to the control board
; $w_dir is the working directory where the file exists
; $n_dir is the network directory where the to copy the command file
; $cmd is the command to send the control board
Func _Send_Command ($w_dir, $n_dir, $cmd)
	$file = FileOpen($w_dir & "command", 2)
		; Check if file opened OK
		If $file = -1 Then
			MsgBox(0, "Error", "Unable to open file.")
			Exit
		EndIf
		FileWrite($file, $cmd)
		FileClose($file)  ; The File must be closed before you can copy it anywhere
		RunWait(@ComSpec & " /c " & "copy command " & $n_dir, $w_dir,@SW_HIDE)
EndFunc
; GUI MESSAGE LOOP
GuiSetState()
; Main Event Loop
While 1
  ; After every loop check if the user clicked something in the GUI window
   $msg = GUIGetMsg()
    if $process_results = True Then
		if $poll_delay < TimerDiff($command_get_begin) Then
			RunWait(@ComSpec & " /c " & "type arduino_log>results.txt", $network_dir,@SW_HIDE)
			; Load command results into Command Results tab
			GuiCtrlSetData($edit_ctl_tab2, "------------------------------------------------------------------" & @CRLF,1)
			GuiCtrlSetData($edit_ctl_tab2, "Command:  " & GUICtrlRead($combo_ctl_search_file) & @CRLF,1)
			GuiCtrlSetData($edit_ctl_tab2, "Results:  " & @CRLF,1)
			_Load_Results ($network_dir, $edit_ctl_tab2, "results.txt")
			GuiCtrlSetData($edit_ctl_tab2, "******************************************************************" & @CRLF,1)
			$process_results = False
			RunWait(@ComSpec & " /c " & "echo>clear_log", $network_dir,@SW_HIDE) ; Send message to Linux polling script to clear the log
			GUICtrlSetState ($processing, 0)
		EndIf
	EndIf	
   Select
    ; Check if user clicked on the close button
	Case $msg = $GUI_EVENT_CLOSE Or $msg = $exititem
        ; Destroy the GUI including the controls
         GUIDelete()
        ; Exit the script
         Exit
	Case $msg = $infoitem
		MsgBox(64, "Info", "Envrionmental Control v0.2" & @CRLF & "By: John Vaughters")
	Case $msg = $status_btn
		; Start processing AVI 
		GUICtrlSetState ($processing, 1)
		_GUICtrlEdit_SetSel ($edit_ctl_tab3, 0, -1) 
		_GUICtrlEdit_ReplaceSel ($edit_ctl_tab3, "")
		_Load_Status_Messages () 
		_Send_Command ($working_dir, $network_dir, "S" )
		Sleep (2000)
		RunWait(@ComSpec & " /c " & "type arduino_log>results.txt", $network_dir,@SW_HIDE)
		_Load_Results ($network_dir, $edit_ctl_tab3, "results.txt")	
		RunWait(@ComSpec & " /c " & "echo>clear_log", $network_dir,@SW_HIDE) ; Send message to Linux polling script to clear the log
		GUICtrlSetState ($processing, 0)
	Case $msg = $set_time_btn ; Set the date/time on the RTC 1307 chip
		$strTime = _Date_Time_GetLocalTime()
		$Time_ary = _Date_Time_SystemTimeToArray($strTime)
		GuiCtrlSetData($edit_ctl_tab2, "B" & StringFormat("%02d%02d%02d%1d%02d%02d%02d", $Time_ary[5], $Time_ary[4], $Time_ary[3], $Time_ary[7], $Time_ary[1], $Time_ary[0], StringTrimLeft($Time_ary[2], 2)) & @CRLF,1)
		_Load_Status_Messages () 
		_Send_Command ($working_dir, $network_dir, "B" & StringFormat("%02d%02d%02d%1d%02d%02d%02d", $Time_ary[5], $Time_ary[4], $Time_ary[3], $Time_ary[7], $Time_ary[1], $Time_ary[0], StringTrimLeft($Time_ary[2], 2)) )
	Case $msg = $fan_btn
		; Toggle ON/OFF State
		if GUICtrlRead($fan_btn) = "Fan ON" Then
			GUICtrlSetData ($fan_btn, "Fan OFF")
			_Send_Command ($working_dir, $network_dir, "F1" )
		ElseIf GUICtrlRead($fan_btn) = "Fan OFF" Then
			GUICtrlSetData ($fan_btn, "Fan ON")
			_Send_Command ($working_dir, $network_dir, "F0" )
		EndIf
	Case $msg = $ac_btn
		; Toggle ON/OFF State 
		if GUICtrlRead($ac_btn) = "A/C ON" Then
			GUICtrlSetData ($ac_btn, "A/C OFF")
			_Send_Command ($working_dir, $network_dir, "A1" )
		ElseIf GUICtrlRead($ac_btn) = "A/C OFF" Then
			GUICtrlSetData ($ac_btn, "A/C ON")
			_Send_Command ($working_dir, $network_dir, "A0" )
		EndIf
	Case $msg = $temp1_btn
		; Start processing AVI 
		GUICtrlSetState ($processing, 1)
		_Load_Status_Messages ()
		_Send_Command ($working_dir, $network_dir, "T1D10" )
		Sleep (3000)
		RunWait(@ComSpec & " /c " & "type arduino_log>results.txt", $network_dir,@SW_HIDE)
		$file = FileOpen($network_dir & "results.txt", 0)
		If $file = -1 Then
			MsgBox(0, "Error", "Unable to open file.")
			Exit
		EndIf
		GuiCtrlSetData($ctl_temp1_lbl, FileReadLine($file,2))
		GuiCtrlSetData($ctl_thresh1_lbl, FileReadLine($file,3))
		FileClose($file)
		RunWait(@ComSpec & " /c " & "del " & "results.txt", $network_dir & "",@SW_HIDE)
		RunWait(@ComSpec & " /c " & "echo>clear_log", $network_dir,@SW_HIDE) ; Send message to Linux polling script to clear the log
		GUICtrlSetState ($processing, 0)
	Case $msg = $temp2_btn
		; Start processing AVI 
		GUICtrlSetState ($processing, 1)
		_Load_Status_Messages ()
		_Send_Command ($working_dir, $network_dir, "T2D20" )
		Sleep (3000)
		RunWait(@ComSpec & " /c " & "type arduino_log>results.txt", $network_dir,@SW_HIDE)
		$file = FileOpen($network_dir & "results.txt", 0)
		If $file = -1 Then
			MsgBox(0, "Error", "Unable to open file.")
			Exit
		EndIf
		GuiCtrlSetData($ctl_temp2_lbl, FileReadLine($file,2))
		GuiCtrlSetData($ctl_thresh2_lbl, FileReadLine($file,3))
		FileClose($file)
		RunWait(@ComSpec & " /c " & "del " & "results.txt", $network_dir & "",@SW_HIDE)
		RunWait(@ComSpec & " /c " & "echo>clear_log", $network_dir,@SW_HIDE) ; Send message to Linux polling script to clear the log
		GUICtrlSetState ($processing, 0)
	Case $msg = $temp3_btn
		; Start processing AVI 
		GUICtrlSetState ($processing, 1)
		_Load_Status_Messages ()
		_Send_Command ($working_dir, $network_dir, "T3D30" )
		Sleep (3000)
		RunWait(@ComSpec & " /c " & "type arduino_log>results.txt", $network_dir,@SW_HIDE)
		$file = FileOpen($network_dir & "results.txt", 0)
		If $file = -1 Then
			MsgBox(0, "Error", "Unable to open file.")
			Exit
		EndIf
		GuiCtrlSetData($ctl_temp3_lbl, FileReadLine($file,2))
		GuiCtrlSetData($ctl_thresh3_lbl, FileReadLine($file,3))
		FileClose($file)
		RunWait(@ComSpec & " /c " & "del " & "results.txt", $network_dir & "",@SW_HIDE)
		RunWait(@ComSpec & " /c " & "echo>clear_log", $network_dir,@SW_HIDE) ; Send message to Linux polling script to clear the log
		GUICtrlSetState ($processing, 0)	
	Case $msg = $temp4_btn
		; Start processing AVI 
		GUICtrlSetState ($processing, 1)
		_Load_Status_Messages ()
		_Send_Command ($working_dir, $network_dir, "T4D40" )
		Sleep (3000)
		RunWait(@ComSpec & " /c " & "type arduino_log>results.txt", $network_dir,@SW_HIDE)
		$file = FileOpen($network_dir & "results.txt", 0)
		If $file = -1 Then
			MsgBox(0, "Error", "Unable to open file.")
			Exit
		EndIf
		GuiCtrlSetData($ctl_temp4_lbl, FileReadLine($file,2))
		GuiCtrlSetData($ctl_thresh4_lbl, FileReadLine($file,3))
		FileClose($file)
		RunWait(@ComSpec & " /c " & "del " & "results.txt", $network_dir & "",@SW_HIDE)
		RunWait(@ComSpec & " /c " & "echo>clear_log", $network_dir,@SW_HIDE) ; Send message to Linux polling script to clear the log
		GUICtrlSetState ($processing, 0)
	Case $msg = $poll_btn ; Eventually this should be a toggle button to poll data  every certian period of time
		; Start processing AVI 
		GUICtrlSetState ($processing, 1)
		_Load_Status_Messages ()
		_Send_Command ($working_dir, $network_dir, "T1T2T3T4D10D20D30D40" )
		Sleep (9000)
		RunWait(@ComSpec & " /c " & "type arduino_log>results.txt", $network_dir,@SW_HIDE)
		$file = FileOpen($network_dir & "results.txt", 0)
		If $file = -1 Then
			MsgBox(0, "Error", "Unable to open file.")
			Exit
		EndIf
		GuiCtrlSetData($ctl_temp1_lbl, FileReadLine($file,2))
		GuiCtrlSetData($ctl_temp2_lbl, FileReadLine($file,3))
		GuiCtrlSetData($ctl_temp3_lbl, FileReadLine($file,4))
		GuiCtrlSetData($ctl_temp4_lbl, FileReadLine($file,5))
		GuiCtrlSetData($ctl_thresh1_lbl, FileReadLine($file,6))
		GuiCtrlSetData($ctl_thresh2_lbl, FileReadLine($file,7))
		GuiCtrlSetData($ctl_thresh3_lbl, FileReadLine($file,8))
		GuiCtrlSetData($ctl_thresh4_lbl, FileReadLine($file,9))
		FileClose($file)
		RunWait(@ComSpec & " /c " & "del " & "results.txt", $network_dir & "",@SW_HIDE)
		RunWait(@ComSpec & " /c " & "echo>clear_log", $network_dir,@SW_HIDE) ; Send message to Linux polling script to clear the log
		GUICtrlSetState ($processing, 0)
	Case $msg = $search_btn
		; Start processing AVI 
		GUICtrlSetState ($processing, 1)
		$file = FileOpen($working_dir & "command", 2)
		; Check if file opened for reading OK
		If $file = -1 Then
			MsgBox(0, "Error", "Unable to open file.")
			Exit
		EndIf
		; Set File Type
		$working_file = GUICtrlRead($combo_ctl_search_file)
		FileWrite($file, $working_file)
		FileClose($file)  ; The File must be closed before you can copy it anywhere
		RunWait(@ComSpec & " /c " & "copy command " & $network_dir, $working_dir,@SW_HIDE)
		$command_get_begin = TimerInit() ;Start the timer until you can retrieve your results
		$process_results = True ; Set Processing flag
	Case $msg = $site_btn
		_IECreate ("www.combustory.com")
	Case $msg = $clear_btn
		_GUICtrlEdit_SetSel ($edit_ctl_tab2, 0, -1) 
		_GUICtrlEdit_ReplaceSel ($edit_ctl_tab2, "")
   EndSelect
WEnd

A/C_Control_v.01 User Guide

Commands

Troubleshooting

Summary

When Troubleshooting a multi-functional issue, it is best practice to break down the issue into pieces and test each piece as a separate system. However, we will first run through a quick test to see if we can find any obvious issues first.

Quick Test

Personal tools
Sponsers
Your Ad Here
Your Ad Here