Day 13- Frustration with Code!

Fjodor and I started out Day 13 by starting again on the lab, hoping to go through laboratories #2 and #5, the labs we will be running with our group of teachers next week.  We started our the day going to the laboratory and working on Laboratory #2. 

When we started Lab #2, our Temperature Sensor Lab, it ended up being that they had someone solder the MAX6675 to some breakout boards.  Fjodor and I hadn’t ever soldered, so we learned and tried to make our prototype.

Our board uses an Arduino to interface with the computer and give us a temperature output.  We started by using last year’s script, but because Arduino has since updated their library, that script wasn’t working.  In fact, it wouldn’t even compile.

We ended up finding another script online, which still wasn’t compiling.  It ends up that the script was using SCK, which is now an Arduino library.  We found and replaced SCK with CLK, and the script compiled.

We were, however, still receiving an interesting output- about 270 degrees Celsius.  We kept trying to figure out why it was so high, but we couldn’t figure out why it was so off. 

For the rest of the day, I recoded the script to get it to work.  Around 4:45 pm, I ended up realizing that it was reading off by a decimal point.  It should have been 27.0 degrees Celsius, not 270 degrees Celsius!  I quickly recoded the solution, and it worked!

Here is our script for the MAX6675 temperature sensor with K-type thermocouple

/*
  Temperature Reading from a MAX6675

Modified By Jason DeFuria, BUSAT <[email protected]> on 7/19/2012

Ryan McLaughlin <[email protected]>
*/

#define SO 12    // MISO
#define CLK 13   // Serial Clock

#define TC_0 11  // CS Pin of MAX6607
int TC_0_calib = 0;  // Calibration compensation value in digital counts (.25˚C)

void setup() {

pinMode(SO, INPUT);
pinMode(CLK, OUTPUT);

pinMode(TC_0, OUTPUT);
digitalWrite(TC_0,HIGH);  // Disable device

Serial.begin(9600);
}

/* Create a function read_temp that returns an unsigned int
   with the temp from the specified pin (if multiple MAX6675).  The
   function will return 9999 if the TC is open.

   Usage: read_temp(int pin, int type, int error)
     pin: the CS pin of the MAX6675
     type: 0 for ˚F, 1 for ˚C
     error: error compensation in digital counts
     samples: number of measurement samples (max:10)
*/
float read_temp(int pin, int type, int error, int samples) {
  float value = 0;
  int error_tc;
  float temp;
  float temp_out;

  for (int i=samples; i>0; i–){
    digitalWrite(pin,LOW); // Enable device

    /* Cycle the clock for dummy bit 15 */
    digitalWrite(CLK,HIGH);
    digitalWrite(CLK,LOW);

    /* Read bits 14-3 from MAX6675 for the Temp
         Loop for each bit reading the value and
         storing the final value in ‘temp’
    */
    for (int i=11; i>=0; i–){
        digitalWrite(CLK,HIGH);  // Set Clock to HIGH
        value += digitalRead(SO) << i;  // Read data and add it to our variable
        digitalWrite(CLK,LOW);  // Set Clock to LOW
    }

    /* Read the TC Input inp to check for TC Errors */
    digitalWrite(CLK,HIGH); // Set Clock to HIGH
    error_tc = digitalRead(SO); // Read data
    digitalWrite(CLK,LOW);  // Set Clock to LOW

    digitalWrite(pin, HIGH); //Disable Device
  }

  value = value/samples;  // Divide the value by the number of samples to get the average

  /*
     Keep in mind that the temp that was just read is on the digital scale
     from 0˚C to 1023.75˚C at a resolution of 2^12.  We now need to convert
     to an actual readable temperature (this drove me nuts until I figured
     this out!).  Now multiply by 0.25.  I tried to avoid float math but
     it is tough to do a good conversion to ˚F.  THe final value is converted
     to an int and returned at x10 power.

   */

  value = value + error;  // Insert the calibration error value

  if(type == 0) {  // Request temp in ˚F
    temp = ((value*0.25) * (9.0/5.0)) + 32.0;  // Convert value to ˚F (ensure proper floats!)
  } else if(type == 1) {  // Request temp in ˚C
    temp = (value*0.25);  // Multiply the value by 25 to get temp in ˚C
  }

  temp_out = temp;  // Send the float to an int (X10) for ease of printing.

  /* Output 9999 if there is a TC error, otherwise return ‘temp’ */
  if(error_tc != 0) { return 9999; } else { return temp_out; }
}

void loop() {

  // Read the temperature and print it to serial
  Serial.print(“Temp F: “);
  Serial.print(read_temp(TC_0,0,TC_0_calib,15));
  Serial.print(“\tTemp C: “);
  Serial.println(read_temp(TC_0,1,TC_0_calib,15));

  delay(250);
}

 

Here are some pictures:

IMG_20120719_122449

Our setup connected with an Arduino Uno

 

IMG_20120719_163955

Running Arduino on my Mac