element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Arduino - Two Inputs Using the Keypad.
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 5 replies
  • Subscribers 392 subscribers
  • Views 1784 views
  • Users 0 members are here
Related

Arduino - Two Inputs Using the Keypad.

Former Member
Former Member over 10 years ago

Hey guys,

i am new to arduino. i am currently working on a small project. basically i am using the 4 x 4 keypad for the input and LCD for display. so once the program runs the user enters two value. so first input is 2 digit input which arduino saves in an array and similarly the second input is a four digit input that is saved into the second array. The program runs successfully and reads the first input correctly though the second input does not work.

 

if (int(key1) != 0 and currentState == 1) {

 

switch (key1) {

case '*':

// sets the first array to zero

currentState = 1;

break;

 

case '#':

//saves the value into the first array

//sets the currentState to 2

currentState = 2;

if (currentState = 2){

switch (key1){

case '*':

//sets the second array to zero

currentState = 2;

break;

case '#':

//saves the value into the second array

currentState = 1;

break;

 

case 'A':

//goes back to the menu

break;

 

default:

currentTValue[0] = currentTValue[1];

currentTValue[1] = key1;

break;

}

}

break;

 

case 'A':

//goes back to the menu

break;

 

default:

currentT2Value[0] = currentT2Value[1];

currentT2Value[1] = currentT2Value[2];

currentT2Value[2] = currentT2Value[3];

currentT2Value[3] = key1;

break;

}

}

}

  • Sign in to reply
  • Cancel

Top Replies

  • Robert Peter Oakes
    Robert Peter Oakes over 10 years ago +3
    You have the classic beginner mistake of using an assignment where you want to perform a comparison currentState = 2 is an assignment not a compare currentState == 2 is a comparison and will return a true…
  • mcb1
    mcb1 over 10 years ago +1 verified
    if (currentState = 2) needs to be if (currentState == 2) Although I'm unsure why you check when you set it the line above and several lines later. Do you really need line 12 and 13 ? if (int(key1) !…
  • Robert Peter Oakes
    Robert Peter Oakes over 10 years ago in reply to mcb1 +1
    A very good and valid point Mark, I was not intending to deminish your valued input, just focus on the lesser issue of the use of comparison vs assignment operators to help Terry along. And yes, it is…
  • mcb1
    0 mcb1 over 10 years ago

    if (currentState = 2)

     

    needs to be if (currentState == 2)

    Although I'm unsure why you check when you set it the line above and several lines later.

     

    Do you really need line 12 and 13  ?

     

    if (int(key1) != 0 and currentState == 1) {
    
     switch (key1) {
     case '*':
     // sets the first array to zero
     currentState = 1;
     break;
    
     case '#':
     //saves the value into the first array
    //sets the currentState to 2
     currentState = 2;
    
    if (currentState = 2){
    switch (key1){
    case '*':
    //sets the second array to zero
    currentState = 2;
    break;
    case '#':
    //saves the value into the second array
    currentState = 1;
     break;
    
     case 'A':
     //goes back to the menu
     break;
      
     default:
     currentTValue[0] = currentTValue[1];
     currentTValue[1] = key1;
     break;
     }
    }
     break;
    
     case 'A':
     //goes back to the menu
     break;
      
     default:
     currentT2Value[0] = currentT2Value[1];
     currentT2Value[1] = currentT2Value[2];
     currentT2Value[2] = currentT2Value[3];
     currentT2Value[3] = key1;
     break;
     }
     }
    }

     

     

    For entering code use the >> and select Syntax Highlighting, then C++.

     

     

    Mark

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Reject Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 10 years ago

    You have the classic beginner mistake of using an assignment where you want to perform a comparison

     

    currentState = 2 is an assignment not a compare

    currentState == 2 is a comparison and will return a true or false that is needed by an "if" statement of other commands like "while and do"

     

    the comparison operators extend to != (Not equal), <= Less than or equal, >= Greater than or equal, || Logical OR of the two values, && Logical AND of two values

     

    so a logical or could be " if (currentValue >= 5) && (currentValue <= 10)"

    in this case if the current value is between 5 and 10 including 5 and 10 then a true would be returned

     

    If the expression was this  " if (currentValue >= 10) || (currentValue <= 5)" then anything between 5 and 10 would return true

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mcb1
    0 mcb1 over 10 years ago in reply to Robert Peter Oakes

    Peter

    While the incorrect operator has been used, there is the question of why check when you set it in the line above.

     

    It's possible the case statement is missing an ending, or similar.

     

     

    I suspect some cutting and pasting went wrong.

    Since the code is not complete, I left it at that issue.

     

    Terry will come back with some clues as to what it should do.

     

     

    Mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 10 years ago in reply to mcb1

    A very good and valid point Mark, I was not intending to deminish your valued input, just focus on the lesser issue of the use of comparison vs assignment operators to help Terry along.

     

    And yes, it is probably a copy paste issue.  Terrry... Your turn image

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • balearicdynamics
    0 balearicdynamics over 10 years ago

    Hello,

     

    despite the = / == check in the if that anyway returns a wrong condition respect what you expect but it is a valid assignation so it is NOT a syntax error, I have pasted your code in the IDE, just to see what are the errors - if any. The problem is that what is really important and is missing in your bunch of code is where and how you declare and initialiases the character arrays.

     

    What sounds strange to me, but it is better if you update the post with the topmost part of the code, at least, is the use of the arrays (I suppose character arrays). Please can you add few more lines of code ?

     

    Last but not least, did you took in account that if key  is a char it is also an integer too ?

     

    Enrico

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube