Changing Array Values and Display them on LCD.

Hello,

This code is made to control two HUGE heat beds for a very large 3D printer. Everything works except I cannot change the temperature setting displayed on the LCD with a keypad.

I have 'int tempSet' as an array that holds 3 keypad values. I then have: 'int TargetTemp = (tempSet[0] * 100) + (tempSet[1] * 10) + tempSet[2];' TargetTemp is then displayed on the LCD. It was the easiest way for me to convert three independent array values into one desired temperature value.

This is my first Arduino project. I am not a software guy (ME student), but I am trying to learn. It might be considered messy. I am sorry about that.

Thank you very much for your help.

-( Import needed libraries )-----*/
#include "Keypad.h"
#include <Wire.h> // Comes with Arduino IDE
// Get the LCD I2C Library here: 
// https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads
// Move any other LCD libraries to another folder or delete them
// See Library "Docs" folder for possible commands etc.
#include <LiquidCrystal_I2C.h>


/*-----( Declare Constants )-----*/
// none.


/*-----( Declare objects )-----*/
// set the LCD address to 0x27 for a 20 chars 4 line display  (*was originally 0x20)
// Set the pins on the I2C chip used for LCD connections:
//                    addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x3F, 20, 4);  // Set the LCD I2C address

//Keypad Object:
const byte rows = 4; //four rows
const byte cols = 3; //three columns
char keys[rows][cols] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'#','0','*'}
};

//keypad connections to the arduino terminals is given as:
byte rowPins[rows] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[cols] = {8, 7, 6}; //connect to the column pinouts of the keypad

// command for library forkeypad
//initializes an instance of the Keypad class
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, rows, cols );


/*-----( Declare Variables )-----*/
  //input pins and values     
int Therm1Pin = A0;
int Therm2Pin = A1;
float Therm1Value = 0;
float Therm2Value = 0;
int Therm1Temp = 0;
int Therm2Temp = 0;
int tempSet[3] = {0, 6, 5}; // stores each keypad entry into this array. Temp setting starts at 000 degrees.
int MinTemp = 8; // Safety shut off temperature. Anything lower than this value will result in shutoff. Prevents heatbed meltdown in the event a thermistor fails or a connection comes loose.
int TargetTemp = (tempSet[0] * 100) + (tempSet[1] * 10) + tempSet[2]; // This will hold our desired heat bed temperature, set by the keypad.
int count=0; // counts how many inputs are put in. If more than 3, it will clear tempSet back to {0, 0, 0} and waits until a new 3 digit temperature is put in.


 //Thermistor and Reference Resistor Values
int R1 = 100; //Reference resistor value in Ohms
int R2 = 100;
int k1 = 2;
int k2 = 2;
int b1 = -100;
int b2 = -100;


void setup() {    /*----( SETUP: RUNS ONCE )----*/

Serial.begin(9600); // Used to type in characters

 lcd.init(); // initialize the lcd for 20 chars 4 lines

lcd.backlight();         // turns backlight on

// NOTE: Cursor Position: CHAR, LINE) start at 0  
  lcd.setCursor(5,0); //Start at character 5 on line 0
  lcd.print("WORCESTER");           
  delay(500);
  lcd.setCursor(4,1);          // Displays this once switched on:
  lcd.print("POLYTECHNIC");    //_____________________
  delay(500);                  //|     WORCESTER      |    <-- instantly    
  lcd.setCursor(5,2);          //|    POLYTECHNIC     |    <-- after half a second (500milSec)
  lcd.print("INSTITUTE");      //|     INSTITUTE      |    <-- after one second
  lcd.setCursor(3,3);          //|   MQP: 2016-2017   |    <-- after 1.7 seconds
  delay(700);                  //---------------------    -- Screen stays on like this for two seconds (total time: 3.7 seconds) --
  lcd.print("MQP: 2016-2017"); //                          
  delay(3000);

} /*--(end setup )---*/



void loop() {   /*----( LOOP: RUNS CONSTANTLY )----*/

float Therm1Value = analogRead(Therm1Pin); 
float Therm2Value = analogRead(Therm2Pin);
int Relay1Pin = 13;
int Relay2Pin = 12;
int Relay3Pin = 11;
int Relay4Pin = 10;


int Therm1Temp = k1*R1*(1023/Therm1Value-1)+b1;
int Therm2Temp = k2*R2*(1023/Therm2Value-1)+b2;

pinMode(Relay1Pin, OUTPUT);   // Sets digital pin 13 as output
pinMode(Relay2Pin, OUTPUT);   // Sets digital pin 12 as output
pinMode(Relay3Pin, OUTPUT);   // Sets digital pin 11 as output
pinMode(Relay4Pin, OUTPUT);   // Sets digital pin 10 as output

Serial.print(Therm1Value,0);
Serial.print("\t");
Serial.print(Therm2Value,0);
Serial.print("\t");
Serial.print(Therm1Temp);
Serial.print("\t");
Serial.println(Therm2Temp);

/* ^^ can everything above (in this loop) be taken out and placed somewhere else? ^^ */

/* Could we possibly clean this up and turn the code below into callable methods? */

 char key = keypad.getKey();
 if (key != NO_KEY) // if someone presses a key:
  {
    int z = 0;
    lcd.setCursor(7,1);
    lcd.print("    ");
    tempSet[z]=key; // stores each keypad input into the tempSet array 
    z++;
    lcd.setCursor(8,1);
    lcd.print(key);
    Serial.print(key);
    count++;
    if (count==4) // If a fourth key is pressed, clear the current temp setting back to zero.
    {
      for (int zz=0; zz<3; zz++) 
       {
         tempSet[zz]= 0;
       }
      count=0;
    }
  }

  //Heat Bed 1 
if (Therm1Temp < TargetTemp && Therm1Temp > MinTemp) //MINIMUM TEMPERATURE set point in degrees C, replace value in parenthesis with desired set point value
 { 
  digitalWrite(Relay1Pin, HIGH);
  digitalWrite(Relay2Pin, HIGH);
 }
else 
 {
  digitalWrite(Relay1Pin, LOW);
  digitalWrite(Relay2Pin, LOW);
  }

  //Heat Bed 2
if (Therm2Temp < TargetTemp && Therm2Temp > MinTemp) //MINIMUM TEMPERATURE set point in degrees C, replace value in parenthesis with desired set point value
 { 
  digitalWrite(Relay3Pin, HIGH);
  digitalWrite(Relay4Pin, HIGH);
 }
else 
 {
  digitalWrite(Relay3Pin, LOW);
  digitalWrite(Relay4Pin, LOW);
  }  

   //LCD Displays:
   //_____________________
   //|--HEAT BED SET AT:--|        
   //|        ###         |   
   //|-HB 1-       -HB 2- |    
   //| ###          ###   |    
   //---------------------    
  lcd.clear(); // clears the screen
  lcd.setCursor(0,0); 
  lcd.print("--HEAT BED SET AT:--");
  lcd.setCursor(8,1);                          
  lcd.print(TargetTemp);           
  lcd.setCursor(0,2);
  lcd.print("-HB 1-"); 
  lcd.setCursor(13,2);
  lcd.print("-HB 2-");
  lcd.setCursor(1,3);
  lcd.print(Therm1Temp);
  lcd.setCursor(14,3);
  lcd.print(Therm2Temp);

 //Safety turn off
 if (Therm1Temp < MinTemp || Therm2Temp < MinTemp) {
 
  digitalWrite(Relay1Pin, LOW);
  digitalWrite(Relay2Pin, LOW);
  digitalWrite(Relay3Pin, LOW);
  digitalWrite(Relay4Pin, LOW);
  
  lcd.clear();
  lcd.setCursor(0,0); 
  lcd.print("-------ERROR!-------");
  lcd.setCursor(0,1);
  lcd.print("::MinTemp Reached::");
  lcd.setCursor(0,2);
  lcd.print("-----Check your-----");
  lcd.setCursor(0,3);
  lcd.print("----thermistors.----");

  for (int i=0; i <= 459000; i++){  //Stops loop, flashes and holds error message for 24 hours.
   lcd.backlight();      
    delay(500);       
   lcd.noBacklight(); 
    delay(200);
   } 

 }

 // TODO: prevent the user from entering too high of a temperature. 

 delay(3000); // Loops every 5 seconds. This keeps the relays from switching on and off rapidly, once ThermTemp is reached. 
 
}  /* --(end main loop )-- */

/* ( END ) */

This might be the source of the problem. I am having a difficult time wrapping my head around how to make the keypad work.

char key = keypad.getKey();
 if (key != NO_KEY) // if someone presses a key:
  {
    int z = 0;
    lcd.setCursor(7,1);
    lcd.print("    ");
    tempSet[z]=key; // stores each keypad input into the tempSet array 
    z++;
    lcd.setCursor(8,1);
    lcd.print(key);
    Serial.print(key);
    count++;
    if (count==4) // If a fourth key is pressed, clear the current temp setting back to zero.
    {
      for (int zz=0; zz<3; zz++) 
       {
         tempSet[zz]= 0;
       }
      count=0;
    }
  }

Heat_Bed_Temp_Control_v2.1.ino (7 KB)

keypad.getKey() returns a char. To convert it to a real number subtract '0' from it.