Programmed relay switch wont hold

First things first, here is my code (yes i know it’s messy and it could probably have been done on fewer lines)

//http://www.arduino.cc/playground/Code/Keypad
#include <Keypad.h> // Download Keypad.zip

//define the cymbols on the buttons of the keypads
char hexaKeys[4][4] = {
 'A', 'B', 'C', 'D',
 'E', 'F', 'G', 'H',
 'I', 'J', 'K', 'L',
 'M', 'N', 'O', 'P'
};
byte rows = 4; //four rows
byte cols = 4; //four columns
byte rowPins[] = {2,3,4,5}; //connect to the row pinouts of the keypad
byte colPins[] = {6,7,8,9}; //connect to the column pinouts of the keypad


Keypad keypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, rows, cols);
float tempC; 		
int tempPin = 0;
char keyPress [2];
int pressIndex = 0;
int key1 = 0;
int key2 = 0;
int keytot = 0;

void setup(){
 Serial.begin(9600);
 pinMode(32, OUTPUT); 
  pinMode(33, OUTPUT);
  pinMode(34, OUTPUT);
  pinMode(35, OUTPUT);
  pinMode(36, OUTPUT);
  pinMode(37, OUTPUT);
  pinMode(38, OUTPUT);
  pinMode(39, OUTPUT);
  digitalWrite(39, 1);
  pinMode(42, OUTPUT); 
  pinMode(43, OUTPUT);
  pinMode(44, OUTPUT);
  pinMode(45, OUTPUT);
  pinMode(46, OUTPUT);
  pinMode(47, OUTPUT);
  pinMode(48, OUTPUT);
  pinMode(49, OUTPUT);
  digitalWrite(49, 1);
  pinMode(50, OUTPUT);
}
 
void loop(){
  tempC = analogRead(tempPin); 	
tempC = (5.0 * tempC * 100.0)/1024.0;  
//Serial.print(tempC); Serial.println(" Grader Celcius"); 	 //using this line to check if tempC works    
 char key = keypad.getKey();
 
 if (key != NO_KEY) {
   key -= 'A'-1;
   keyPress [pressIndex++] = key;
if (pressIndex == 2) {    // we've had two keypresses, so process them
Serial.println(keyPress[0], DEC);
Serial.println(keyPress[1], DEC);
  pressIndex = 0;  // reset for the next pair.
}
if (keyPress[0] == 1) {
  key1 = 1;
}
if (keyPress[0] == 2) {
  key1 = 2;
}
if (keyPress[0] == 3) {
  key1 = 3;
}
if (keyPress[0] == 5) {
  key1 = 4;
}
if (keyPress[0] == 6) {
  key1 = 5;
}
if (keyPress[0] == 7) {
  key1 = 6;
}
if (keyPress[0] == 9) {
  key1 = 7;
}
if (keyPress[0] == 10) {
  key1 = 8;
}
if (keyPress[0] == 11) {
  key1 = 9;
}
if (keyPress[0] == 14) {
  key1 = 0;
}
if (keyPress[1] == 1) {
  key2 = 1;
}
if (keyPress[1] == 2) {
  key2 = 2;
}
  if (keyPress[1] == 3) {
  key2 = 3;
}
if (keyPress[1] == 5) {
  key2 = 4;
}
if (keyPress[1] == 6) {
  key2 = 5;
}
if (keyPress[1] == 7) {
  key2 = 6;
}
if (keyPress[1] == 9) {
  key2 = 7;
}
if (keyPress[1] == 10) {
  key2 = 8;
}
if (keyPress[1] == 11) {
  key2 = 9;
}
if (keyPress[1] == 14) {
  key2 = 0;
}
keytot = (key1 * 10 + key2);
Serial.println (keytot);
if (keytot > tempC); {
  digitalWrite(50, 1);
}
if (keytot < tempC); {
  digitalWrite(50, 0);
}


   if (keyPress[0] == 1){ // 1
digitalWrite(42, 1);
 digitalWrite(43, 0);
 digitalWrite(44, 0);
 digitalWrite(45, 1);
 digitalWrite(46, 1);
 digitalWrite(47, 1);
 digitalWrite(48, 1);
   }
 if (keyPress[0] == 2){ // 2
  digitalWrite(42, 0);
 digitalWrite(43, 0);
 digitalWrite(44, 1);
 digitalWrite(45, 0);
 digitalWrite(46, 0);
 digitalWrite(47, 1);
 digitalWrite(48, 0);
 }
 if (keyPress[0] == 3) { // 3
  digitalWrite(42, 0);
 digitalWrite(43, 0);
 digitalWrite(44, 0);
 digitalWrite(45, 0);
 digitalWrite(46, 1);
 digitalWrite(47, 1);
 digitalWrite(48, 0);
 }
 if (keyPress[0] == 5) { //4
    digitalWrite(42, 1);
 digitalWrite(43, 0);
 digitalWrite(44, 0);
 digitalWrite(45, 1);
 digitalWrite(46, 1);
 digitalWrite(47, 0);
 digitalWrite(48, 0);
 }
 if (keyPress[0] == 6) { //5
   digitalWrite(42, 0);
 digitalWrite(43, 1);
 digitalWrite(44, 0);
 digitalWrite(45, 0);
 digitalWrite(46, 1);
 digitalWrite(47, 0);
 digitalWrite(48, 0);
 }
 if (keyPress[0] == 7) { //6
   digitalWrite(42, 0);
 digitalWrite(43, 1);
 digitalWrite(44, 0);
 digitalWrite(45, 0);
 digitalWrite(46, 0);
 digitalWrite(47, 0);
 digitalWrite(48, 0);
 }
 if (keyPress[0] == 9) { // 7
   digitalWrite(42, 0);
 digitalWrite(43, 0);
 digitalWrite(44, 0);
 digitalWrite(45, 1);
 digitalWrite(46, 1);
 digitalWrite(47, 1);
 digitalWrite(48, 1);
 }
 if (keyPress[0] == 10) { // 8
   digitalWrite(42, 0);
 digitalWrite(43, 0);
 digitalWrite(44, 0);
 digitalWrite(45, 0);
 digitalWrite(46, 0);
 digitalWrite(47, 0);
 digitalWrite(48, 0);
 }
 if (keyPress[0] == 11) { // 9
    digitalWrite(42, 0);
 digitalWrite(43, 0);
 digitalWrite(44, 0);
 digitalWrite(45, 1);
 digitalWrite(46, 1);
 digitalWrite(47, 0);
 digitalWrite(48, 0);
 }
 if (keyPress[0] == 14) { // 0
   digitalWrite(42, 0);
 digitalWrite(43, 0);
 digitalWrite(44, 0);
 digitalWrite(45, 0);
 digitalWrite(46, 0);
 digitalWrite(47, 0);
 digitalWrite(48, 1);
 }
 if (keyPress[1] == 1){ // 1
digitalWrite(32, 1);
 digitalWrite(33, 0);
 digitalWrite(34, 0);
 digitalWrite(35, 1);
 digitalWrite(36, 1);
 digitalWrite(37, 1);
 digitalWrite(38, 1);
   }
 if (keyPress[1] == 2){ // 2
  digitalWrite(32, 0);
 digitalWrite(33, 0);
 digitalWrite(34, 1);
 digitalWrite(35, 0);
 digitalWrite(36, 0);
 digitalWrite(37, 1);
 digitalWrite(38, 0);
 }
 if (keyPress[1] == 3) { // 3
  digitalWrite(32, 0);
 digitalWrite(33, 0);
 digitalWrite(34, 0);
 digitalWrite(35, 0);
 digitalWrite(36, 1);
 digitalWrite(37, 1);
 digitalWrite(38, 0);
 }
 if (keyPress[1] == 5) { //4
    digitalWrite(32, 1);
 digitalWrite(33, 0);
 digitalWrite(34, 0);
 digitalWrite(35, 1);
 digitalWrite(36, 1);
 digitalWrite(37, 0);
 digitalWrite(38, 0);
 }
 if (keyPress[1] == 6) { //5
   digitalWrite(32, 0);
 digitalWrite(33, 1);
 digitalWrite(34, 0);
 digitalWrite(35, 0);
 digitalWrite(36, 1);
 digitalWrite(37, 0);
 digitalWrite(38, 0);
 }
 if (keyPress[1] == 7) { //6
   digitalWrite(32, 0);
 digitalWrite(33, 1);
 digitalWrite(34, 0);
 digitalWrite(35, 0);
 digitalWrite(36, 0);
 digitalWrite(37, 0);
 digitalWrite(38, 0);
 }
 if (keyPress[1] == 9) { // 7
   digitalWrite(32, 0);
 digitalWrite(33, 0);
 digitalWrite(34, 0);
 digitalWrite(35, 1);
 digitalWrite(36, 1);
 digitalWrite(37, 1);
 digitalWrite(38, 1);
 }
 if (keyPress[1] == 10) { // 8
   digitalWrite(32, 0);
 digitalWrite(33, 0);
 digitalWrite(34, 0);
 digitalWrite(35, 0);
 digitalWrite(36, 0);
 digitalWrite(37, 0);
 digitalWrite(38, 0);
 }
 if (keyPress[1] == 11) { // 9
    digitalWrite(32, 0);
 digitalWrite(33, 0);
 digitalWrite(34, 0);
 digitalWrite(35, 1);
 digitalWrite(36, 1);
 digitalWrite(37, 0);
 digitalWrite(38, 0);
 }
 if (keyPress[1] == 14) { // 0
   digitalWrite(32, 0);
 digitalWrite(33, 0);
 digitalWrite(34, 0);
 digitalWrite(35, 0);
 digitalWrite(36, 0);
 digitalWrite(37, 0);
 digitalWrite(38, 1);
 }
 
 }
}

