Memorial day is 05/25/2015 – Orders will ship out on May 26th. There will not be any deliveries or shipping on Monday. Any order placed after 11am ET on Friday May 22nd will not start to ship out until Tuesday May 26th.

Combining MAX31855 sketch and PING sketch - strange readings?
Moderators: adafruit_support_bill, adafruit

Combining MAX31855 sketch and PING sketch - strange readings?

by RosscoS on Tue Nov 13, 2012 3:23 pm

Hi All,

I am working on a program that will eventually be a espresso machine PID controller that also senses when a person approaches the machine and turns on downlights to illuminate the working area.

I approached this problem by writing/modifying existing separate sketches that run each part (eg, temp sensing, PID control, person present[PING]).
Now I am trying to gradually merge the code.

When I merged the PING code with the MAX31855 code, I found that the thermocouple temps are now jumping around a bit and have also dropped significantly.

I can't figure out what is causing this... :?

Thanks in advance for any replies!

Code: Select all | TOGGLE FULL SIZE
#include <LiquidCrystal.h>  //this library is modified by Lady Ada to support I2C
#include <Wire.h>
#include <Adafruit_MAX31855.h>

// establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;
 
    unsigned long previousMillis = 0;        // will store last time barista light off timing sequence was triggered
    unsigned long currentMillis = 0;          // will store current millis reading
    unsigned long interval = 5000;           // interval from stepping away from machine to barista lights off
 
  const int pingPin = 12;           //pin for ping pulses & reading
  int baristaLight = 13;             //pin for controlling barista lights based on ping and interval
 
  int baristaPresentState;          //  variable for using the presence of the barista as a condition
 
  int thermoDO = 11;              // MAX31855 Data
  int thermoCS = 10;              // MAX31855 Chip Select
  int thermoCLK = 9;              // MAX31855 Clock

Adafruit_MAX31855 thermocouple(thermoCLK, thermoCS, thermoDO);

LiquidCrystal lcd(0); //Default address for I2C LCD

 
 void setup()
 {
   Serial.begin(9600);
   pinMode(baristaLight,OUTPUT);      // baristaLight is an output
   baristaPresentState = LOW;        // initialize baristaPresentState
   currentMillis = millis();        // initialize currentMillis
   lcd.begin(16, 2);                // initialize lcd
   lcd.setBacklight(HIGH);          // turn on led backlight
   lcd.clear();                      // clear lcd
 }
 
 
  void loop()
  {
    readTemps();          // run readTemps
    baristaPresence();   //run baristaPresence
 
   
  }
 
void baristaPresence()  {
    unsigned long currentMillis = millis();
 
  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
 
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  currentMillis = millis();
 
  // Determine whether there is a person standing in front of machine within range
  if (inches < 12)                          //if barista within 12 inches
    {
      digitalWrite(baristaLight,HIGH);      //turn barista light on
      baristaPresentState = HIGH;          //barista is present
     
    }
   
  else if ((inches > 6)&&(baristaPresentState == HIGH))    //if barista is not within 12" and previous baristaPresentState is HIGH
    {
     previousMillis = currentMillis;                        // begin timimg cycle
     baristaPresentState = LOW;                              //barista not present
    }
    else if ((currentMillis - previousMillis > interval) && (baristaPresentState == LOW))    //timing cycle complete and if barista is not present
     
      {
      digitalWrite(baristaLight, LOW);                      //turn light off
      baristaPresentState = LOW;                            //barista not present
       
   
      }
 
 
 
   
 
  }
 
  long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}


void readTemps()  {

   double c = thermocouple.readCelsius();    // read thermocouple in Celsius

   
   if (isnan(c)) {                      // if c is not a number
     lcd.setCursor(0,1);
     lcd.println("TC ERR!");            // thermocouple error!!
     Serial.print("TC ERR!!!");
   } else {                            //  otherwise
     lcd.setCursor(0,0);
     lcd.print("IT:");
     lcd.setCursor(3,0);
     lcd.print(thermocouple.readInternal());    // lcd print Internal Temp
     lcd.setCursor(9,0);
     lcd.print("C:");
     lcd.setCursor(11,0);
     lcd.print(thermocouple.readCelsius());    // lcd print Celsius Temp
     Serial.print("IT= ");
     Serial.println(thermocouple.readInternal());    // serial print Internal Temp
     Serial.print("C= ");
     Serial.print(thermocouple.readCelsius());      // serial print Celsius Temp
     Serial.print("F= ");
     Serial.println(thermocouple.readFarenheit());    // serial print Farenheit temp
     Serial.print("Error ");
     Serial.println(thermocouple.readError());        // serial print Read Error Bits
     
     
   }
   
}
RosscoS
 
Posts: 44
Joined: Wed May 09, 2012 2:41 pm

Re: Combining MAX31855 sketch and PING sketch - strange readings?

by RosscoS on Tue Nov 13, 2012 3:51 pm

I was just checking over everything and figured I would run the example serial code for the MAX31855. I found that the thermocouple readings are reading significantly lower than the internal temp, despite them being about 1" apart.

I have double checked that D0 is on pin 11, CS is pin 10 & CLK is pin 9

Here is the code I used-

Code: Select all | TOGGLE FULL SIZE
/***************************************************
  This is an example for the Adafruit Thermocouple Sensor w/MAX31855K

  Designed specifically to work with the Adafruit Thermocouple Sensor
  ----> https://www.adafruit.com/products/269

  These displays use SPI to communicate, 3 pins are required to 
  interface
  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries. 
  BSD license, all text above must be included in any redistribution
 ****************************************************/

#include "Adafruit_MAX31855.h"

int thermoDO = 11;
int thermoCS = 10;
int thermoCLK = 9;

Adafruit_MAX31855 thermocouple(thermoCLK, thermoCS, thermoDO);
 
void setup() {
  Serial.begin(9600);
 
  Serial.println("MAX31855 test");
  // wait for MAX chip to stabilize
  delay(500);
}

void loop() {
  // basic readout test, just print the current temp
   Serial.print("Internal Temp = ");
   Serial.println(thermocouple.readInternal());

   double c = thermocouple.readCelsius();
   if (isnan(c)) {
     Serial.println("Something wrong with thermocouple!");
   } else {
     Serial.print("C = ");
     Serial.println(thermocouple.readCelsius());
   }
   //Serial.print("F = ");
   //Serial.println(thermocouple.readFarenheit());
 
   delay(1000);
}
RosscoS
 
Posts: 44
Joined: Wed May 09, 2012 2:41 pm

Re: Combining MAX31855 sketch and PING sketch - strange readings?

by RosscoS on Tue Nov 13, 2012 4:01 pm

Found the culprit, the PING unit when connected to the same power rails as MAX31855 causes the strange readings!
RosscoS
 
Posts: 44
Joined: Wed May 09, 2012 2:41 pm

Re: Combining MAX31855 sketch and PING sketch - strange readings?

by adafruit_support_bill on Wed Nov 14, 2012 7:57 am

Found the culprit, the PING unit when connected to the same power rails as MAX31855 causes the strange readings!

Yes. They have a reputation for that. Sharp IR range sensors are pretty noisy too.

adafruit_support_bill
 
Posts: 37568
Joined: Sat Feb 07, 2009 10:11 am

Re: Combining MAX31855 sketch and PING sketch - strange readings?

by RosscoS on Fri Nov 16, 2012 3:34 am

Giving the PING its own LM7805 solved the problem :)
RosscoS
 
Posts: 44
Joined: Wed May 09, 2012 2:41 pm