Hey all
Im having some trouble with a project im doing. Its meant to be a temperature control for a 3D print head thats separated from the printer electrical. First of this code was adapted from Greatscotts soldering iron found on instructables, I couldn't seem to get the original code to work at all and its better a little better now.
The pot is meant to set the desired temperature, the thermo couple (maxx6675) checks the temperature of the head and the Arduino is meant to fire a relay to turn the heater on or off. A couple of issues im having is the code doesn't seem to go into the void loop (the heater is turned on in setup and never turns off nor does the OLED display update until i press the reset button as if updatedisplay isn't being read)
#include <SPI.h>
#include <Wire.h>
#include <max6675.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_MOSI 11
#define OLED_CLK 13
#define OLED_DC 8
#define OLED_CS 10
#define OLED_RESET 9
#define thermoDO 11
#define thermoCS 12
#define thermoCLK 13
#define potentiometer A3
#define heat 4
float temperature;
int pottemperature;
int counter;
int heatVAL = 0;
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
void setup() {
display.begin(SSD1306_SWITCHCAPVCC);
pinMode(potentiometer, INPUT);
pinMode(heat, OUTPUT);
digitalWrite(heat, HIGH);
display.setTextSize(1.9);
display.setTextColor(WHITE);
counter = 1;
temperature = thermocouple.readCelsius();
updatedisplay();
attachInterrupt(0, zero, 1);
}
void loop() {
}
void zero() {
counter++;
if (counter < 2) {
if (temperature > pottemperature ) {
digitalWrite(heat,HIGH);
}
else {
digitalWrite(heat, LOW);
}
}
if (counter == 2) {
heatVAL = digitalRead(heat);
detachInterrupt(1);
temperature = thermocouple.readCelsius();
updatedisplay();
counter = 0;
attachInterrupt(0, zero,1);
}
}
void updatedisplay() {
pottemperature = analogRead(potentiometer);
pottemperature = map(pottemperature, 0, 800, 0, 400);
display.clearDisplay();
display.setCursor(0, 0);
display.print("SET:");
display.print(pottemperature);
display.setCursor(0, 20);
display.print("TMP:");
display.print(int(temperature));
display.display();
}
This is either a really easy or really hard question.
Thanks everyone
Why are you using an interrupt?
Tbh i have no idea, Its a left over from the original code.
I just removed it from my working code and no real difference to the project.
I've refactored it so it doesn't use the interrupt. I'm assuming that the code that's there would work if it were called appropriately - that may well be a foolish assumption.
#include <SPI.h>
#include <Wire.h>
#include <max6675.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_MOSI 11
#define OLED_CLK 13
#define OLED_DC 8
#define OLED_CS 10
#define OLED_RESET 9
#define thermoDO 11
#define thermoCS 12
#define thermoCLK 13
#define potentiometer A3
#define heat 4
float temperature;
int pottemperature;
unsigned long ReadTempTime;
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
void setup()
{
display.begin(SSD1306_SWITCHCAPVCC);
pinMode(potentiometer, INPUT);
pinMode(heat, OUTPUT);
digitalWrite(heat, HIGH);
display.setTextSize(1.9);
display.setTextColor(WHITE);
temperature = thermocouple.readCelsius();
updatedisplay();
}
void loop()
{
if(millis()-ReadTempTime > 1000)
{
temperature = thermocouple.readCelsius();
pottemperature = analogRead(potentiometer);
pottemperature = map(pottemperature, 0, 800, 0, 400);
if (temperature > pottemperature )
{
digitalWrite(heat, HIGH);
}
else
{
digitalWrite(heat, LOW);
}
updatedisplay();
ReadTempTime=millis();
}
}
void updatedisplay()
{
display.clearDisplay();
display.setCursor(0, 0);
display.print("SET:");
display.print(pottemperature);
display.setCursor(0, 20);
display.print("TMP:");
display.print(int(temperature));
display.display();
}
Compiled, not tested - that's your job 
That looks to be well on its way to working Wildbill do you mind me asking what you changed so i can learn from my mistakes?
as an aside and this could be more hardware related the reading for the thermocouple seems to be very erratic, It will read 150 then 17 then back to 148 in a matter of seconds.
Thanks mate
I deleted the empty loop function and renamed zero to be loop. I whacked all code setting up interrupts and then rebuilt loop to use millis to read the pot & thermocouple once a second. I'm not sure how long the thermocouple read takes so the frequency may be less than 1Hz. I cleaned up the update display function so that it does what its name says - i.e. moved the lines reading the pot out. Deleted a couple of useless lines of code and variables, reformatted it and called it done.
I saw the reformat bit. Just couldn't put the two side by side to compare them.
Thanks again, its a tough learning curve for someone with very little computer coding experience.