New Member, working on a project

Hello, I am new to the forum and the Arduino. I have a project I am working on, and I’m currently at a sticking point. I’m hoping someone can get me over this hump.

I’m using a 4x4 keypad to enter a number. This will be a setpoint. It is being displayed on an LCD screen. There are sections in the code to make sure the setpoint is not less than 10, and not more than 45. What I am currently trying to do is get it to continuously blink one of two LED’s depending on what the setpoint is. As it is right now, it only half works. They keypad accepts my setpoint after the “A” key is pressed and moves down the program after the numbers are pressed and the “D” key is pressed. This is what I want.

Where I’m stuck is after that in the code. It blinks the correct LED one time, but once it turns it back off, it simply sits and does nothing. It’s seems to me like it’s sitting there waiting for the “A” key to be pressed again.

Here is the complete code:

#include <LiquidCrystal.h>
#include <Keypad.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

//4x4 Matrix key pad

const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns

// Define the Keymap

char keys[ROWS][COLS] =

{
{‘1’,‘2’,‘3’,‘A’},
{‘4’,‘5’,‘6’,‘B’},
{‘7’,‘8’,‘9’,‘C’},
{’*’,‘0’,’#’,‘D’}

};
byte rowPins[ROWS] = { 22, 23, 24, 25 };

byte colPins[COLS] = { 26, 27, 28, 29 };

Keypad kpd = Keypad( makeKeymap(keys), rowPins,
colPins, ROWS, COLS );

void setup() {

lcd.begin(16, 2);
lcd.print(“PRESS A TO ENTER”);
lcd.setCursor(0,1);
lcd.print(“SETPOINT”);
pinMode(53, OUTPUT); //53 is a Red LED
pinMode(52, OUTPUT); //52 is a Green LED
pinMode(34, INPUT);
int GetNumber();
int num;
}

void loop() {
lcd.setCursor(0,1);
//int GetNumber();
//int num = 0;
int num;
char key = kpd.getKey();

if(key == ‘A’)
{
int num = 0;
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“NEW SETPOINT”);
lcd.setCursor(0,1);
while(key != ‘D’)
{
switch (key)
{
case NO_KEY:
break;

case ‘0’: case ‘1’: case ‘2’: case ‘3’: case ‘4’:
case ‘5’: case ‘6’: case ‘7’: case ‘8’: case ‘9’:
lcd.print(key);
num = num * 10 + (key - ‘0’);
break;

case ‘*’:
num = 0;
lcd.clear();
break;
}
key = kpd.getKey();
}

if(num > 45)
{
num = 45;
}

if(num < 10)
{
num = 10;
}

lcd.clear();
delay(500);
lcd.setCursor(0,0);
lcd.print("SP = ");
lcd.setCursor(5,0);
lcd.print(num);

if(num >= 25 )
{
digitalWrite(52, LOW);
digitalWrite(53, HIGH);
delay(750);
digitalWrite(53, LOW);
delay(750);
}
else
{
digitalWrite(53, LOW);
digitalWrite(52, HIGH);
delay(750);
digitalWrite(52, LOW);
delay(750);
}
}
}

Any help is greatly appreciated.

My advice is to add some Serial.println() statements at strategic locations in your sketch and then run the sketch with Tools > Serial Monitor open. That will allow you to see what is happening in your program.

https://www.arduino.cc/reference/en/language/functions/communication/serial/println

Welcome to the forum.

Please read the basic instructions about how to post your code using "code tags". See item #7 of How To Use This Forum You can modify your initial post to add the code tags.

It's seems to me like it's sitting there waiting for the "A" key to be pressed again.

Indeed it as, as most of the code is within the block defined by

if (key == 'A') {//almost all your code is within these brackets}

cattledog: Welcome to the forum.

Please read the basic instructions about how to post your code using "code tags". See item #7 of How To Use This Forum You can modify your initial post to add the code tags.

Indeed it as, as most of the code is within the block defined by

if (key == 'A') {//almost all your code is within these brackets}

Apologies for the post structure, I'll do that in the future.

The issue seems to be that instead of leaving the "num" as the value entered in, it clears it and sets it to 0 after one pass through the code. I discovered this after my original post. I made some modifications to the program. One used an input pin to turn an LED on. It turns on and off depending on status of input even if I do not press "A."

The other modification was to have it print the value of "num" on the LCD screen at the very end of the code. That number is always printed as 0. So when it prints "SP = (num)," num is what I entered, but after that, it gets cleared and set to zero, hence, the LED blinks on and off once and then stops.

I made some modifications to the program.

Now would be the time to post your new code in code tags.

Archer3807: The issue seems to be that instead of leaving the "num" as the value entered in, it clears it and sets it to 0 after one pass through the code.

That is the expected behavior of a local variable. If you want the variable to retain its value even after loop() returns, you need to make the variable global: https://www.arduino.cc/reference/en/language/variables/variable-scope--qualifiers/scope/

It seems you also might have some confusion between a variable declaration and a variable definition:

Archer3807:   int num;

Here you declare a variable named num that is local to the loop() scope. Since you don't initialize it to a specific value, it will have a random value determined by whatever happens to be written at that memory location.

Archer3807:    int num = 0;

Here you declare another variable named num that is local to the if (key == 'A') scope. This is what's known as a "shadow variable". While you are inside the if (key == 'A') scope (between the two curly braces of that statement), all references to the num variable will be for this shadow variable instead of the one declared in the loop() scope.

Archer3807:          num = 0;

This is a variable definition. It sets the previously declared variable num to the value 0.

I will repost the code, it will have to be tomorrow because I’m currently away from the PC that had the IDE on it.

Pert, thank you for that explanation. I will work on cleaning up my declarations when I get back to the IDE tomorrow. I think I know how to fix it now.

Ok, I managed to sort it out this evening after fixing the declarations. Thank you Pert for catching that.

You're welcome. I'm glad to hear it's working now. Enjoy! Per