Welcome, %1$s. Please login or register.

Login with username, password and session length
 

Author Topic: Basic Arduino coding Part 1 - Blinking LED's using delays.  (Read 3843 times)

Offline zhaith

  • Commander
  • ***
  • Posts: 189
Basic Arduino coding Part 1 - Blinking LED's using delays.
« on: September 04, 2014, 01:38:24 pm »
Heya folks, over the last couple of weeks i've had a few PM's asking for assistance with using arduino's  for  use in model lighting, rather that just posting links to my completed code I thought it might be a nice idea to start a guide so that those interested can have a better understanding of the code I've written but to be able to expand on the code in these example so that you can customize it to exactly how you want it to work.

First of all please be aware that i'm pretty new to this so there may well be better ways of doing things than I am doing them, but it works for me and if I can help you avoid some of the problems i've encountered so far then this exercise will  be worth it. The aim is to try and provide the basics in a friendly environment, from experience i have found some of the arduino forums to be not very newbie friendly at all :(.

I'll post the complete code for each section at the end of each post so all you need to do it cut and paste it into the arduino software and upload it to your chosen type of arduino, i'm using the Uno for this but the code will work equally well on the nano and pro-mini versions.

Any feedback will be gratefully received so feel free to add comments/suggestions.

An on a final note - for examples that involve a single LED, i'll be using the on-board LED connected to pin 13, as far as i'm aware both the nano and pro-mini also have this, if yours doesn't then you'll need a LED and appropriate resistor for this.

So first up a few basics

Arduino programs are referred to as "sketches" I have no idea why but if there's anything that you can think off that you want a sketch to do i'm pretty sure someone else will have also thought of it and somewhere there will be an example of code close to what you want, there literally thousands of them that you can adapt to suit your needs :).

Each sketch is generally made up of three parts, at the start of the sketch the constant/variables are defined, the next part is the setup, any code in the setup will be executed when the program starts and anything in the setup will only run once, the final section is called the loop, this is where the bulk of the code written can be found, any code in the loop will run continuously until the reset button is operated or the arduino is turned off.

So for the first example we'll make a LED blink on and off once every second

The first thing we need to do is assign a pin on the arduino that the LED will be connected to, in this example it will be pin 13 (which has the onboard LED connected to it) this will be a constant and thus will never change.

const int  ledPin = 13        This line assigns the constant ledPin to pin 13 on the arduino, this enables you to use "ledPin" to refer to pin 13 at any point in the sketch, you can call it whatever you like but it's best to stick to something that makes sense to you.

We now move onto the setup, you'll notice the term void in front of it i'll explain this in a later post, for now just be aware that it's there and needed.

void setup(){
     pinMode(ledPin, OUTPUT);
  }

Notice that the code is inside parenthesis {}, it's important that all the code in both setup and the loop are contained inside these, incorrect placement will cause your sketch to fail to compile (convert from a language we can understand to code the arduino understands)

pinMode - configures a specific pin on the arduino to be either an input or output
(ledPin, OUTPUT); - this tells the arduino that pin 13 which we previously defined as ledPin will be an output, you could also write it as pinMode(13, OUTPUT);

The final part of the sketch follow, the loop.

void loop() {
  digitalWrite (ledPin, HIGH); 
  delay (1000);         
  digitalWrite (ledPin, LOW); 
  delay (1000);
}               

The 1st line will switch on the led using the digitalWrite command, with the value set to HIGH, the arduino will output voltage to the specified pin
The 2nd line will make the program wait for 1 second before it will process another command, the use of the delay() function has some downsides which i'll cover in a later post.
The 3rd line will then switch the led off, by setting the value to LOW, the arduino will no longer output voltage to the LED.
And finally the 4th line will make the program wait another second.

The program will then repeat the code continuously resulting in the LED flashing on and off at one second intervals.

Below you'll find the full code, just copy and paste into the arduino software, once uploaded you should see the led on your board flashing, you can change the flash rate on and off by simply changing the delay time.

Hopefully this isn't too confusing, it's not the easiest thing to put down in writing if i'm honest.

Bye for now.


Quote
// define constants, these values will not change
// and are generally used to configure inputs/outputs

const int ledPin = 13;  /*set the arduino pin that the
LED in this example will be connected to.*/

/*setup - after bootup all code in this section will only execute
once */

void setup() {
  pinMode(ledPin, OUTPUT); //sets the LED pin as an output
}

/*The loop is where you will  find the main guts of the program.
This code will run continuously */

void loop() {
  digitalWrite (ledPin, HIGH);  //Turns on the LED
  delay (1000);            //program waits for one second (1000ms)
  digitalWrite (ledPin, LOW);   //Turns off the LED
  delay (1000);                 //program waits for one second
}


Offline Tankton

  • Admiral
  • *****
  • Posts: 2174
    • Cameron's Decals and Such
Re: Basic Arduino coding Part 1 - Blinking LED's using delays.
« Reply #1 on: September 04, 2014, 08:32:45 pm »
This is going to be very helpful to us.
Thank you.

Feed us all you can.
Live Long and Prosper

Cameron Lewis Sr.

www.cameronsdecals.com

Offline billski

  • Admiral
  • *****
  • Posts: 785
  • Jolan'tru
    • YouTube channel
