Tuesday, 18 June 2013

Major Project (Not Complete)

Major Project

For my major project I decided to expand on my original temperature reader.

Additions:
-A new automatic clock reader I found online. Requires an older version of the arduino environment I found on-line to run due to changes in keywords, but it's better than a manually in putted time.

-EEPROM write

-LEDs to alarm me when it is below 20 degrees celcius, and I should therefore light the fire.

Code:

const int temperaturePin = 0;

#include <EEPROM.h>
#include <Time.h>

#define TIME_MSG_LEN  11    // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER  'T'   // Header tag for serial time sync message
#define TIME_REQUEST  7    // ASCII bell character requests a time sync message

int addr = 0;
int led1 = 13;
int led2 = 12;

void setup()
{
  Serial.begin(9600);
  pinMode(led1, OUTPUT);  
  pinMode(led2, OUTPUT);

  Serial.begin(9600);
  setSyncProvider( requestSync);  //set function to call when sync required
  Serial.println("Waiting for sync message");
}

void loop()
{
  float voltage, degreesC, degreesF;
  int val = analogRead(0) / 4; //EEPROM so results so can be recovered if Arduino turns off

  voltage = getVoltage(temperaturePin);
  degreesC = (voltage - 0.5) * 100.0; //reads arduons voltage, and calculates that into celcius
  EEPROM.write(addr, val);
 
   if(Serial.available() )
  {
    processSyncMessage();
  }
  if(timeStatus()!= timeNotSet)
  {
    digitalClockDisplay();
  }
  delay(1000);
 
  if (degreesC > 10)
  {
    digitalWrite(led2, HIGH);
  }
  else if (degreesC < 10)
  {
    digitalWrite(led1, HIGH);
  }
 
  addr = addr + 1;
  if (addr == 512)
    addr = 0;
   
  delay(1000); //wait 1 hour
}

float getVoltage(int pin)

{
  return (analogRead(pin) * 0.004882814);
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(dayStr(weekday()));
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(monthShortStr(month()));
  Serial.print(" ");
  Serial.print(year());
  Serial.println();
}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

void processSyncMessage() {
  // if time sync available from serial port, update time and return true
  while(Serial.available() >=  TIME_MSG_LEN ){  // time message consists of a header and ten ascii digits
    char c = Serial.read() ;
    Serial.print(c);
    if( c == TIME_HEADER ) {      
      time_t pctime = 0;
      for(int i=0; i < TIME_MSG_LEN -1; i++){  
        c = Serial.read();        
        if( c >= '0' && c <= '9'){  
          pctime = (10 * pctime) + (c - '0') ; // convert digits to a number  
        }
      }  
      setTime(pctime);   // Sync Arduino clock to the time received on the serial port
    }
  }
}
time_t requestSync()

{
  Serial.print(TIME_REQUEST,BYTE);
  return 0; // the time will be sent later in response to serial mesg
}

Problems:
-Requires older version (less than 1.0) of the arduino environment to run the time reader

-EEPROM still glitchy. Not sure on problem. Worked at one point but no longer does

Photo:

No comments:

Post a Comment