Author Topic: Arduino Calculator  (Read 20777 times)

0 Members and 1 Guest are viewing this topic.

Offline BillyD

  • Regular Contributor
  • *
  • Posts: 218
  • Country: ie
Re: Arduino Calculator
« Reply #50 on: September 01, 2016, 01:12:46 pm »
just read that piece of code you posted. this is very flat and basic C, it is very easy to read and could be a good starting point for your learning curve. You cannot expect to get an answer what to type where here, because the reason of most if not all people being here is the curiosity of how things are made up and tied together.
first of all, easy for you to say, you probably studied c in univirsity or something. secondly, r u saying people dont know the answer to my question? cause it seems like people like you know it pretty well. as i said, if you dont want to give an answer, then leave my thread.

OK, I would put the entire buzz code right after the key is first detected and before it is processed.

So, right after:
  if (key != NO_KEY)
  {

Exactly where you put:
    digitalWrite(8, LOW);


Do the entire beep right there.  Maybe put a delay(25) between turning the beep on and turning it off.

Here is a page describing how to get a beep out of a piezo, if that is what you are using:
http://www.hobbytronics.co.uk/arduino-tutorial7-piezo-beep

For a piezo, there's a little more to it than just setting a pin low and then setting it high.  If you are using a buzzer then then turn it on, delay(25) and turn it off.
finally a solid answer, thank you so much. I will try it out. Thanks once more! :)

With respect, there were quite a few solid answers before this one which you chose to ignore and, in at least one case, direct an insult at the person who helped you.

This might be enough to get you through this assignment but in the longer term you might want to rethink the way in which you approach problem solving and dealing with people.

 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
Re: Arduino Calculator
« Reply #51 on: September 01, 2016, 04:10:49 pm »
Here's a suggestion:  If you think the beep idea I mentioned above is worthwhile for your project, try it out on a 10 line program.  Don't try it out on a 500 line program.  I always write my code in bits and pieces and string them together later on when I understand how the components will work.

I would get the beep working.  Then I would get the LCD working, separately.  Then I would look around for other chunks and get them working.  Finally, I would stitch them together.  And I would save the sample bits, they will  come in handy in the future.

Overall, the process is know as Top Down Design (start with the big idea at the top) and Bottom Up Coding (write the device drivers first).  Build up from the foundation.  There is also a process known as 'step-wise refinement' (Niklaus Wirth).  Get something working, improve it and try again.

True story:  When I first learned FORTRAN back in '70, I had a fellow trying to help me get started.  I knew what I wanted to do but I had no idea how to do it.  I had seen the computer but I hadn't dared touch it!  He mentioned vectors and arrays and I felt like the stupid step-child.  He might as well have been on a different planet.  I simply didn't have the background, at that particular moment, to understand what he was saying.

So, he stuck with me for another hour or so and I finally caught on.  First you crawl, then you walk and finally you run!  And I bought some books...  Still have them, 46 years later!  Once the light comes on, all this stuff becomes clear.

 

Offline edy

  • Super Contributor
  • ***
  • Posts: 2385
  • Country: ca
    • DevHackMod Channel
Re: Arduino Calculator
« Reply #52 on: September 01, 2016, 06:51:06 pm »
I read through the whole thread as well. I agree with some of the comments regarding posts that ask for answers that otherwise seem apparent. I guess the main issue is the OP admits to not having much coding experience, and thus getting over their head with a project that requires coding experience to debug (in case anything goes wrong) and not showing that they even attempted to solve the problem.

As far as the code goes, I would just look a bit into the main loop where it says under "//read keypad" there is a line that says "if (key != NO_KEY)". I would simply add ANOTHER line just before that which handles the beep:

if (key != NO_KEY) then BEEP!!!!

So if there is NOT a NO_KEY press (double negative meaning something *IS* pressed) then just do your beeping there. The program then continues on to the remaining code and asks the question again (the if NO_KEY thing) and handles whatever it needs to handle.

Of course, the BEEP sub-routine will have to be figured out. You need to make a small test program to see how you are going to implement it, on what pins, and how.

This calculator program is not hard, but if you know little about coding then it will look like a daunting task. First step is to make SMALL PROGRAMS to blink LED's, accept button inputs, make buzzers work, etcetera. Then do keypad reading (this Calculator seems to be using a matrix-style approach to figure out the rows and columns to read in the keys, with a loop).

Like a previous post mentioned, without us having the same setup as you, and spending the time to configure it all the same, we would not know if our solution even is correct or if there is another bug somewhere. You would be running in circles if you are just looking for answers. For any such problem you need to be able to debug and work on it independently quite a bit and learn along the way, for your own good.

