Reading data from device in programming

Thread Starter

Parth786

Joined Jun 19, 2017
642
Before you get into all of that, is your LCD working? Can you display characters and strings where you want them i.e. locate the cursor and write characters on both lines?
I ask because you went from a simple switch detect to several LCD versions and now to a keyboard scanner but the LCD code is gone.
It looks to me like you are jumping around without a plan. What is the plan? Despite your nice statement, I don't know what is better for you, only you know that. You asked about adding two numbers but your example of adding multiple digit numbers increased the complexity. In response, I suggested that you were moving into more complex coding and that was something to think about before proceeding. My suggestion that you need a keyboard scanner was something to think about as you kept adding things (the second operator).
As we have been telling you, you can't proceed without a plan. If you want to make a calculator, think about the main components and what they do:
.
Sorry, I think I miss understood your explanation in post #17 I thought you were talking about original keypad programming. that's the reason I posted only keypad program. I think you didn't notice my above post. I said my LCD is working and I have tested code. My main plan is to write program for calculator. I tried to write with matrix keypad but I couldn't get success. so I decide to replace keypad with switches. than Wbhan sir, suggested to me these things
Write a program that does nothing more than display a "1" on the screen.
Then change it so that it alternates between a "1" and a "2", changing once a second.
Then start worrying about tying what is displayed to reading switch inputs.
So I followed there advice. I tested my program. when I press switch1, LCD display "1" and when I press switch LCD display "2'. so I think my hardware is fine. I need to learn programming. than in post #16 I explained what I want to do
I wanted to make following in program
addition program
1) get the first argument, maybe multiple digits
2) get the operator '+'
3) get the second argument
4) get another operator '=' then perform the operation
That's a lot to do but note that the big task is split into smaller tasks - get keyboard input, display things. Note that the each big part is split into smaller parts. With a good design, you split things up then code and test each one separately. Once its working, move on to the next thing. That's why I asked about the LCD. If it's not working, get it working then leave it in your code and use it. Then on to the next step. and the next. When you are out of steps, you are done.
But even if you don't want the whole calculator, displays and keypads are common modules for many projects and its worth knowing how to do them.
That's the only way I know how to do it.
I know very well I cant write complex program at this level so that's why I divided my task in to small parts. so If you see program. first my programming is working or not, than I wrote program to display number on LCD using switches. and than I tried to write to add tow numbers

Now I have four switches, LCD and 8051 and I have to write working code for following condition
1) get the first argument, maybe multiple digits
2) get the operator '+'
3) get the second argument
4) get another operator '=' then perform the operation
I will try to add this features in my working program
You appear to be ignoring advice given before.
Draw a flow chart before you start coding. Design comes before coding. The flow chart must work on paper. The code must match the flow chart otherwise there is no point in drawing a flow chart.
I am not ignoring advice. I tried my best effort to make flow chart but every time I failed to make good flow chart. I feel making algorithm is more easy than drawing flow chart. but you suggested many times so I am making new flow chart. when it complete I will post that flow chart.
 

JohnInTX

Joined Jun 26, 2012
4,787
You don't have to be fancy in the flow chart. Just sketch it out on paper. Add boxes for your variables. Trace through the steps with your finger:
Say aloud '1'. That is your switch closing. Take that input and trace through the flow chart all the way to the variable box in your desired numeric representation - binary integers, BCD, floats, whatever. I'd start with integers.
Say aloud '2'. That is the net digit Trace that input into the variable box and determine what you have to do to make two consecutive switch closures 1 and 2 become 12 in your numeric representation.
Say aloud '+' and figure out what effect that will have. You can't add yet because you don't have the second argument.. Probably have to store the pending operation somewhere. Show that on your flowchart.
Continue by getting the second argument until the next operator.
Process the operation. If it's another '+' you can do that intermediate operation then get another argument. If it's '=' do the last operation and restart from the top.
Consider carefully how the choice of numeric representation affects how you add/subtract AND display the numbers. The LCD uses ASCII. How do you go from a sum to ASCII digits?
Decide what you'll do in the case of an error (argument too big, invalid operator etc..)

You will find that as you progress, you'll see lots of places that require further thought. Better on paper than in code..
Once it flows OK, see where you can simplify. Getting arguments and displaying the input seems to me like a good place for a function/subroutine.

Fix problems in the flow then take a picture and post. As we both have said many times, if you can't get it to work on paper, it won't work on the chip.

If it sounds like a lot that is because it is a lot - that's what it takes. You might want to nibble around the edges by just adding and displaying consecutive switch presses:
1 1 2 1 implies 1+1+2+1=5. That's easier but still uses some of the stuff you'll need for more complex operation.

Good luck!
 

WBahn

Joined Mar 31, 2012
29,930
Forget about the size being one or two digits. Go with single digit operands at first. Keep the complexity down and build it up as you go.

Or, write a simple program that let's you get multiple-digit inputs and then just echoes the value to the second line of your display. Then you can use that code as a building block.

But let's take your flowchart as is.

The first block says to get a number that may be one or two digits. How will your code know when the second digit is entered? It must be able to detect when the first digit's entry is over and when the second digit's entry starts. What happens if the just wants one digit? What happens if they enter a third digit? What do you do with this number after it's been entered?

Those are just the questions left unanswered by your first block.

Now you say to get the + operator. What if the user enters something other than the + operator? You need to account for all possibly actions the user might take. Now, you can certainly write your program so that if the user doesn't do what they are supposed to, then the program simply crashes or behaves in an undefined way -- and that's not an unreasonable approach at first when you are developing your code. But pretty quickly you should trap disallowed actions and have your program die gracefully -- perhaps output "error" to the LCD and then lock up or accept any key entry after that point as a signal to start over.

Then you have a block that gets the second number. Same questions as the first block. In particular, where are the two values stored?

With the "=" key you are doing a test. If they don't enter an equals key you "Hold previous value". What is the "previous" value? The one that was entered in the first block, or the second? You then go up to the first block again but now you are going to get two values -- what do you do with this "previous value" that you held? If they do enter an "=" key, then you "Do addition" -- addition on what? And what do you do with it? Your flow chart doesn't have you displaying anything at any time to the LCD display. Perhaps that is implied (though coding by implication is not a good coding strategy). But whether you do or not you immediately go up and get a new number from the user. What is the display showing at this point? When does the display get cleared?
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
I am just focusing on flow chart. as I told you making flow chart is very difficult for me . I am having problem to make flow chart. I tried to make another flow chart. this flow chart is incomplete and I need help to complete this flow chart. if you see the flow chart there are four switches first I am checking which one is pressed
if switch 1 is pressed than display number '1" on LCD.
if switch 2 is pressed than display operator "+" on LCD.
if switch 3 is pressed than display operator "2" on LCD.
if switch 4 is pressed than display operator "result" on LCD.
after that I think I have to convert text to binary numbers but I have know idea how to convert and store numbers ?
upload_2017-7-27_11-18-35.png

what is first things to draw then next .. next.. next?
 
Top