Hey! One of my projects involves creating a small layout of electronics that will control a RGB Led. I have wired everything up correctly. I am using a 3x4 Keypad, a 16x2 LCD screen with an adapter, and an RGB led. I am in the very beginning of this code and I have a slight problem. When attempting to print the numbers into strings and later into the input for the RGB led, I get a slight problem.
The problem is that I can't seem to write for each individual string. I don't really know how to explain this so please forgive me if I'm doing a bad job. I want to be able to press a button on my keypad, that button press will be translated into a number for a string ("string[Red, Blue, or Green][One, Two, or Three]"). I seem to be only able to write for the first string and I can't figure out how to fix this. A wait function or while function maybe? The wait function just delays it for a few seconds and I want to be able to wait for as much time as needed. As for the while function, I don't know how to use it.
My program is below, I am just working on the "red" right now. I apologize for my code if it seems a bit messy or rudimentary. I haven't yet put in the editing of the RGB led color.
/* 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 and finalize your current number with
the # button. The current values will be displayed at the bottom.
Pins:
Arduino to lcd interface
5V to VCC
Gnd to Gnd
A5 to SCL
A4 to SDA
Arduino to keypad
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
*/
String stringRedInput, stringRedOne,stringRedTwo, stringRedThree;
String stringGreenInput, stringGreenOne, stringGreenTwo, stringGreenThree; //Creates all the strings
String stringBlueInput, stringBlueOne, stringBlueTwo, stringBlueThree;
int color = 1; //Allows control over waht color you are writing
int currentString = 1; //Tells you what string number to write on
int redPin = 9;
int greenPin = 10; //Sets up the individual color pins
int bluePin =11;
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
const byte ROWS = 4; //Four rows
const byte COLS = 3; //Three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'}, //Creates the keypad
{'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
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){
lcd.begin(16,2);
lcd.clear();
//TEST
Serial.begin(9600);
//TEST
stringRedInput = String();
stringRedOne = String(); //Initializes red strings
stringRedTwo = String();
stringRedThree = String();
stringBlueInput = String();
stringBlueOne = String(); //Initializes blue strings
stringBlueTwo = String();
stringBlueThree = String();
stringGreenInput = String();
stringGreenOne = String(); //Initializes green strings
stringGreenTwo = String();
stringGreenThree = String();
}
void loop(){
char key = keypad.getKey();
lcd.setCursor(0,0);
if(key == '*'){
color+=1;
}
if(color == 4){
color = 0;
}
if(color == 1){
lcd.print("Red: ");
if(key != '*'){
lcd.setCursor(5,0);
stringRedOne = String(key);
lcd.print(stringRedOne);
char keyTwo = keypad.getKey();
stringRedTwo = String(keyTwo);
lcd.print(stringRedTwo);
char keyThree = keypad.getKey();
stringRedThree = String(keyThree);
lcd.print(stringRedThree);
stringRedInput = stringRedOne + stringRedTwo + stringRedThree;
Serial.println(stringRedInput);
}
}
if(color == 2){
lcd.print("Green: ");
}
if(color == 3){
lcd.print("Blue: ");
}
lcd.setCursor(0,1); //Prints this line on the bottom of the LCD display
lcd.print("R" + stringRedInput + " G" + stringGreenInput + " B" + stringBlueInput);
}
The problem is with the section below I think.
if(color == 1){
lcd.print("Red: ");
if(key != '*'){
lcd.setCursor(5,0);
stringRedOne = String(key);
lcd.print(stringRedOne);
char keyTwo = keypad.getKey();
stringRedTwo = String(keyTwo);
lcd.print(stringRedTwo);
char keyThree = keypad.getKey();
stringRedThree = String(keyThree);
lcd.print(stringRedThree);
stringRedInput = stringRedOne + stringRedTwo + stringRedThree;
Serial.println(stringRedInput);
}
}
[code]
The Serial.print function was just so I could see what was happening without the LCD. It just displayed a whole bunch of blank characters until I pressed a number, which displayed then was swept away by the blank characters.
Any help appreciated! Thanks!