While loop with 4x4 keypad

Hello again,

I am looking for a way to make something keep happening until a button is pressed. In particular, I want to loop lcd.blink until a number 1-9 is pressed on a 4x4 keypad. Or if someone could try to explain to me why my code is not printing a number after I press the * button, I would appreciate that as well. This is what I have so far:

#include <Keypad.h> //libraries
#include <Adafruit_LiquidCrystal.h>

static int count = 0; //variable used as first number
Adafruit_LiquidCrystal lcd_1(0);
const byte ROWS = 4;
const byte COLS = 4;
int del = 100;
int fabLoc = 11;  // tells the location of the fabrication #
int partLoc = 7;// tells the location of the part #
int IR = 13;
int sensorStatus = digitalRead(IR);

void countUp(){        
  		++count;
    	lcd_1.setCursor(7,1);
    	lcd_1.print("     ");
        lcd_1.setCursor(7,1);
    	lcd_1.print(count);
  		}

void countDown(){
  		if(count!=0){
        	--count;
       		lcd_1.setCursor(7,1);
    		lcd_1.print("     ");
        	lcd_1.setCursor(7,1);
    		lcd_1.print(count);
      		}
    	}

char hexaKeys [ROWS][COLS] ={
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};

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

void setup()
{
  Serial.begin(9600);
  pinMode (IR, INPUT);
  lcd_1.begin(16, 2);
  lcd_1.print("Fab Cell #");
  lcd_1.setCursor(0,1);
  lcd_1.print("Part #");
  lcd_1.setCursor(7,1);
  lcd_1.print(count);
}

void loop(){

  char customKey = customKeypad.getKey();
  
	
  	if (customKey=='C'){
        countUp();
    }
  
  	if (customKey=='D'){
      countDown();
    }
	
  if (sensorStatus==1){
    countUp();
  }
  

  switch (customKey){
  case '*':
    // This is where I believe my issue is
    do{ 
    	lcd_1.setCursor(fabLoc,0);
    	lcd_1.print(" ");
    	lcd_1.setCursor(fabLoc,0);
		lcd_1.blink();
    }while (((customKey>='0') && (customKey <= '9')));
  		if ((customKey>='0') && (customKey <= '9')) {
        	lcd_1.setCursor(fabLoc,0);
      		lcd_1.noBlink();
      		delay(del);
   			lcd_1.print(customKey);
   			Serial.println(customKey);
    		}
  	break;
    
  case'#':
  lcd_1.setCursor(partLoc,1);
    lcd_1.print(" ");
    lcd_1.setCursor(partLoc,1);
	lcd_1.blink();
    if ((customKey>='0') && (customKey <= '9')) {
  		lcd_1.noBlink();
      	delay(del);
   		lcd_1.print(customKey);
   		Serial.println(customKey);
    	}
  	break;
  }
    
}

...and what does it do, currently?

Why should it print a number after the '*' key?

where is 'customKey' updated in this loop?

I want it to print whatever number (0-9) that is inputted

I am unaware of what you mean by updated. What do you mean by that?

Changed.

I think you want something like this:

bool blinking = false;

void loop()
{
  if (blinking)
  {
    lcd_1.blink();
  }

  if (sensorStatus == 1)
  {
    countUp();
  }

  char customKey = customKeypad.getKey();

  switch (customKey)
  {
    case 'C':
      countUp();
      break;

    case 'D':
      countDown();
      break;

    case '*':
      lcd_1.setCursor(fabLoc, 0);
      lcd_1.print(" ");
      lcd_1.setCursor(fabLoc, 0);
      blinking = true;
      break;
      
    case'#':
      lcd_1.setCursor(partLoc, 1);
      lcd_1.print(" ");
      lcd_1.setCursor(partLoc, 1);
      lcd_1.blink();
      blinking = true;
      break;

    case '0'...'9':
      lcd_1.noBlink();
      blinking = false;
      delay(del);
      lcd_1.print(customKey);
      Serial.println(customKey);
      break;
  }
}
1 Like

This is the ONLY place in your program you are updating customKey. See the problem now?

I guess I am a little confused about what you mean by updating. What purpose does it serve to update it?

Consider

y = 0;
do
{
x++;
}
while (y < 10);

@johnwasser offers you a great solution.

But to answer your specific question. Look at the loop below. Nowhere in this loop do you change the value of customKey. Therefore, the value of customKey when you ENTER the loop will determine whether the loop executes one time or infinite number of times.

do{ 
    	lcd_1.setCursor(fabLoc,0);
    	lcd_1.print(" ");
    	lcd_1.setCursor(fabLoc,0);
		lcd_1.blink();
    }while (((customKey>='0') && (customKey <= '9')));

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.