Sunday, March 24, 2013

Control System

Started getting to work on the control system for my senior design project. It looks pretty cluttered right now. Going to use the 3D printer at work to design a case and then solder everything together into a nice little package. The code will also be cleaned up soon with some comments. Thanks to Arduino.cc and http://bildr.org/2011/07/ds18b20-arduino/ for most of the code.

 Cluttered wire mess.


The wires bundled at the right are the temperature sensors. These correspond to three different readings of W1, W2, and P, which are shown below. 






#include <OneWire.h>
#include <LiquidCrystal.h> //LCD Screen
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //LCD Screen

//Temperature chip i/o
  // on digital pin 2

void setup(void) {
  Serial.begin(9600);
  lcd.begin(16, 2);
}

void loop(void) {
  float temperatureW1 = getTempW1();
  float temperatureW2 = getTempW2();
  float temperatureP = getTempP();
  //Serial.println(temperature);
  lcd.print("W1:");
  lcd.print(temperatureW1);
  lcd.print(" P:");
  lcd.print(temperatureP);
  lcd.setCursor(0, 1);
  lcd.print("W2:");
  lcd.print(temperatureW2);
  delay(2000); //just here to slow down the output so it is easier to read
  lcd.clear();
}


float getTempW1(){
  int DS18S20_Pin = 7;
  OneWire ds(DS18S20_Pin);
  //returns the temperature from one DS18S20 in DEG Celsius

  byte data[12];
  byte addr[8];

  if ( !ds.search(addr)) {
      //no more sensors on chain, reset search
      ds.reset_search();
      return -1000;
  }

  if ( OneWire::crc8( addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return -1000;
  }

  if ( addr[0] != 0x10 && addr[0] != 0x28) {
      Serial.print("Device is not recognized");
      return -1000;
  }

  ds.reset();
  ds.select(addr);
  ds.write(0x44,1); // start conversion, with parasite power on at the end

  byte present = ds.reset();
  ds.select(addr);  
  ds.write(0xBE); // Read Scratchpad


  for (int i = 0; i < 9; i++) { // we need 9 bytes
    data[i] = ds.read();
  }

  ds.reset_search();

  byte MSB = data[1];
  byte LSB = data[0];

  float tempRead = ((MSB << 8) | LSB); //using two's compliment
  float TemperatureSum = tempRead / 16;
  return TemperatureSum;
}

float getTempW2(){
  int DS18S20_Pin = 8;
  OneWire ds(DS18S20_Pin);
  //returns the temperature from one DS18S20 in DEG Celsius

  byte data[12];
  byte addr[8];

  if ( !ds.search(addr)) {
      //no more sensors on chain, reset search
      ds.reset_search();
      return -1000;
  }

  if ( OneWire::crc8( addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return -1000;
  }

  if ( addr[0] != 0x10 && addr[0] != 0x28) {
      Serial.print("Device is not recognized");
      return -1000;
  }

  ds.reset();
  ds.select(addr);
  ds.write(0x44,1); // start conversion, with parasite power on at the end

  byte present = ds.reset();
  ds.select(addr);  
  ds.write(0xBE); // Read Scratchpad


  for (int i = 0; i < 9; i++) { // we need 9 bytes
    data[i] = ds.read();
  }

  ds.reset_search();

  byte MSB = data[1];
  byte LSB = data[0];

  float tempRead = ((MSB << 8) | LSB); //using two's compliment
  float TemperatureSum = tempRead / 16;
  return TemperatureSum;
}

float getTempP(){
  int DS18S20_Pin = 9;
  OneWire ds(DS18S20_Pin);
  //returns the temperature from one DS18S20 in DEG Celsius

  byte data[12];
  byte addr[8];

  if ( !ds.search(addr)) {
      //no more sensors on chain, reset search
      ds.reset_search();
      return -1000;
  }

  if ( OneWire::crc8( addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return -1000;
  }

  if ( addr[0] != 0x10 && addr[0] != 0x28) {
      Serial.print("Device is not recognized");
      return -1000;
  }

  ds.reset();
  ds.select(addr);
  ds.write(0x44,1); // start conversion, with parasite power on at the end

  byte present = ds.reset();
  ds.select(addr);  
  ds.write(0xBE); // Read Scratchpad


  for (int i = 0; i < 9; i++) { // we need 9 bytes
    data[i] = ds.read();
  }

  ds.reset_search();

  byte MSB = data[1];
  byte LSB = data[0];

  float tempRead = ((MSB << 8) | LSB); //using two's compliment
  float TemperatureSum = tempRead / 16;
  return TemperatureSum;
}

No comments:

Post a Comment