# How to Code Arduinos Part 2

596

37

## Introduction: How to Code Arduinos Part 2

Have you ever wondered How to code Arduinos, well your in the right place.This is a second part to my How to code Arduinos Instructable to show you all some more Arduino Commands. so sit back, relax, and enjoy learning. Please read the How to code Arduinos Instructable as this expands on what that teaches.

For this Instructable you will need:

Computer

Arduino IDE

A Glazed Doughnut (No it's not essential, but why not)

### Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

## Step 1: Inputs

Not really a lot more for Inputs, but there was something that I found interesting.

AnalogReference()

Remember in the last Instructable when we used AnalogRead() to detect voltage, well this extends that function. So normally the Arduino reads a a voltage between 0 and 5 volts. This changes it so that the voltage can be one of three things(This is for every Arduino but the mega), EXTERNAL(matchs voltage of AREF pin), INTERNAL(normally equal to 1.1 volts but is different on the Arduino Mega) or DEFAULT(which sets it back to normal).

`analogReference(EXTERNAL);`

This goes in the void setup. This sets it so that the Max voltage is set be the voltage on the AREF pin, so to put it simply, a voltage between 0 and AREF voltage. Make sure to not attach anything to the AREF pin until it has been registered by the Arduino. This is done by uploading the code before placing any voltage on the AREF pin. Don't go above 5 volts on the AREF pin or you could damage the Arduino.

## Step 2: IF (IF = Only One){be Sad}

Well luckily IF is not the only controller, so I'm not sad. We'll cover some of those IF like commands now.

WHILE

A WHILE loop is used when you want Code to run over and over again till the WHILE is false.

```while(sens < 10){
}```

The above text will run as long as SENS is less then 10. You may be wondering what the difference is between WHILE and IF. Look at the image at the top and you will see text highlighted in yellow. This is the code that the Arduino runs. You can see that all the code in void loop runs when you use FOR, but when you use WHILE, which is a loop, it only runs the code in the WHILE loop until it is no longer true.

DO WHILE

When we add a DO to the WHILE we have a while loop that will always run once, then if the WHILE is true it will run again.

```do{
}while(Test condition)```

FOR

The FOR loop is used when you want to run a statement a certain number of times.

```for(int x = 0; x < 10; x++){
}```

The Above text will run 10 times. First it makes a integer named X, then checks if x is less then 10, and if that's true it adds 1 to X and runs the code.

## Step 3: Serial, How to Read It

Well we already know how to send Serial information (Read the Part 1), let's learn how to send data to the Arduino this time. This can be done with the Arduino IDE Serial Monitor.

Serial.available

This commands are both used to make a Serial reading Arduino.

```if (Serial.available() > 0){
}```

This checks to see if there is any Serial data, which is in the form of a number, and if there is it catches it and keeps it as a integer. We can also use this to read a letter like this.

```if (Serial.available() > 0){
}```

Now we have one letter that we can use to control the Arduino. Like send it a T (True) to turn on a led or a F (False) to turn it off.

## Step 4: The End

Well that about wraps it up. As always thank you for reading and I hope you've enjoyed. If you have any questions, suggestions, or found some info that is not correct, please tell me in the comments below.

keep on making.