/*@file place_value_screen_display
@brief This code is to compare the place value of 2 number and if correct print the each place value digit*/
#include <Keypad.h>//include the library
// Define keypad layout and size
const byte ROWS = 4;
const byte COLS = 3;
const byte ones = 10; // Pin number for the "ones" button
const byte tens = 11; // Pin number for the "tens" button
const byte hundreds = 12; // Pin number for the "hundreds" button
const byte thousands = 13; // Pin number for the "thousands" button
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {2, 3, 4, 5}; // Pins connected to the keypad rows
byte colPins[COLS] = {6, 7, 8}; // Pins connected to the keypad columns
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
#define MAX_NUMBER_LENGTH 4// Store up to 4 digits entered
char orgArray[MAX_NUMBER_LENGTH]; // array initialize
char Array[MAX_NUMBER_LENGTH];
int numberIndex = 0; // Current position in orgArray
#define MAX_NUMBER 4 //store upto 4 digits
char checkArray[MAX_NUMBER]; // array initialize
int Index = 3; // set the position of array as 3
// Variables to track switch states
bool onesPressed = false;
bool tensPressed = false;
bool hundredsPressed = false;
bool thousandsPressed = false;
void setup() {
Serial.begin(9600);//start serial communication
//set the modes of switches
pinMode(ones, INPUT_PULLUP);
pinMode(tens, INPUT_PULLUP);
pinMode(hundreds, INPUT_PULLUP);
pinMode(thousands, INPUT_PULLUP);
for (int i = 0; i < MAX_NUMBER_LENGTH; i++) {
orgArray[i] = ' '; // Initialize array with spaces
}
}
void loop() {
// Read the keypad
char key = keypad.getKey();
// Check for button presses and set flags
if (digitalRead(ones) == LOW) {
Serial.println("ones switch pressed");
onesPressed = true;
delay(300); // Debounce delay
}
// Check for tens button presses and set flags
if (digitalRead(tens) == LOW) {
Serial.println("tens switch pressed");
tensPressed = true;
delay(300);// Debounce delay
}
// Check for hundreds button presses and set flags
if (digitalRead(hundreds) == LOW) {
Serial.println("hundreds switch pressed");
hundredsPressed = true;
delay(300);// Debounce delay
}
// Check for thousands button presses and set flags
if (digitalRead(thousands) == LOW) {
Serial.println("thousands switch pressed");
thousandsPressed = true;
delay(300);// Debounce delay
}
// Process keypad input based on the state of the switches
if (key != NO_KEY) {
if (onesPressed || tensPressed || hundredsPressed || thousandsPressed) {
// if ones pressed store the key in checkArray[3] and print the digit
if (onesPressed) {
checkArray[3] = key;
Serial.println("ones Numbers Entered: ");
Serial.println(checkArray[3]);
}
// if tens pressed store the key in checkArray[2] and print the digit
if (tensPressed) {
checkArray[2] = key;
Serial.println("tens Numbers Entered: ");
Serial.println(checkArray[2]);
}
// if hundreds pressed store the key in checkArray[1] and print the digit
if (hundredsPressed) {
checkArray[1] = key;
Serial.println("hundreds Numbers Entered: ");
Serial.println(checkArray[1]);
}
// if thousands pressed store the key in checkArray[0] and print the digit
if (thousandsPressed) {
checkArray[0] = key;
Serial.println("thousands Numbers Entered: ");
Serial.println(checkArray[0]);
}
// Reset switch states after processing
onesPressed = false;
tensPressed = false;
hundredsPressed = false;
thousandsPressed = false;
} else if (key >= '0' && key <= '9' && numberIndex < MAX_NUMBER_LENGTH) {
// No switch pressed, just store the number in orgArray
orgArray[numberIndex] = key;
numberIndex++;
} else if (key == '#') {
// On '#' press, adjust digit positions and display the result
adjustDigitsPosition();
// Print each digit and its corresponding position
Serial.print("Number Entered at 0: ");
Serial.println(orgArray[0]);
Serial.print("Number Entered at 1: ");
Serial.println(orgArray[1]);
Serial.print("Number Entered at 2: ");
Serial.println(orgArray[2]);
Serial.print("Number Entered at 3: ");
Serial.println(orgArray[3]);
Serial.print("Question:");
for (int i = 0; i < MAX_NUMBER_LENGTH; i++) {
Serial.print(orgArray[i]);
Array[i]=orgArray[i];
}
Serial.println(); // Move to a new line
}
**else if (key == '*') {**
** bool correct=false;**
** if (Array[0] == checkArray[0] && Array[1] == checkArray[1] && Array[2] == checkArray[2] && Array[3] == checkArray[3] ) { **
** correct = true;**
** }**
** if(correct){**
// If correct, display the positions of each digit
Serial.println("correct");
for(int i=0;i<MAX_NUMBER_LENGTH;i++)
{
switch(i){
case 0:
Serial.print(Array[0]);
Serial.println("thousands");
break;
case 1:
Serial.print(Array[1]);
Serial.println("hundreds");
break;
case 2:
Serial.print(Array[2]);
Serial.println("tens");
break;
case 3:
Serial.print(Array[3]);
Serial.println("ones");
break;
}
}
}//if not correct display wrong
else if (correct==false) {
Serial.println("wrong");}
// Reset the orgArray for next inputs
numberIndex = 0;
for (int i = 0; i < MAX_NUMBER_LENGTH; i++) {
orgArray[i] = ' '; // Reset orgArray
}
}
}
}
// Function to adjust the digit positions in orgArray based on numberIndex
void adjustDigitsPosition() {
// Calculate the starting index based on the number of digits entered
int startIndex = MAX_NUMBER_LENGTH - numberIndex;
// Move digits to the right position in orgArray
for (int i = MAX_NUMBER_LENGTH - 1, j = numberIndex - 1; j >= 0; i--, j--) {
orgArray[i] = orgArray[j];
}
// Fill the unused positions with spaces
for (int i = 0; i < startIndex; i++) {
orgArray[i] = ' ';
}
}
In this code bolded portion is not working properly,ie if we give 2 same numbers it not comparing properly always shows wrong on serial moniter.Could anyone help us.