loading

I have been programming my Arduino to create projects for almost 2 years now. At first I would just use code written by other people to complete projects but after I started to understand how the code was set up I wanted to write my own simple code. But sometimes writing simple code is hard for people who are just starting to get used to the Arduino Uno. I often looked up different ways to write the code but the hardest part was understanding errors and trying to fix them. I could never find a person who could explain exactly what I needed to do. So I had decided to create this instructable to help others who are also having problems. I will try to explain setting up, vocabulary, how to use different statements, and fixing errors on simple code. I hope you like it.

Step 1: Basic Set-Up

When writing simple code for the Arduino there are two main parts in the coding window. As seen in the first picture there is a void setup, this is where the setup code will be written. After that there is a void loop, this is where code will be ran repeatedly. Here is some simple code for making an LED blink.

void setup()
{

pinMode(13, OUTPUT);

}

void loop()

{

digitalWrite(13, HIGH);

delay(1000);

digitalWrite(13, LOW);

delay(1000);

}

As you can see in the void setup the code is pinMode(13, OUTPUT). This is basically telling the Arduino that is will output power through pin 13. In the void loop it is telling the Arduino to send power, wait, stop power, wait, and repeat. Here is some simple vocab. to help.

HIGH = on (out put 5 volts)

LOW = off (out put 0 volts)

delay = basically telling it to wait before running the next part of the code

(1000) = When this is put by delay it is telling it how long to wait. Remember it counts in milliseconds so 1000 = 1 sec. 2000 = 2 sec. 3000 = 3 sec. ect.

WARNING: THERE MUST ALWAYS BE A SEMI-COLON AT THE END OF EACH LINE UNLESS IN A DIFFERENT CIRCUMSTANCE.

Step 2: Setting Constants

Constants are used to make it easier to follow the code. It is basically like calling certain hardware by a name so you can keep track of it. This helps when you program a lot of things at once like LED's, push buttons, sensors, ect. Here is another example.

const int button1Pin = 2;
const int button2Pin = 3;

const int led1 = 11;

const int led2 = 12;

const int led3 = 13;

This is saying I have 2 push buttons and 3 LED's.

void setup() {
pinMode (button1Pin, INPUT);

pinMode (button2Pin, INPUT);

pinMode (led1, OUTPUT);

pinMode (led2, OUTPUT);

pinMode (led3, OUTPUT);

}

void loop() {

int button1State, button2State;

button1State = digitalRead(button1Pin);

button2State = digitalRead(button2Pin);

if ((button1State == LOW)

&& !

(button2State == LOW))

{

digitalWrite(led1, HIGH);

}

else

{

digitalWrite(led1, LOW);

}

if ((button2State == LOW) && ! (button1State == LOW))

{

digitalWrite(led2, HIGH);

}

else

{

digitalWrite(led2, LOW);

}

if ((button1State == LOW) && (button2State == LOW))

{

digitalWrite(led3, HIGH);

digitalWrite(led2, LOW);

digitalWrite(led1, LOW);

}

else

{

digitalWrite(led3, LOW);

}

}

This may seem like a lot but for now just focus where I used things like led1 or button2Pin, I was specifically talking about that piece of hardware. I will explain the if and else statements later. For now here is some more vocab.

At the beginning of void loop there was another constant it was:

int button1State, button2State = this is another name but it is for the state of the button (pushed or not pushed)

button1State = digitalRead(button1Pin) = the state of button1Pin (the first button)

button2State = digitalRead(button2Pin) = the state of button2Pin (the second button)

WARNING: A LOT OF ERRORS COME FROM THE CODE BEING WRITTEN THE WRONG WAY, MAKE SURE EVERY THING IS RIGHT.

Step 3: If and Else Statements: Part 1

If and Else statements are probably one of my favorite functions in coding. I like them because they can test multiple scenarios and do the function of one that is correct. These are used in a lot of real life coding. Here is and example:

if (button1State == LOW)

{

digitalWrite(led2, HIGH)

}

else

{

digitalWrite(led2, LOW)

}

This code is saying, if the State of button one is being pushed then send power to led2, if State of button 1 is not being pushed than don't send power to led2. Statements like this are simple but there are more complex ones, here is a table of logic statements.

== Equivalence (button1State == LOW)

!= Difference (button1State != button2State)

&& And ((button1State) &&( button2State == LOW))

|| Or (button1State == LOW || button2State ==LOW)

! Not (!button1State == LOW)

Step 4: If and Else: Part 2

There are a lot of ways to use if and else statements. For instance you can use them to turn LED's on and off. Maybe 1 button turns on 1 LED, another button turns on a different LED, but if you press both it turns on a different LED. For instance:

if (button1State == LOW) // if you push button 1

&& ! // and we are not .....

(button2State == LOW) // pushing button 2

{

digitalWrite(led1, HIGH) // then turn on LED 1

}

else // if it's not true then......

{

digitalWrite(led1, LOW) // don't turn on power to LED 1

}

Here is some more code to use for the example above,

if (button2State == LOW) // if you push button 2

&& ! // and we are not.....

(button1State == LOW) // pushing button 1

{

digitalWrite(led2, HIGH) // then turn on LED 2

}

else // if it's not true then.....

{

digitalWrite(led2, LOW) // don't turn on LED 2

}

Step 5: Fixing Errors

This is the hardest part for me, fixing errors!! There were so many error problems I couldn't understand. But Now I am going to try to show you some error codes so you can try to fix them.

Buttons_and_LEDs:54: error: expected '}' at end of input
expected '}' at end of input

I have encountered this problem many times, first let me try to show you what it means. Buttons and LEDs is actually my title. But the 54: means the error is in line 54. The next part I think is a bug in the program. I was only fix it when I put another } at the end of the program.

expected identifier before '(' token

I found that it was an error in the statment below this was how I had it:

if (button1State == LOW) && (button2State == LOW)

Now I know when you compare two variables using the && statement you need more parenthesis. It would now be like:

if ((button1State == LOW) && (button2State == LOW))

Now I have the extra parenthesis the error was fixed.

Next:

Buttons_and_LEDs:40: error: 'else' without a previous 'if'
expected ')' before ';' token

Else without a prevouse if was so hard for me to figure out but you just need to take out 1 character. The code was:

if ((button2State == LOW);
&& !

(button1State == LOW))

{

digitalWrite(led2, HIGH);

}

else

{

digitalWrite(led2, LOW);

}

See the semi colon in the if line, that can't be there at all. This is the code after I fixed it.

if ((button2State == LOW)
&& !

(button1State == LOW))

{

digitalWrite(led2, HIGH);

}

else

{

digitalWrite(led2, LOW);

}

Now that it is gone the code works fine.

Step 6: Final Words

Now you can see that coding is just like writing with a very strict structure. But the more you practice the better you get. Instead of copying and pasting try writing your own code. Even modify other codes to do your biding. After I started to understand more and more it became easier to write more code on my own. But sometimes I still need help. Coding the Arduino Uno is one of my favorite hobbies and I hope to learn more about it as I become more experienced. I hope these instructions have helped. HAPPY CODING.

OK thanks<br>???
<p>ok i have a arduino uno and im trying to make a robot that can go were it wants to but it cant go past little station u set up id like to no what this is called and how to write a code for it thanks anntenna guy</p>
well done, I feel your pain in finding errors in the code <br>thank you

About This Instructable

2,016views

52favorites

License:

More by Rico Q:Pro Dog Towel Pi Color Changing Lamp Attiny 85 Programming Shield 
Add instructable to: