Hi,
I have interfaced a keypad, a LCD and established serial communication successfully to arduino mega 2560.
I set limits through the keypad and compare the limit value with the value read through serial port.
After the comparison, the process doesn't repeat. The LCD displays the result and doesn't display the menu for next cycle and repeat the operation.
How do i keep the arduino active using the loop?
#include <Keypad.h>
#include <LiquidCrystal.h>
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'\n','0','\e','\b'}
};
byte rowPins[ROWS] = {30,31,32,33}; //connect to row pinouts
byte colPins[COLS] = {34,35,36,37}; //connect to column pinouts
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char UL[6];
char LL[6];
int k = 0;
int l = 0;
long int uvalue;
long int lvalue;
int i = 0;
const int RLED = 26;
const int YLED = 27;
const int GLED = 28;
char inByte[6] = {0};
long int inValue = 12345;
int m = 0;
int n = 0;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup(){
Serial.begin(9600);
lcd.begin(20,4);
lcd.cursor();
pinMode(RLED, OUTPUT);
pinMode(YLED, OUTPUT);
pinMode(GLED, OUTPUT);
displayMenu();
}
void displayMenu(){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("'A'-Enter the Limits");
lcd.setCursor(0, 2);
lcd.print("'B'-Reset");
}
void loop()
{
//displayMenu();
while (n<0) {
char key1 = keypad.getKey();
switch(key1) {
case 'A':
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Enter Upper Limit");
lcd.setCursor(0, 1);
while (k<5) {
char key = keypad.getKey();
if (key) {
if(key == '\b'){
lcd.rightToLeft();
lcd.write(" ");
lcd.leftToRight();
UL[k]=0;
k--;
}
else if(key == '\e'){
goto here;
}
else if(key != '\b'||'\e') {
lcd.print(key);
UL[k] = key;
k++;
}
}
}
goto here;
here:
uvalue = atol(UL);
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Lower Limit");
lcd.setCursor(0, 1);
while(l<5) {
char key = keypad.getKey();
if (key) {
if(key == '\b'){
lcd.rightToLeft();
lcd.write(" ");
lcd.leftToRight();
LL[l]=0;
l--;
}
else if(key == '\e'){
goto there;
}
else if(key != '\b'||'\e')
{
// Print a message to the LCD.
lcd.print(key);
LL[l] = key;
//Serial.println(key);
l++;
}
}
}
lvalue = atol(LL);
delay(1000);
lcd.clear();
goto there;
there:
lvalue = atoi(LL);
delay(1000);
lcd.print("Select the Range");
while(i<1){
char key2 = keypad.getKey();
switch(key2)
{
case '0':
{
lcd.setCursor(0, 1);
Serial.println(key2);
serialread();
lcd.print("read value is");
inValue = atol(inByte);
lcd.println(inValue);
compare();
m=0;
inValue=0;
inByte[6]=0;
}
break;
case '1':
{
lcd.setCursor(0, 0);
Serial.println(key2);
serialread();
lcd.setCursor(0, 2);
lcd.print("read value is");
inValue = atol(inByte);
lcd.println(inValue);
compare();
//displayMenu();
//m=0;
//inValue=0;
//inByte[6]=0;
//displayMenu();
}
break;
}
}
delay(1000);
lcd.clear();
inValue=0;
uvalue=0;
lvalue=0;
k=0;
l=0;
i=0;
m=0;
inByte[6]=0;
displayMenu();
}
break;
case 'B':
{
lcd.clear();
k=0;
l=0;
i=0;
uvalue=0;
lvalue=0;
inByte[6] = 0;
m=0;
displayMenu();
}
break;
}
}
n++;
}
void serialread()
{
while (m<5){
while (Serial.available() > 0){
inByte[m] = Serial.read();
m++;
}
}
}
void compare()
{
if(inValue > uvalue)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Read R value is ");
lcd.print(inValue);
lcd.setCursor(0, 2);
lcd.print("R > Upperlimit");
}
else if(inValue < lvalue)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Read R value is ");
lcd.print(inValue);
lcd.setCursor(0, 2);
lcd.print("R < Lowerlimit");
}
else
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Read R value is ");
lcd.print(inValue);
lcd.setCursor(0, 2);
lcd.print("R within Range");
}
}
Please check the code and help me..How do i repeat the process?