Control Structure problem

Hello everyone,

I am new to Arduino and programing in general and was wondering if I could receive some help on this project I was assigned. Essentially I have to make a part counter that is activated with an IR sensor. The part I am having trouble with is getting parts of my code to activate after other parts are being ran. I currently do not have an Arduino on hand, as it is on its way shipping but I am using the tinkerCAD simulator.

I want it so that after I hit the '' button on the 4x4 keypad, that it will make it so I can input a number next to the 'Fab Cell #' . I currently have two separate blocks of code that work great individually but I want it so that inputting a number will ONLY work if you first press the '' button first.

Thanks anyone for the help, I am very excited to code Arduino.

Here is my code, sorry for any incorrect formatting this is my first post:

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

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


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);
  lcd_1.begin(16, 2);
  lcd_1.print("Fab Cell #");
  lcd_1.setCursor(0,1);
  lcd_1.print("Part #");
  lcd_1.setCursor(partLoc,1);
  lcd_1.print("0");
}

void loop(){

  char customKey = customKeypad.getKey();
  
  if (customKey=='*'){ // this is the block of code that I want to run first that will allow all the other blocks to run after this is completed
	lcd_1.setCursor(fabLoc,0);
    lcd_1.print(" ");
    lcd_1.setCursor(fabLoc,0);
	lcd_1.blink();
  	}
     
	if (customKey=='1'){
        lcd_1.setCursor(fabLoc,0);
  		lcd_1.noBlink();
      	delay(del);
   		lcd_1.print('1');
   		Serial.println('1');
      	delay(100);
    	}
  
	if (customKey=='2'){
   		lcd_1.setCursor(fabLoc,0);
 		lcd_1.noBlink();
      	delay(del);
      	lcd_1.print('2');
    	Serial.println('2');
      	delay(100);
    	}

	if (customKey=='3'){
        lcd_1.setCursor(fabLoc,0);
  		lcd_1.noBlink();
      	delay(del);
   		lcd_1.print('3');
    	Serial.println('3');
      	delay(100);
    	}
  
  	if (customKey=='4'){
        lcd_1.setCursor(fabLoc,0);
 		lcd_1.noBlink();
      	delay(del);
   		lcd_1.print('4');
    	Serial.println('4');
      	delay(100);
    	}
      
     if (customKey=='5'){
        lcd_1.setCursor(fabLoc,0);
 		lcd_1.noBlink();
       	delay(del);
   		lcd_1.print('5');
   		Serial.println('5');
        delay(100);
    	}
  
	if (customKey=='6'){
        lcd_1.setCursor(fabLoc,0);
  		lcd_1.noBlink();
     	delay(del);
   		lcd_1.print('6');
    	Serial.println('6');
      	delay(100);
    	}

	if (customKey=='7'){
        lcd_1.setCursor(fabLoc,0);
  		lcd_1.noBlink();
      	delay(del);
   		lcd_1.print('7');
    	Serial.println('7');
      	delay(100);
  		}
  
  	if (customKey=='8'){
        lcd_1.setCursor(fabLoc,0);
  		lcd_1.noBlink();
      	delay(del);
   		lcd_1.print('8');
    	Serial.println('8');
      	delay(100);
    	}
      
     if (customKey=='9'){
        lcd_1.setCursor(fabLoc,0);
  		lcd_1.noBlink();
       	delay(del);
   		lcd_1.print('9');
    	Serial.println('9');
       	delay(100);
     	}
  
  	if (customKey=='0'){
        lcd_1.setCursor(fabLoc,0);
  		lcd_1.noBlink();
      	delay(del);
   		lcd_1.print('0');
    	Serial.println('0');
     	delay(100);
    	}
}

There are 4x4 (16) buttons on the Keypad and they are all marked. Which button are you referring to which when pressed the data entry phase gets enabled? How many digits you want to enter?

4x4Keypad

(post deleted by author)

Hello jonathanmm

Keep it simple and stupid.
Run some tutorials to the hardware selected.
If you are happy with the results of the tutorials you can merge these to the project by using control structures.