Re: Basic Arduino coding Part 1 - Blinking LED's using delays.
« Reply #2 on: September 05, 2014, 12:06:30 am »
excellent info. For myself i know nothing about arduino never mind sketches but i would love to try it. What would a complete novice like me need to buy to get started?

Offline AussieTrekkie

  • Admiral
  • *****
  • Posts: 698
Re: Basic Arduino coding Part 1 - Blinking LED's using delays.
« Reply #3 on: September 05, 2014, 12:20:42 am »
To gat started all you need is an Arduino Nano or Mini and a breadboard.

You can pick up the Arduinos on eBay for just a few dollars.
The Nano plugs straight in to the computer, but the Mini needs what is called a breakout board to program it.

I've had problems programming the mini with a breakout board, just doesn't work for me. It does for everyone else though.
So I just use the Nano.

Thanks for putting up this thread Phil. I will be watching it to keep learning how to program these things.

Scott.

Offline manu

  • Captain
  • ****
  • Posts: 273
Re: Basic Arduino coding Part 1 - Blinking LED's using delays.
« Reply #4 on: September 05, 2014, 02:34:31 am »
Great zhaith!
if you want I can contribute some code and explanation "without delay" for independent blinking of several LEDs. But I don't want to hijack your thread.

Offline zhaith

  • Commander
  • ***
  • Posts: 189
Re: Basic Arduino coding Part 1 - Blinking LED's using delays.
« Reply #5 on: September 05, 2014, 04:24:06 am »
Thats already planned bud but thanks for the offer, i want to be able to show why delays have issues and the follow up with code that does the same but avoids the use of delays completely :)

Offline Sergio

  • Admiral
  • *****
  • Posts: 630
Re: Basic Arduino coding Part 1 - Blinking LED's using delays.
« Reply #6 on: September 05, 2014, 06:12:19 am »
Thanks for sharing this information! Totally newbie friendly :)

Quarky

  • Guest
Re: Basic Arduino coding Part 1 - Blinking LED's using delays.
« Reply #7 on: September 05, 2014, 06:12:49 am »
Brilliant! Thank you for taking the time to do this for us nooks!

... OK daft question warning!

If I have an arduino and plug this into my computer, is there a program I have to install to write the sketches and save you the board?  Or does this auto load from the board...?

Am waiting for 3 nanos to arrive in the post ;)

Offline simi

  • Admiral
  • *****
  • Posts: 639
  • JUST enough knowledge to do some serious damage
Re: Basic Arduino coding Part 1 - Blinking LED's using delays.
« Reply #8 on: September 05, 2014, 06:38:51 am »
Hey Quarky,

You can download the IDE (software to write the software and deploy to the processor) on the link below.  The site there also has a bunch of examples that you can follow.  The IDE also comes with most of the samples that you can use as examples.  Here's my thoughts - once you get comfortable with a couple of core concepts - it's very easy to work with and you can control alot of things!  I was amazed on the availability/cheap cost of components that are not even (or could be I guess) model related like light sensors, motion sensor, sound sensors, IR sensors, etc...pretty cool stuff.

Also - there might be a small struggle getting your PC to "recognize" the Arduino - but there's a bunch of online support for that and once it does - you're off to the races.

Start simple and you'll quickly learn the core concepts -then go nuts!

http://arduino.cc/en/Main/Software

PS: Google is your friend if you run into issues!


Cheees!
As a software architect, I'm pretty darn good.  As someone with knowledge of building things in the real world, well, I'm a software architect.

Offline manu

  • Captain
  • ****
  • Posts: 273
Re: Basic Arduino coding Part 1 - Blinking LED's using delays.
« Reply #9 on: September 05, 2014, 06:53:56 am »
Quote
PS: Google is your friend if you run into issues!

And of course SFMA is your friend!! ;D

Offline Mstowell

  • Admiral
  • *****
  • Posts: 738
  • Gort! Barenga!
Re: Basic Arduino coding Part 1 - Blinking LED's using delays.
« Reply #10 on: September 05, 2014, 01:35:58 pm »
excellent info. For myself i know nothing about arduino never mind sketches but i would love to try it. What would a complete novice like me need to buy to get started?

I want ahead and purchased the following from Sparkfun.com:  https://www.sparkfun.com/products/12001
It is a bit of overkill for what we typically do for our models, but I thought it would be a good way to get my feet wet, and I was right.  I now have a much better understanding about how to program Arduino.
I tried to go the route of buying breadboard and components, but did not know enough to know how to get started.  I didn't know enough to know what I didn't know, if that makes sense.
This kit give access to many sample sketches.
I don't regret spending the money on this kit and I now have a test bed that I can easily use to work up programs and lighting layouts.
Just my thoughts on how to get started.
Matt

Offline Kila2000

  • Trainee Borg
  • Admiral
  • *****
  • Posts: 877
  • I will finish a model, I will finish a model ...
    • Models by Wil
Re: Basic Arduino coding Part 1 - Blinking LED's using delays.
« Reply #11 on: September 05, 2014, 10:32:09 pm »
I for one am keen to learn Arduino so this thread will be of great value.
Had tons of people telling me what I should get, but none ever took the time to explain the coding side of things  ;D
Wil, the Kiwi Borg, in training.

I'm not a complete Borg, some pieces are missing.

 




Enterprise © Bloc