It is supposed to turn on a red LED when the temperature (variable name “tempC”) drops below the assigned temperature (variable name "keytot). the temperature in my room atm is around 25 degrees C, but when i press something like 11 or so, it just blinks for a split second and turns off again, i want the LED to stay on as long as keytot > tempC haaalp :frowning:

if (keytot > tempC); {

No semicolon wanted here.

works now thanks :) i will be back in a few seconds when something else goes wrong :)

whee, new problem, it only scans once, so if the temperature rises above the keytot it doesent turn off, i have to press again then. i want it to be automatic

Did you remove both semicolons?

if (keytot > tempC); {
  digitalWrite(50, 1);
}
if (keytot < tempC); {
  digitalWrite(50, 0);

is now

if (keytot > tempC) {
  digitalWrite(50, 1);
}
if (keytot < tempC) {
  digitalWrite(50, 0);

if that’s what u mean

but if i set keytot to 30 degrees, and hold my finger on the LM35 in order for the tempC to rise above 30, it doesent turn off, so i need it to rescan all the time so it notices when keytot is no longer > tempC.

but if i set keytot to 30 degrees, and hold my finger on the LM35 in order for the tempC to rise above 30, it doesent turn off, so i need it to rescan all the time so it notices when keytot is no longer > tempC.

Yeah. So, what's the problem?

OK, time to post your code again.

The bit:- if (key != NO_KEY) { Defines a block of stuff to do when a key is pressed. This includes testing keytot, so you never test it when a key is not pressed. Move it outside this block so it is tested each time through the loop.

PaulS:

but if i set keytot to 30 degrees, and hold my finger on the LM35 in order for the tempC to rise above 30, it doesent turn off, so i need it to rescan all the time so it notices when keytot is no longer > tempC.

Yeah. So, what's the problem?

problem is that it's supposed to be able to maintain a set temperature by turning a heater on and off whenever it rises or drops above or below a set temperature, but i'm nowhere near my hardware atm so cant test the code, just grabbing some ideas off u guys so i can test tomorrow, or hoping that u could spot some idiotic mistake i made.

hoping that u could spot some idiotic mistake i made.

Spotted see previous post from me.

oh didn’t see. actually i dont even know what that code does in there, someone else wrote it for me or it’s from the keypad tutorial in polayground or something … so i just delete it or what ?

It would help if, before posting code, you ran the auto-format tool in the IDE.

Your temperaure test is only performed if a key has been pressed, but you measure the temperature every time through loop.

so i just delete it or

No you move it to where it is executed every time round the loop.

i've never used auto-format ? didn't even know there was anything like that, only using the serial monitor, the compile and the upload button, what does auto-format do ?

... what does auto-format do ?

Formats your code neatly, automatically.

ok i've made it work, now i have an electronic question. when keytot and tempC is withing half a degree of each other the LED (which represents my heater) starts blinking like crazy cuz it goes under and above all the time .. so i'm worried this will damage my heater, can i slow down the update rate of tempC or increase the precision of the reading or soemthing that will cause less blinking ?

Google "hysteresis"