Jump to content

Getting to grips with programming an Arduino Uno


Recommended Posts

As the title suggests, i have a land rover based electronic control project in my sights, which will run alongside my university project (both are unrelated but the arduino will be used with both) and i have never played with this sort of stuff before, barring playing around on a picaxe 20m2 programming software (just making a light flash on and off)

the picaxe uses some form of simple programming language where the arduino apparently uses C language... whatever that is.

does anyone have any previous with programming in general, c language and arduino microcontrollers?

i will be posting up what i learn here as i go and would also appreciate any tips on it :)

cheers

Link to comment
Share on other sites

Assuming you have a "knack" for it, your best bet is to start with sample projects and figure out what works/what doesn't. there are many, many resources for C programming in general but for embedded device programming you don't normally need to learn many of the nuances as most of what you do will be more hardware specific so most general C examples will not make sense.

For micro controllers try and avoid running code in the "main" loop, using examples of event/interrupt driven projects will help you grasp the basics of the interface between hardware and software while at the same time let you concentrate on small chunks of independent code. It's a common mistake for beginners to just create a loop to run all their code but interrupt/event driven code is far more efficient and simpler in the long run, just takes a bit of getting used to. The principal should be that the hardware should call your code when something changes or a timer ends rather than your code constantly looking to see what has changed as that is slow and inefficient.

I use MicroChip controllers for all my projects and their free IDE that includes a C compiler, demo boards for specific PICs make it very easy to start as you can concentrate on the software rather than "debugging" the hardware. Once I have a design running on one of the demo boards I then refine the hardware design and build it onto a custom PCB.

If you want a hand with anything specific give me a shout but the best thing to do is start with a project in mind and learn as you go. It will seem daunting at first but C is a pretty straight forward language as programming languages go and most compilers will warn you of any obvious mistakes as you go.

Link to comment
Share on other sites

What DaveW said.

The K&R bible is well worth having as it's the definitive reference and looks good on your bookshelf, the Absolute Beginners Guide To C is pretty good to get you started and explains some of the foibles nicely.

C is probably the most popular / widespread / universal / useful programming language so worth learning, just be aware that the arduino native development environment hides some of the real goings-on from you.

Check out hackaday.com and dangerousprototypes.com for various projects and things, and the Stack Exchange group of sites are pretty good for getting help: arduino.stackexchange.com for Arduino specifics, stackoverflow.com for general C programming related questions, and electronics.stackexchange.com for the hardware side.

Link to comment
Share on other sites

Arduino is (intentionally) very easy. While I agree with what dave says about making code event driven using interrupts, and you can do this on an Adruino, for the size of projects you can fit on it - and the speed you need to run them it's rarely worth the effort.

'C' is just a programming language. Many other languages are variations on C, sharing the same basic syntax & structure. The arduino flavour of C is much simplified and very easy to learn. Most of the complicated stuff you might want to do with it - such as talking to an LCD - is available in 3rd party libraries which you can download & use.

I've used several Unos and Megas in various projects (including my scary segway most recently which used an Uno) It used a mixture of a main loop and interrupts. The Mega is essentially the same as an Uno in terms of coding - but uses a different processor which has more storage and I/O lines.

Download the IDE here: http://arduino.cc/en/main/software#toc2

Plug in your Arduino. You may be prompted to install a USB driver - which you will have to download. It will configure the Arduino to talk to the PC via a virtual Com Port. In the IDE, go to the tools menu, select that com port, then in the tools menu, make sure 'Board' is set to Uno (or whatever flavour you are using).

Start with one of the bits of example code, click the upload button (next to the tick icon) - and it should upload & run.

If you need any specific help - you never know, I might know the answer! Otherwise Google is your friend!

Si

Link to comment
Share on other sites

thanks for all the kind words and linkies guys, it does seem like it will be fairly daunting at the moment but i feel fairly reassured that i have a lecturer or 2 who know their stuff when it comes to programming and combined with the help and interest from here i think i can make things work :) unfortunately i cant share details of my dissertation project, however i can share what i have learned with all of you and apply it to the land rover based project which i will run alongside it.

time to start reading i think! I have worked out the formulas that i need to write to the uno, and what inputs/outputs i would like to achieve and use, although havent got a clue where to start at the moment.

their website seem to have a good few "getting started tips," i think ill start with getting an LED to blink :hysterical:
if i can master that then my project should be a breeze!!! :blink::blink::ph34r:

Link to comment
Share on other sites

I haven't done any programming for years so I'm well out of date but I used to love it due to the instant feedback you got and how quickly you could change things to get improvements. Getting it to work was usually the easy bit but you could spend ages trying to refine it and make it fool proof. I used to get really picky about doing it with the least lines of code and having no repetition, many a night hour spent in a dark room. :rofl:

Good luck with the project!

Link to comment
Share on other sites

I'm watching this with interest. I purchased a Mega board and started reading, but not got much further yet. The inspiration for this was seeing the ECU controller done for the Bosch PWM fuel pumps, with the dream of fitting an om606 into the 90!

My first project planned was to build a hydraulic pressure tester / data logger. The first issue was that the Mega has a 0 to 5v input and most transducers operate with a 4 to 20 mA output. I have seen several circuits using resistors to convert the mA signal to voltage, but realise there are issues if the sensor becomes disconnected, causing over voltage on the input. Do any of you programming adults know of a shield for the mega?

Sorry discomikey I don't mean to hijack your thread! What project do you have in mind Mikey?

Link to comment
Share on other sites

I was using a sort of C+ on the Oopic for the fish. Calling functions from the library was good ("oopic.servo" sort of thing), but it only really works on the main pic if you only send it to a position. If you are moving servo's all the time it becomes awkward, so the first thing to add-on was a servo driver chip. Next was another oopic, so one does control, and other other does the primary thinking on interrupts. LED indicators were good, (flash code) but a two line LCD display is better and so on.

When you start to sniff around other folks projects you find multiple pics or stamps pre-processing stuff for a cpu. Then you see the car guys had the same problem and have canbus, which is another version of serial comms like I2C, or USB. As I see it is because reading high speed inputs like ABS encoders and crank positioning systems really blocks up the main processor, so it's best outsourced. Then your primary code only has to deal with edited information :)

Link to comment
Share on other sites

I have a few DUE's and an UNO. Using the UNO as a dialler. The arduino platform is very good IMHO as it's very easy to use although there is some complaints that the overhead of the standard libraries can slow things down a little but that it what makes it so easy to learn. And it has a very wide following so copy and pasting others guidance and using other libraries is very helpful especially if you just want to get it to do something specific / repetitively.

Link to comment
Share on other sites

Never used an Arduino, but I do software development in a C-related language for a living. If you have any questions, I'm sure there's a few of us on here that can help out.

What's your LR project?

Thanks all for the posts, hopefully should be getting on this in the next week or so, just got to finish these neverending load case diagrams first,

as for the LR project you will all just have to keep an eye on the thread and ill keep dropping hints as it unfolds, it will be good to see if anyone can guess what it is as i start writing etc :)

Link to comment
Share on other sites

I had an automatic horn function the other day, driving through Newport town center, indicators self cancelled and the horn stuck on, somethings shorting out, needless to say I got some funny looks when I had to pull over unscrew the grill and yank the wires!

but no, you will never guess haha :)

Link to comment
Share on other sites

  • 2 weeks later...

so more on the LR based project, i have decided to use Picaxe 20m2 chip for this as i have one which i had to build and programme for a uni assignment and therefore its free haha

alongside my assignment i have been writing this:

can you tell what it is yet?

post-13725-0-33502500-1417354312_thumb.png

:D

a rather large book on arduino programming has been borrowed from the library too, however im putting that off until the end of this week as i fear learning basic and C language at once may confuse me slightly!

this took me around 3 hrs to piece together from trial and error and using the commands manual on the picaxe website.

i have no idea on what the PWM settings should be and the numbers 100 and 102 are just random numbers which will be altered once inputs are configured and i know what i want.

i fear that the b1+1/ b1-1 may be too small of a change so may alter that to 5 rather than 1, or something.

i have no idea what the input pulse of C.2 will be as of yet, nor what motor i will be using for an output so these are due to change.

any more educated guesses?

Link to comment
Share on other sites

muhahah no one has guessed it yet, and elbekko, i should hve proabably made it more clear but yes that is basic running a picaxe 20m2 board rather than an arduino that i originally thought to use. i will be learning c language next for my IP and posting here along the way.

do you want a clue?

C.4 is the brake light switch

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We use cookies to ensure you get the best experience. By using our website you agree to our Cookie Policy