thermostat for heating element

I have the code that is kinda working. It is turning on the heating element and turning it off when I warn the thermistor but the screen is getting jumbled up. It’t because the code I am using was originally written for the Uno and I have to use the MEGA2560.

I have attached photo

I am using a Mosfit in place of the LCD.

#include <LiquidCrystal.h>// include the library code
int tempPin = A0; // make variables// thermistor is at A0
int tup = 3; //Temp up button
int tdown = 4; //Temp down button
int tcf = 5; //F to C toggle
boolean far = true; //Default F to true
boolean cel = false; //Default C to false
boolean lightMe; //Light up LED
char abbrv; //Shortcut for C or F abbreviation
float temp; //Raw temp value
float settemp; //Desired temp
int defF = 65; //Default Far.
int defC = 19; //Default Cel.
int upstate = 0; //Temp up button state
int downstate = 0; //Temp down button state
int tcfstate = 0; //C to F button state
int ledPin = 13; //LED’s pin
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // lcd is at 12,11,5,4,3,2 on the breadboard

void setup() {
Serial.begin (9600); // set the serial monitor tx and rx speed
lcd.begin(16, 2); // set up all the “blocks” on the display
lcd.setCursor(0, 0); // set the cursor to colum 0 row 0
lcd.print(“hello, world!”); // display hello world for 1 second
lcd.clear(); // clear the lcd
pinMode(tup, INPUT); //Set temp up button to input
pinMode(tdown, INPUT); //Set temp down button to input
pinMode(tcf, INPUT); //Set C to F button to input
pinMode(ledPin, OUTPUT); //Set LED to output
}

void loop() {
upstate = digitalRead(tup); //Read state of temp up
downstate = digitalRead(tdown); //Read state of temp down
tcfstate = digitalRead(tcf); //Read state of C to F button
int tvalue = analogRead(tempPin); // make tvalue what ever we read on the tempPin

if (tcfstate == LOW) { //If the C to F button is pressed
if (far == true) { //And Farenheit is true already
far = false;
cel = true; //Enable Celsius
}
else if (far == false) { //Otherwise if Farenheit isn’t true
far = true; //Enable Farenheit
cel = false;
}
}

if (far == true) { //If Farenheit is true
temp = farenheit(tvalue); //Caclulate temp in F
abbrv = ‘F’; //Set label to F
}
if (cel == true) { //If Celsius is true
temp = celsius(tvalue); //Calculate temp in C
abbrv = ‘C’; //Set label to C
}

if (upstate == LOW) { //if up button is pressed,
defF = defF + 1; //raise desired F by 1
defC = defC + 1; //Raise desired C by 1
}
if (downstate == LOW) { //if down button is pressed
defF = defF - 1; //lower desired F by 1
defC = defC - 1; //lower desired C by 1
}

lcd.setCursor(0, 0);
lcd.print("Current ");
lcd.print (temp); // Print the current temp in f
lcd.print (abbrv);
delay(255);
if (cel == true) {
lcd.setCursor (0, 1); // set the cursor to 0,1
lcd.print ("Desired "); // Print set to and your ideal temperature in f
lcd.print (defC);
lcd.print (abbrv);
}
if (far == true) {
lcd.setCursor (0, 1); // set the cursor to 0,1
lcd.print ("Desired "); // Print set to and your ideal temperature in f
lcd.print (defF);
lcd.print (abbrv);
}

lightUp(temp, defC, defF); //run custom light up command

if (lightMe == true) { //if light up is true
digitalWrite(ledPin, HIGH); //turn on LED
} else if (lightMe == false) { //otherwise
digitalWrite(ledPin, LOW); //turn off LED
}
} // we’re done

float farenheit(float raw) { //calculate far value
float f; //create temporary variable F
f = raw / 2.07; //calculate temperature from raw value
return f; //return temperature
}

float celsius(float raw) { //calculate cel value
float c; //create temp variable C
c = raw / 7.1; //calculate temperature from raw value
return c; //return temperature
}

boolean lightUp(float act, int desC, int desF) { //Decides if LED should light up, imports current temp, desired C, and desired F
if (act < desC || act < desF) { //if the actual is less than the desired F or C value
lightMe = true; //Turn on the LED
} else { //otherwise
lightMe = false; //Turn the LED off
}
return lightMe; //return that value
}

LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // lcd is at 12,11,5,4,3,2 on the breadboard

Can you please explain this ?

rkilpatrickii:
I am using a Mosfit in place of the LCD.

What is a Mosfit and how does it replace an LCD?

Steve

the code should read LiquidCrystal lcd(7, 8, 9, 10, 11, 12);// BS E D4 D5 D6 D7

These are the pins out to LCD, Lined up inputs on pins.

In my case the Mosfit has nothing to do with the LCD Screen. I have MosfitN data pin where an LED was. So that when the LED was supposed to come on, a gate is closed in the Mosfit allowing power to go to my heating element.

the code should read LiquidCrystal lcd(7, 8, 9, 10, 11, 12);// BS E D4 D5 D6 D7

Which pins on the Mega is the LCD connected to ?

I still have no real idea what a Mosfit is and I can't see any reference to it in the code. You don't mean a MOSFET do you? And if it has nothing to do with the LCD why do you say that it is "in place of the LCD"?

I think we really need a circuit diagram showing how everything is connected.

Steve

if this is what you see in your code; use PMW Pins 7 -12 on the mega. Although that might be why I’m messing up. I changed mine to that order because it worked that what in another thermostat code I was working with. I’ll change mine back and I’ll let you know what happens.

Sorry, MOSFET used in place of LED

UKHeliBob:
Which pins on the Mega is the LCD connected to ?

Nope, Now I have no screen at all

rkilpatrickii:
Nope, Now I have no screen at all

That is not the answer to the question posed

Sorry, been up for two days strait working on this.

If your still confused about the pins, open the .jpg, you can clearly what goes from the Mega to the LCD

Can someone help me set up the thermistor correctly? I have tried putting this in a couple different locations, but haven't figured it out yet.

int tempReading = analogRead(tempPin);
// This is OK
double tempK = log(10000.0 * ((1024.0 / tempReading - 1)));
float tempVolts = tempReading * 5.0 / 1024.0;
float tempC = (tempVolts - 0.5) * 10.0;
float tempF = tempC * 9.0 / 5.0 + 32.0;

Hi,
Have you got the gnd of the mega connected to the gnd of the protoboard?

Do you have a DMM to measure some voltages around your project?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
NOT a fritzy picture please.

Thanks.. Tom... :slight_smile: