Arduino programs

Anything to do with programing the Arduino Platform.
Post Reply
CLYON
Posts: 35
Joined: Dec 2nd, 2012, 8:52 am

Arduino programs

Post by CLYON »

We probably need a spot just for programming, but I put it here. I know just enough about programming Arduino to get me in trouble. The simplest things seem to confuse me. Can someone explain when to use these brackets {}? I will give a couple of examples.

void loop()
{
something happens here

something happens here
}

or

void loop()
{
{something happens here}

{something happens here}

}

For the most part both will run the same. I have had trouble with IF statements where it just doesn't act right if you don't put that block of code in brackets. Can someone explain when to use and when I can get by without them?

Chuck
a_shorething
Posts: 289
Joined: Sep 10th, 2013, 5:26 pm
Location: New Jersey Shore

Re: Arduino programs

Post by a_shorething »

You're right, there should maybe be a forum for Arduino or maybe control type boards.


For programming you're better off taking some example code from the Arduino main site that is close to what you want (like the serial com example or maybe a motor control sketch). I am a programmer and almost never 'start from scratch' on any programming. It's easier to start with something that already works and modify it.

To answer your question though, it goes like this:

void loop()
{
something happens here;

something happens here;
If (a==b)
{ do this;
}// end of if condition

}// end of void loop


Don't forget the semicolons after each instruction and whenever you use the equals signs, one set means assignment and a double set is a comparator.

This means if you use one, it sets a to the value of b but if you use 2 it compares their values.

The single equals WILL screw you up and it's one feature of C# and Arduino that still screws me up.
CLYON
Posts: 35
Joined: Dec 2nd, 2012, 8:52 am

Re: Arduino programs

Post by CLYON »

I did look at other programs and that is what thru me off. Some programs like blinking an LED only have brackets after void loop and at the end. Other programs have brackets throughout. The definition I found was to put "blocks of code within brackets". I will try and get two examples later and get someone to explain the difference. Thanks.

Chuck
CLYON
Posts: 35
Joined: Dec 2nd, 2012, 8:52 am

Re: Arduino programs

Post by CLYON »

OK, go to the Arduino site http://arduino.cc/en/Tutorial/HomePage. Look at the two programs, blink, and button. Why is the blink program only using one set of brackets and the button using two sets? Why couldn't the button program use only one set.

Chuck
a_shorething
Posts: 289
Joined: Sep 10th, 2013, 5:26 pm
Location: New Jersey Shore

Re: Arduino programs

Post by a_shorething »

I guess you mean like this:

// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;


Why aren't the parts after the 'if' statement wrapped? The answer is I don't know but I don't know if this will compile without errors. I don't think it would, but I may be wrong.

I think it's just because the guys that wrote the compiler tried to make it less like a rigid programming language and more like pseudo-code. I'm sure there is a pre-processor step that goes in and attempts to put the curly brackets in there.

My advice: Use them everywhere and you can't go wrong.
jsut210
Posts: 35
Joined: Aug 14th, 2013, 7:53 am
Location: Maryland

Re: Arduino programs

Post by jsut210 »

Hopefully I will be able to help a little. I am very good with the C language, just not good at explaining things. So here goes.

You should try to think of anything that ends with () or (variable) and a method or function. What this means is that in code when you call it, whatever follows it will be executed. Here's an example:

void test() {
//Do stuff
}

You can see that after I declare my function, I add a { to signify the beginning of the code block that calling test() will run. Then, I add a } to show where the block of code ends.

But how do you actually call a funtion? It's very easy. Say I wanted to call the test() function above. All I do is add test(); to my code. Here's another example.

void setup() {
test();
}

void test() {
digitalWrite(13, HIGH);
}

In this code, the test() function is called once when the program begins. The test() function simply sets pin 13 to high or +5v. Now if you're wondering what void loop and setup mean, I'll explain. Void setup is called once when the program begins. Void loop is called over and over as long as the Arduino is on. So in short form, you use { } to show the program where a block of code starts and ends.

The double brackets you see at the end of the button code serve two different purposes. One closes the block of code assigned to the if() statement, the other closes the block of code assigned to the loop() function.

Also, those if statements don't need the brackets because there is only one line of code that goes with each. For example:

if(something) {
//do something
//do something else
}

In this case, everything inside the brackets will be executed. However:

if(somehing)
//do somehting
//do something else

In this case only "do something" will be executed.

Hope I could help! Ask any more questions that you have, I am happy to assist.
CLYON
Posts: 35
Joined: Dec 2nd, 2012, 8:52 am

Re: Arduino programs

Post by CLYON »

I am starting to understand a little. I have two books, Getting Started With Arduino, and The Arduino Cookbook. I am able to look at a lot of programs and compare. I found this at the Arduino website:


Beginning programmers, and programmers coming to C from the BASIC language often find using braces confusing or daunting. After all, the same curly braces replace the RETURN statement in a subroutine (function), the ENDIF statement in a conditional and the NEXT statement in a FOR loop.


Because the use of the curly brace is so varied, it is good programming practice to type the closing brace immediately after typing the opening brace when inserting a construct which requires curly braces. Then insert some carriage returns between your braces and begin inserting statements. Your braces, and your attitude, will never become unbalanced.

I came from BASIC so I am going to look at it like a RETURN statement, that helps.

Thanks for everyones help.

Chuck
rossrov
Posts: 383
Joined: Feb 28th, 2013, 5:01 pm
Location: Australia

Re: Arduino programs

Post by rossrov »

Timely thread and all good points made above. Recently took the plunge and decided to ditch good old faithful BASCOM and get into Arduino. Reasons being that the Arduino ethernet looked pretty easy to get going, uploading is straight off the USB with no additional programmer needed, and learning Arduino first will be a help in learning "proper" C down the track should I decide to do that. I can certainly relate to trying to write Arduino (and JavaScript) as if it were BASIC. Once I get a grip on the "object oriented" concept things will get easier for me.

The missing curly braces mentioned in the above posts reminded me of something. I was trying to put some JavaScript into one of the webserver examples and it would not compile because of what would otherwise be legitimate and normally required quotes. I removed the quotes, it compiled, and when I looked at the source code in the browser the quotes had been magically added by the compiler!

The serial monitor in Arduino is pretty neat. No need to wire up a MAX232 chip, a separate cable and port and be tied to the old PC. Can now use newer PC that doesn't have an RS232 serial port
FJM
Posts: 23
Joined: Nov 6th, 2013, 11:02 am

Re: Arduino programs

Post by FJM »

Hi there,

I've got a good understanding of C/C++, so I'll give an explanation a shot...

Essentially, the braces (or curly brackets, {}) define a block of code, as you mentioned earlier. They define a group of statements that belong together. If you only have one statement, the semi-colon marks the end of the block. Otherwise, you need to group the statements with braces.

When you define a function such as setup() or loop(), they define the block of code that belongs to that function:

Code: Select all

void loop()
  {
      // do something here
  }
You'll notice that I use different formatting. I like to align my braces under each other as I find it makes matching easier, and adds "white space" to the program to spread it out and make it easier to read. It's just a matter of preference.

It works the same for an 'if' statement.

Code: Select all

  // This will work as there is only one line
  if (buttonState == HIGH)
     digitalWrite(LedPin, HIGH);

  // This will work as we have braces around the block of code
  if (buttonState == HIGH)
    {
    digitalWrite(LedPin, HIGH);
    LedIsOn = true;
    }

  // This will not do what we want; LedIsOn will always be true as the ; after the 'if' ends the block.
  // Don't let the indentation fool you - C/C++ ignores the spaces and line feeds.
  if (buttonState == HIGH)
    digitalWrite(LedPin, HIGH);
    LedIsOn = true;
The same rules apply for else statements, for() loops, structures, classes (for C==), etc.

From the Arduino examples, the Blink example only has the braces around the blocks that defines each function. There is no need for more.

The Button example has braces to define the block that belongs with the 'if' and the block that belongs with the 'else'. In reality, the braces are not necessary as each block has only one statement. However, it is good form to always include the braces - if you later add another line to the 'if' block but forget to add the braces, your code will not work as expected or may not compile:

Code: Select all

// Button code without braces is still valid...
  if (buttonState == HIGH)
    digitalWrite(LedPin, HIGH);
  else
    digitalWrite(LedPin, LOW);

  // ...but this will not compile. The 'if' looks like a single line 'if' with no 'else' while the 'else' does not have an 'if'
  if (buttonState == HIGH)
    digitalWrite(LedPin, HIGH);
    LedIsOn = true;
  else
    digitalWrite(LedPin, LOW);
LedIsOn = false;[/code]

There are other effects that using braces can have but this is the critical part.

Hope this helps and I haven't actually muddled your understanding. I have been know to do that. :)

Fred
Post Reply