YouTube: www.devhackmod.com LBRY: https://lbry.tv/@winegaming:b Bandcamp Music Link
"Ye cannae change the laws of physics, captain" - Scotty
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
Re: Arduino Calculator
« Reply #53 on: September 01, 2016, 09:11:39 pm »

This calculator program is not hard, but if you know little about coding then it will look like a daunting task. First step is to make SMALL PROGRAMS to blink LED's, accept button inputs, make buzzers work, etcetera. Then do keypad reading (this Calculator seems to be using a matrix-style approach to figure out the rows and columns to read in the keys, with a loop).

Like a previous post mentioned, without us having the same setup as you, and spending the time to configure it all the same, we would not know if our solution even is correct or if there is another bug somewhere. You would be running in circles if you are just looking for answers. For any such problem you need to be able to debug and work on it independently quite a bit and learn along the way, for your own good.

And there is no way in the world a newcomer is capable of working on a 500 line chunk of code.  That was my point with the FORTRAN story.  It might as well come from an another planet!

It's sometimes too easy for those of us who have written tens or hundreds of thousands of lines of code to remember where we began.  My first experience was humiliating!  I remember it vividly!

If there is going to be a beep() function, the call might as well come right after the existing IF statement.  Whether buzzer or piezo, a function is the way to go but that implies a little more coding skill.  Not a lot and there are certainly examples but when you start from nothing, everything is a challenge.

 

Offline rrinker

  • Super Contributor
  • ***
  • Posts: 2046
  • Country: us
Re: Arduino Calculator
« Reply #54 on: September 02, 2016, 12:53:59 am »
 I said much the same in my reply. Start small, add on. Build a program that makes the beeper sound, Then make it make the beeper sound when a button is pushed. Try the examples that comes with various libraries, like the one for the I2C LCD. That's how I figured out how to use an I2C LCD. Then take each of those small known to work bits and start combining to build up the final project.
 You are absolutely right, a rank beginner is going to look at a 500 line program and just glass over. If you don't know the basics, you can't see how all that code works together. Byte sized (ha!) chunks is the way to go. It's the way the tutorials are set up even, and every book I ever learned from, going back to when I first learned BASIC on the TRS-80 Model 1, my first experience at programming. And I'm always learning - I'm relatively new to Arduino. C, I taught myself a long time ago, but rarely ever used it, so this is like a whole new thing. Granted, once you know another language or two, adding more is much easier than the initial learning of the first one, but it's not like I opened the box and started creating 500-100 line projects. I'm building up to the final products I want to make with Arduinos, not trying to solve world peace on my first day.

 

Offline tatus1969

  • Super Contributor
  • ***
  • Posts: 1273
  • Country: de
  • Resistance is futile - We Are The Watt.
    • keenlab
Re: Arduino Calculator
« Reply #55 on: September 02, 2016, 09:17:28 am »
I agree to the point that it is difficulty to start learning a programming language with a piece of code that had not been made for educational purposes, and is neither very structured nor heavily commented. But I am convinced that this is not the road block that the OP is facing here, because of several reasons:

- one of my sons, aged 13, is very (too) much computer affine, but has no experience in any programming language. I would say he has learned most about computers playing Minecraft, going into the deepest depth of redstone magic. But he wants to learn a programming languague, and about three weeks ago I showed him how to start with Visual C++. That ended with a small console program that would print out something, but only if the right key is entered. Pretty close to the beep problem here  ;) I showed the 'calculator.txt' file to him. After skipping a few pages on the screen, he immediately identified that 'if (key != NO_KEY)' as the place to start.

- the OP seemed to get upset immediately after the first posters suggested to gain more programming and Arduino knowledge that would help him find the solution himself.

- he apparently has put that hardware project together, connected the additional buzzer, and figured out that 'digitalWrite(8, ..);' manipulates it. He talks about having tried out things, so he will also have the toolchain up and running. So he had already put quite some things together, facing the effort and showing the ability to learn there.

He also seems to have left this thread after he got the answer he expected, which text to put into which line.
We Are The Watt - Resistance Is Futile!
 

Offline rrinker

  • Super Contributor
  • ***
  • Posts: 2046
  • Country: us
Re: Arduino Calculator
« Reply #56 on: September 02, 2016, 07:32:08 pm »
 Perhaps true, but some people have the logical organization to immediately pick up on programming type problems and some do not. The first group is like your son, and somewhat myself. The second group - they aren't going to figure out anything staring at a complex program but that doesn't mean they can't learn to program. They just need to go about it differently.
 There seems to be a high correlation between musical ability and mathematical ability - makes sense since music is highly mathematical - well, I'm one of the exceptions. As long as a piece is not too complex, I can read sheet music, but actually tell you what it means? Or listen to a key being struck on a piano and tell you what note? Or that this note is exactly 1 octave higher than that note? Not a clue. I can use CAD or various hand tools and draw schematics, scientific diagrams, or mechanical drawings, but I couldn't draw a picture of a cat if my life depended on it.

 No, based on another thread the OP started, I think he was looking for someone to do his homework for him. Thus the rejection of all hints without giving the exact answer as people being harsh and unhelpful. Back in college I tutored for the FORTRAN class all engineers had to take back then. It was pretty easy to tell who was coming because they actually wanted to learn how to do the work and who just wanted to be shown the answer. I've carried that through in other forums I participate in, I tend to not immediately post just the answer, but as simple an explanation as I can come up with on how to derive the answer. A good example, it's fairly common in model railroading when using DCC control to use white LEDs for headlights and so forth, and with standard DCC voltages the correct value for a current limiting resistor is 1K. Unless you need to dim the LED down for some reason, 1K is pretty much always the answer - you net about 9ma through a 20ma peak LED< perfectly in the safe zone. But when someone posts the question of what resistor do I need, I don't just reply with 1K - I put in a short explanation of Ohms Law and how you GET 1K as an answer. Hopefully the poster learns something and when they need to use the same LED but with a 5V power supply they can figure out that 1K is too much and calculate a correct value.

 
 

Offline Brumby

  • Supporter
  • ****
  • Posts: 12298
  • Country: au
Re: Arduino Calculator
« Reply #57 on: September 03, 2016, 12:57:15 am »
Agreed.

Sometimes giving direct answers without imparting understanding is a bad thing.  The inquirer may take the answer given for a particular set of parameters and then try to apply it in a completely different - and inappropriate - scenario.  This could end up with damaged components or people.

.... and the blame will most probably be laid at our feet - something that tends to go with impatient and demanding attitudes.
 

Offline skillz21Topic starter

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: au
Re: Arduino Calculator
« Reply #58 on: September 03, 2016, 02:00:06 am »
Ok, i finally got the beep working. so now i have the problem of the two middle rows of the keypad not working. any idea why?
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12860
Re: Arduino Calculator
« Reply #59 on: September 03, 2016, 02:11:55 am »
I believe you are using a 3rd party keyboard library, so the most likely causes of problems are:
- Your code isn't handling the returned keypress value correctly so some values have no effect
- You've tried to use some I/Os needed for the keypad for other purposes
- your keypad has a hardware fault.

Make a copy of your current code (sketch) and strip out all the calculator stuff.  Replace it with code to simply display the last key pressed.   If that works for all the keypad rows, the bug is in the calculator code, otherwise its hardware or an I/O conflict.  Carefully make a list of what's using each I/O pin of the ATmega328P - Caution: some I/O pins go to more than one pin on the Arduino headers.  If you dont find any conflicts, check that any initialisation code that runs after the keypad initialisation doesn't change any of the keypad pins.
« Last Edit: September 03, 2016, 03:15:42 am by Ian.M »
 

Offline skillz21Topic starter

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: au
Re: Arduino Calculator
« Reply #60 on: September 03, 2016, 02:55:28 am »
i changed the code a bit so that i dont have to use the i2c. then i realised if i used the original code, i would not have enough i/o pins for the buzzer. so i changed the code to use less of the pins on the lcd, (using D0, D1, D2, D3 instead of D0, D1, D2, D3, D4, D5, D6, D7) the lcd didnt display anything at all. i tried using this hello world code.
#include <LiquidCrystal.h>

LiquidCrystal lcd(2, 3, 13, 8, 9, 10, 11);

void setup() {
  lcd.begin(16, 2);
  lcd.print("hello, world!");
}

void loop() {
  lcd.setCursor(0, 1);
  lcd.print(millis() / 1000);
}
once again, it didn't work, am i just missing something?
oh yeah, with the keyboard, it works perfectly, i tried it with a sketch, and the buzzer beeped every time i pressed a key.
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12860
Re: Arduino Calculator
« Reply #61 on: September 03, 2016, 03:11:25 am »
I changed the code a bit so that i dont have to use the i2c. then i realised if I used the original code, I would not have enough i/o pins for the buzzer. so I changed the code to use less of the pins on the lcd, (using D0, D1, D2, D3 instead of D0, D1, D2, D3, D4, D5, D6, D7) the lcd didn't display anything at all.
...
Oh yeah, with the keyboard, it works perfectly, I tried it with a sketch, and the buzzer beeped every time I pressed a key.
Hitachi HD44780 compatible LCD 4 bit mode uses D7 - D4, with D3 - D0 left unconnected.

Glad to hear the keypad's OK.  It sounds like there is either an initialisation conflict or a problem with the calculator key handling code.
« Last Edit: September 03, 2016, 03:14:54 am by Ian.M »
 

Offline skillz21Topic starter

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: au
Re: Arduino Calculator
« Reply #62 on: September 03, 2016, 03:38:47 am »
ok it works perfectly now! (without i2c)
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12860
Re: Arduino Calculator
« Reply #63 on: September 03, 2016, 04:02:00 am »
On an Arduino Uno R3  the dedicated SDA and SCL pins next to AREF on the top left header are also wired to A4 and A5 on the bottom right header.  If you were attempting to use A4 and A5 for the keypad, hardware I2C using the Wire library would have caused a conflict with the keypad library.
« Last Edit: September 03, 2016, 04:03:58 am by Ian.M »
 

Offline skillz21Topic starter

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: au
Re: Arduino Calculator
« Reply #64 on: September 03, 2016, 04:13:49 am »
i was using the dedicated SDA and SCL pins
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12860
Re: Arduino Calculator
« Reply #65 on: September 03, 2016, 04:33:36 am »
... but were you also trying to use A4 and A5 as that doesn't work?
 

Offline skillz21Topic starter

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: au
Re: Arduino Calculator
« Reply #66 on: September 03, 2016, 05:33:44 am »
... but were you also trying to use A4 and A5 as that doesn't work?
I can't use A4 and A5 as they were being used by the keypad.
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12860
Re: Arduino Calculator
« Reply #67 on: September 03, 2016, 05:40:44 am »
A4 and A5 are connected to the same ATmega328P pins as SDA and SCL.  If you are using SDA and SCL for I2C, you *C*A*N*N*O*T* use A4 and/or A5 for *A*N*Y*T*H*I*N*G* else!
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
Re: Arduino Calculator
« Reply #68 on: September 03, 2016, 03:11:07 pm »
So, I went looking for the Uno schematic.  The first I found didn't show the dual use of A4 and A5, it didn't show the I2C connections at all.

This version clearly shows that A4 and A5 are used as SDA and SCL respectively:
http://download.arduino.org/products/UNO/Arduino-UNO-Rev3e-SCH.pdf

When you read a user manual for the chip, you may find these pins referred to by their port/pin as PC4 and PC5.  Just to toss in a little more confusion...
 
The following users thanked this post: Ian.M

Offline skillz21Topic starter

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: au
Re: Arduino Calculator
« Reply #69 on: September 04, 2016, 04:32:10 am »
A4 and A5 are connected to the same ATmega328P pins as SDA and SCL.  If you are using SDA and SCL for I2C, you *C*A*N*N*O*T* use A4 and/or A5 for *A*N*Y*T*H*I*N*G* else!
Woah... ok, that explains it.
 

Offline skillz21Topic starter

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: au
Re: Arduino Calculator
« Reply #70 on: September 04, 2016, 04:35:05 am »
Thanks so much guys i finally got the calculator fully working with i2c. one last problem though, the lcd backlight doesn't light up, it lights up with the normal code (without i2c).
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12860
Re: Arduino Calculator
« Reply #71 on: September 04, 2016, 06:45:17 am »
How is the backlight controlled?
Without your current schematic and code we can only guess.  It  could be another conflict like the one between the I2C pins and the keypad middle rows, or maybe, due to the complexity of the calculator code, you've made a mistake and its never reaching the routine that turns the backlight on.
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
Re: Arduino Calculator
« Reply #72 on: September 04, 2016, 03:06:40 pm »
Check whatever documentation you have for the LCD and see if there is a command sequence required to turn on the backlight.
 

Offline skillz21Topic starter

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: au
Re: Arduino Calculator
« Reply #73 on: September 05, 2016, 09:38:21 am »
Here's the code i'm currently using. (once again, ignore the info at the top, i am using i2c)
 

Offline alexanderbrevig

  • Frequent Contributor
  • **
  • Posts: 700
  • Country: no
  • Musician, developer and EE hobbyist
    • alexanderbrevig.com
Re: Arduino Calculator
« Reply #74 on: September 05, 2016, 09:53:15 am »
Rw_pin and rowPins[0] are the same. The same is true for D7_pin and colPins[1] and D6_pin and colPins[2].
Does it currently work?
« Last Edit: September 05, 2016, 09:55:27 am by alexanderbrevig »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf