Use keypad to reset array values

Hello,

I am having trouble resetting a three digit integer array with a keypad. This array is then turned into a single integer and is displayed on an LCD screen.

This project is designed to regulate the temperature of two large 3D printer heat beds.

Here is the code that's important to my question:

-( 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>

//Keypad Object:
const byte ROWS = 4; //four rows
const byte COLS = 3; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {5, 6, 7, 8}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {2, 3, 4}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

int tempSet[3] = {0,6,5}; // stores each keypad entry into this array. Temp setting starts at 000 degrees.
int TargetTemp = (tempSet[0] * 100) + (tempSet[1] * 10) + tempSet[2]; 
int z = 0;
int key;


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

Serial.begin(9600); 
lcd.init(); // initialize the lcd for 20 chars 4 lines
lcd.backlight();

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



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


   //LCD Displays:
   //_____________________
   //|--HEAT BED SET AT:--|        
   //|        ###         |   
   //|-HB 1-       -HB 2- |    
   //| ###          ###   |    
   //---------------------    
  lcd.clear(); // clears the screen
  lcd.setCursor(0,0); 
  lcd.print("--HEAT BEDS 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);


for (int i=0; i <= 40; i++){
key = keypad.getKey();
 if (key != NO_KEY)
  {
    tempSet[z] = key;
    z++;
    
  } else  
       {
         delay(100);
       } 

  if (z == 4) 
    {
       z = 0;
    }
  
  
}

}
for (int i=0; i <= 40; i++){
key = keypad.getKey();
 if (key != NO_KEY)
  {
    tempSet[z] = key;
    z++;
    
  } else  
       {
         delay(100);
       } 

  if (z == 4) 
    {
       z = 0;
    }
  
  
}

I'm confused by this. What's the point of looping through this 40 times? I thought you only wanted three keypresses.

-( Import needed libraries )-----*/

You have (fortunately) left the realm of Java programming. Libraries are included, NOT imported.

What's the point of looping through this 40 times?

41, actually.

OP: Your placement of curly braces is too damned inconsistent. Put EVERY { on a line BY ITSELF. Put EVERY } on a line BY ITSELF. Use Tools + Auto Format, so your code doesn't look like it was typed by a drunken monkey.

There is absolutely NO excuse for delay() in the loop that tries to read key presses. A while loop, while the number of key pressed is less than 3, can loop 5544552145872121455847111 times, with no extra load on the Arduino. Using delay() to limit the number of iterations of the while loop to 5544552145872121 times does nothing to make the Arduino's life easier.

Of course, the most important thing you should get from that last rant is that you SHOULD be using a while loop, NOT a for loop, to get the required number of key presses, IF you find it necessary to wait for them all to arrive. Personally, I would NOT find it necessary.

Doing something with tempSet, after you do get three key presses, seems an essential step to me. One that you forget. The calculation of TargetTemp does NOT bind TargetTemp to the values in tempSet.

I didn't include all of the code, since it wouldn't fit in a single post. The delay is meant to hold the loop by 4 seconds. That way, once the heat beds reach target temperature, the relays are not flickering on and off. The heat beds are either heating up for four seconds, or cooling down for four seconds. I do not know how else to do this within the loop.

I have very little experience with programing at all. This is my first Arduino project. Thank you for the help!

That way, once the heat beds reach target temperature, the relays are not flickering on and off.

That's not the way your home thermostat works. What it does is heat up to the target temperature, and then turn the heater off. It does not start the heater again until the actual temperature has dropped below the target temperature by some fixed (or adjustable, if you have a fancy thermostat) amount.

The thermostat knows nothing about time. There may be an associated clock that does, and diddles with the setpoint based on time, but the thermostat will happily run the heater for hours if needed to get the temperature up to the set point, or not run the heater for weeks if it is not needed (as in the summer).

I didn't include all of the code, since it wouldn't fit in a single post.

It certainly would. If you had read the stickies at the top of the forum, you'd have learned that the initial post dialog and the Reply dialog have an Additional Options link (the Quick Reply field does not) that lets you attach a file when the file size exceeds 9500 characters.

You need to separate the running of the heater or cooler from getting the data from the keypad. Take a look at Robin2's post on reading serial data. While it focuses on reading serial data, it is equally appropriate for reading other kinds of data that arrive intermittently, like keypad data.

Serial input basics - updated