Repetitive 0's?

So I posted a topic a few days ago and a couple people helped me on how to input the multiple digit variables with a keypad. I followed their advice and all I can see are 0’s now. They come in groups of 3 and spam my lcd screen. I would’ve posted this in my other topic but didn’t want any confusion with the title. That being said, I very much hope that someone else has experienced this and solved it or someone sees an error in my code.

I will have my code below and will attach a photo of the problem.

``````/* This code allows you top input values via a 3x4 keypad which will display
on the lcd screen in number format and alter the RGB Led color. You can switch
which color you edit with the * button. Use # to update the colours
Pins:
Arduino to lcd interface
5V to VCC
Gnd to Gnd
A5 to SCL
A4 to SDA

8 to 1
7 to 2
6 to 3
5 to 4
4 to 5
3 to 6
2 to 7

Arduino to RGB
~220ohm resistors between digital pins and color pins
9 to Red
10 to Blue
11 to Green
Gnd to Gnd
*/

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

int yR = 0;
int yG = 0;
int yB = 0;

int digitRA = 0;
int digitRB = 0;
int digitRC = 0;
int redDigit = 1;

int digitGA= 0;
int digitGB= 0;
int digitGC= 0;
int greenDigit = 1;

int digitBA = 0;
int digitBB = 0;
int digitBC = 0;
int blueDigit = 1;

void KeyRed(int x){
if (redDigit==1){
digitRA = x;
lcd.print(x);
}
if (redDigit==2){
digitRB = x;
lcd.print(x);
}
if (redDigit==3){
digitRC = x;
lcd.print(x);
}
redDigit = redDigit + 1;
if (redDigit >=4){}
}

void KeyGreen(int x){
if (redDigit==1){
digitGA = x;
lcd.print(x);
}
if (redDigit==2){
digitGB = x;
lcd.print(x);
}
if (redDigit==3){
digitGC = x;
lcd.print(x);
}
greenDigit = greenDigit + 1;
if (greenDigit >=4){}
}

void KeyBlue(int x){
if (blueDigit==1){
digitBA = x;
lcd.print(x);
}
if (redDigit==2){
digitBB = x;
lcd.print(x);
}
if (redDigit==3){
digitBC = x;
lcd.print(x);
}
blueDigit = blueDigit + 1;
if (blueDigit >=4){}
}

int Transform(int a, int b, int c){
int x;
x = a*100 + b*10 + c;
return x;
}

int color = 1;  //Allows control over what color you are writing

int redPin = 9;
int greenPin = 10;  //Sets up the individual color pins
int bluePin =11;

const byte ROWS = 4; //Four rows
const byte COLS = 3; //Three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'7','8','9'},
{'*','0','#'}
};
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

void setup(){
lcd.begin(16,2);
lcd.clear();

//TEST
Serial.begin(9600);
//TEST

}

void loop(){

if(key == '#'){
lcd.clear();
analogWrite(redPin, Transform(digitRA, digitRB, digitBC));
analogWrite(greenPin, Transform(digitGA, digitGB, digitGC));
analogWrite(bluePin, Transform(digitBA, digitBB, digitBC));
lcd.setCursor(0,1);
lcd.write("R");
lcd.print(Transform(digitRA, digitRB, digitBC));
lcd.write("G");
lcd.print(Transform(digitGA, digitGB, digitGC));
lcd.write("B");
lcd.print(Transform(digitBA, digitBB, digitBC));
}

if(key == '*'){
color+=1;
}
if(color >= 4){
color = 1;
}
if(key >=0){

if(color == 1){
KeyRed(key);
}

if(color == 2){
KeyGreen(key);
}

if(color == 3){
KeyBlue(key);
}

}
}
``````

There are a few comments here and there but I have given up trying to put them in when I began to redo most of the code. As always, I am new. So saying stuff that is beyond the basics will be hard pressed for me to understand.

I would start by making a change to loop() as follows:

``````void loop(){

if (key != NO_KEY){         // Add this new line
Serial.print("key = ");  // Add this new line
Serial.println(key);     // Add this new line

// Put all of your existing code here

}                           // Add this new line
}
``````

As the code stands now, you execute all of your code even when there is no key press. This means you are burning though your code hundreds, if not thousands, of times each second, which is likely throwing zeroes on your screen. Run the code above and pay attention to the values you see. You shouldn't see anything until you press a key. That will give you a starting place to look for what's happening.

What's the point of this?

``````  redDigit = redDigit + 1;
if (redDigit >=4){
}
``````
• If it is four or more, do nothing.

I did that so that the program doesn’t do anything if it gets more than 4 numbers. If I were to press 1234 the only numbers I want it to receive are 1,2, and 3 to create the 3 digit number 123

econjack: I would start by making a change to loop() as follows:

``````void loop(){
``````

if (key != NO_KEY){        // Add this new line     Serial.print("key = ");  // Add this new line     Serial.println(key);    // Add this new line

// Put all of your existing code here     }                          // Add this new line }

``````

As the code stands now, you execute all of your code even when there is no key press. This means you are burning though your code hundreds, if not thousands, of times each second, which is likely throwing zeroes on your screen. Run the code above and pay attention to the values you see. You shouldn't see anything until you press a key. That will give you a starting place to look for what's happening.
``````

I did this and by hitting a few buttons I [u]can[/u] write some input to my RGB led but when I press a number it displays something totally different. E.g. If I press 1 the LCD prints 49, if I press 2 it says 50, so on and so forth. It adds 48 to each number. This also messes around with my lower line display saying I am inputing absurd numbers like "6070" and "4200". I am very happy that you have given me the means to tell the led what to do but why is this happening?

RedStar:
I did that so that the program doesn’t do anything if it gets more than 4 numbers. If I were to press 1234 the only numbers I want it to receive are 1,2, and 3 to create the 3 digit number 123

Yes but this would do the same thing:

`````` redDigit = redDigit + 1;
``````

You don’t need to test for something, and if that condition is matched, do nothing. Why bother?

That’s like saying to your wife:

“Yes, darling?”
“You see that timer there?”
“Yes I see it.”
“Well, when that timer rings, I want you to do absolutely nothing.”
“OK dear, I can manage that. But what is the point of the timer in the first place?”
“Well, so when it rings, you can do nothing.”
“Ah, I see. Wouldn’t it be just as easy to not set the timer in the first place?”

RedStar: E.g. If I press 1 the LCD prints 49, if I press 2 it says 50, so on and so forth. It adds 48 to each number.

``````char keys[ROWS][COLS] = {
{'1','2','3'},
{'7','8','9'},
{'*','0','#'}
};
``````

The ASCII code for '1' is 0x31 or 49 in decimal. Does that give you a hint?

Okay so I understand that part about the timer... I think that I was thinking that that the program would be confused if the digit got above 3.

I don't know how to change the ASCll to "text" nor do I really know what ASCll is. Would there be a certain line I could use to switch the format? Also, why does the Arduino care now in this program yet when I had a seperate program that printed what I pressed it didn't care a single bit?

It adds 48 to each number. ... Would there be a certain line I could use to switch the format?

Well, you could subtract 48 as you have identified that the value is 48 too large.

[quote author=Nick Gammon link=msg=2060386 date=1422260803] Well, you could subtract 48 as you have identified that the value is 48 too large.

[/quote]

So am I correct in saying that I could use the line

``````Key = key - 48;
``````

I apologize for these short and probably very obvious questions but I just want to clarify things now as opposed to testing later, getting it wrong, and bumping this thread.

RedStar: So am I correct in saying that I could use the line

``````Key = key - 48;
``````

I apologize for these short and probably very obvious questions but I just want to clarify things now as opposed to testing later, getting it wrong, and bumping this thread.

You have the right general idea, except that C++ is case-sensitive.

Besides which, rather than bumping the thread, you could have tested it yourself, and seen if the numbers were no longer 48 too large.