Have a nice day and enjoy coding in C++.

Hello paulpaulson

I know I am not doing this in the most effiecient way, I just dont know what control structures to use and how to use it.

Sorry, but i dont quite understand what is your problem?

@jonathanmm,

Your two topics on the same or similar subject have been merged.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

hello
not about your question
just about the code

You have 10 of nearly identical blocks of code like:

if (customKey=='1'){
        lcd_1.setCursor(fabLoc,0);
  		lcd_1.noBlink();
      	delay(del);
   		lcd_1.print('1');
   		Serial.println('1');
      	delay(100);
    	}

all of it can be easily replaced with one:

if ((customKey>='0') && (customKey <= '9')) {
        lcd_1.setCursor(fabLoc,0);
  		lcd_1.noBlink();
      	delay(del);
   		lcd_1.print(customKey);
   		Serial.println(customKey);
      	delay(100);
    	}

First you need to understand what a state machine is, see Demonstration code for several things at the same time

Then see Using millis() for timing. A beginners guide and understand how to get rid of delays and combine millis with a state machine.

The basic structure that will do what you want is to have a state for receiving the 'start' character then moving to a state that deals with the other characters, perhaps multiple states depending on what you want to do.

The basic idea is not 'only run this code after a particular character has been received' but run all the code repeatedly very fast but at each iteration have it decide if it needs to do its thing or move on to the next bit, which will also decide if it needs to do its thing or move on.

You are keying in characters so you need to have a function to check for characters and put them somewhere, then another function that decides what to do with them. However, the thing I want you to understand is both functions get run every time around loop, the 'get character' function checks for a new character and if it finds one it puts it in whatever character store you have (maybe an array) and leaves an indication that there's a new character. The 'process character' function checks for new characters in the store and does something with them.

Good practice is to NOT put everything in loop() but to split the code into functions with meaningful names. Doing so makes the code easier to understand and easier to debug.

This can be reduced to:

	if (customKey>='0' && customKey<='9'){
        lcd_1.setCursor(fabLoc,0);
  		lcd_1.noBlink();
      	delay(del);
   		lcd_1.print(customKey);
   		Serial.println(customKey);
      	delay(100);
    	}
1 Like

It is possible that John posted his answer before the different threads were merged so he never saw your post. That is one reason that cross posting is against the rules, members wasting their time duplicating advice.

1 Like

I want it so that when you press '*' in the bottom left that it enables data entry. Ideally I would like to use a digit number

I apologize I wasn't sure if my first post was uploaded.

Add a variable to your code with name, for example dataEntryEnable. This variable should be tested before accepting '0' - '9' keys. if it false - the pressing of the buttons should be mutely ignored.
When the program starts, dataEntryEnable = false, this changed to true if ones press the "" button. After pressing any button other than "" the variable became false again.
This give you the program, that will be accept one numeric key only after pressing the "*"

The following sketch (tested on UNO) allows you to enter a 4-digit code from the Keypad and they appear on Serial Monitor. You can easily interface your LCD.

#include <Keypad.h> //libraries

const byte ROWS = 4;
const byte COLS = 4;
bool flag = false;
char keyCode[5] = {0};
int arrayIndex = 0;

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}; //R1R2R3R4
byte colPins[COLS] = {5, 4, 3, 2};  //C1C2C3C4

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

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  char customKey = customKeypad.getKey();
  if (customKey != 0x00)
  {
    if (flag == false)
    {
      if (customKey == '*')
      {
        flag = true;
      }
    }
    else
    {
      keyCode[arrayIndex] = customKey;
      Serial.print(keyCode[arrayIndex]);
      arrayIndex++;
      if (arrayIndex == 4)
      {
        keyCode[arrayIndex] = '\0'; //insert null-charcater
        arrayIndex = 0;
        Serial.println();
        Serial.print("You have entered : "); Serial.println(keyCode);
        flag = false;
      }
    }
  }
}

Output:

7896
You have entered : 7896
1234
You have entered : 1234

What ends 'data entry'?

I don't understand what you mean by "a digit number".

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