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
#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
}
}