I'm making a thermometer using MAX6675 and the thermocouple.
I'm using Arduino Uno to get the information from the sensor when display on 3 cathode displays with multiplexing.
The problem is that it only displays the value it got right before the loading on arduino, and the same stays unless I press the reset button.
My intetion is about reading the temperature while display the value read.
Hope you guys can help to solve it ;D
Code:
/*
Temperature Reading from a MAX6675
Ryan McLaughlin ryanjmclaughlin@gmail.com
*/
#define A 8
#define B 2
#define C 3
#define D 4
#define E 5
#define f 6
#define G 7
#define WD A3
#define GND1 A2
#define GND2 A1
#define GND3 A0
#define SO 12 // MISO
#define SCK 13 // Serial Clock
#define TC_0 11 // CS Pin of MAX6607
unsigned int data=0;
int TC_0_calib = 0; // Calibration compensation value in digital counts (.25[ch730]C)
void setup() {
pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
pinMode(D, OUTPUT);
pinMode(E, OUTPUT);
pinMode(f, OUTPUT);
pinMode(G, OUTPUT);
pinMode(GND1, OUTPUT);
pinMode(GND2, OUTPUT);
pinMode(GND3, OUTPUT);
pinMode(WD, OUTPUT);
pinMode(SO, INPUT);
pinMode(SCK, OUTPUT);
pinMode(TC_0, OUTPUT);
digitalWrite(TC_0,HIGH); // Disable device
Serial.begin(9600);
}
void digit0 () {
// for 0 needed to turn ON F A B C D E segments, so:
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(f, HIGH);
//////////////////////// G segment should be turn OFF
digitalWrite(G, LOW);
};
void digit1 () {
digitalWrite(A,LOW);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(f, LOW);
digitalWrite(G, LOW);
};
void digit2 () {
digitalWrite(A,HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, LOW);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(f, LOW);
digitalWrite(G, HIGH);
};
void digit3 () {
digitalWrite(A,HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, LOW);
digitalWrite(f, LOW);
digitalWrite(G, HIGH);
};
void digit4 () {
digitalWrite(A,LOW);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(f, HIGH);
digitalWrite(G, HIGH);
};
void digit5 () {
digitalWrite(A,HIGH);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, LOW);
digitalWrite(f, HIGH);
digitalWrite(G, HIGH);
};
void digit6 () {
digitalWrite(A,HIGH);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(f, HIGH);
digitalWrite(G, HIGH);
};
void digit7 () {
digitalWrite(A,HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(f, LOW);
digitalWrite(G, LOW);
};
void digit8 () {
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(f, HIGH);
digitalWrite(G, HIGH);
};
void digit9 () {
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, LOW);
digitalWrite(f, HIGH);
digitalWrite(G, HIGH);
};
void showdigit (int digit)
{
switch (digit) {
case 0:
digit0 ();
break;
case 1:
digit1 ();
break;
case 2:
digit2 ();
break;
case 3:
digit3 ();
break;
case 4:
digit4 ();
break;
case 5:
digit5 ();
break;
case 6:
digit6 ();
break;
case 7:
digit7 ();
break;
case 8:
digit8 ();
break;
case 9:
digit9 ();
break;
default:
break;
};
};
void showdigits (int number)
{
// e.g. we have "1234"
int aux=0, a=0;
showdigit(number/100); // segments are set to display "1"
digitalWrite(GND1, LOW); // first digit on,
digitalWrite(GND2, LOW); // other off
digitalWrite(GND3, HIGH);
delay (4);
aux = (number%100)/10; // remainder of 1234/1000 is 234
digitalWrite(GND3, LOW); // first digit is off
showdigit(aux); //// segments are set to display "2"
digitalWrite(GND2, HIGH); // second digit is on
delay (4); // and so on....
digitalWrite(GND2, LOW);
showdigit(number%10);
digitalWrite(GND1, HIGH);
delay (4);
};
/* 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 [ch730]F, 1 for [ch730]C
error: error compensation in digital counts
samples: number of measurement samples (max:10)
*/
unsigned int read_temp(int pin, int type, int error, int samples) {
unsigned int value = 0;
int error_tc;
float temp;
unsigned int temp_out;
for (int i=samples; i>0; i--){
digitalWrite(pin,LOW); // Enable device
/* Cycle the clock for dummy bit 15 */
digitalWrite(SCK,HIGH);
digitalWrite(SCK,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(SCK,HIGH); // Set Clock to HIGH
value += digitalRead(SO) << i; // Read data and add it to our variable
digitalWrite(SCK,LOW); // Set Clock to LOW
}
/* Read the TC Input inp to check for TC Errors */
digitalWrite(SCK,HIGH); // Set Clock to HIGH
error_tc = digitalRead(SO); // Read data
digitalWrite(SCK,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[ch730]C to 1023.75[ch730]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 [ch730]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 [ch730]F
// temp = ((value0.25) * (9.0/5.0)) + 32.0; // Convert value to [ch730]F (ensure proper floats!)
// } else if(type == 1) { // Request temp in [ch730]C
temp = (value0.25); // Multiply the value by 25 to get temp in [ch730]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_temp(TC_0,1,TC_0_calib,10);
data = read_temp(TC_0,1,TC_0_calib,10);
//Serial.println(data);
showdigits(data);
}