Page 3 of 3

for loops

Arduino – For Loop

Arduino’s language is packed full of goodies that make things quite a bit easier to accomplish. Without loops, we would rewrite code over and over again, and it would be the ugliest code you’ve ever seen.

It takes a while before some people even start using loops, you’re in the right place if that’s you. Using loops will change the way you structure your sketches completely. You’ll find this is a huge time saver. Some times a little effort goes a long way!

for loops

What is it?

A for loop is a loop that runs a set amount of times. It loops until a predetermined condition is met, you set this condition at the beginning of the loop. Once the condition is met, it breaks out of the loop, and continues on with the code.

Most of the time, it’s good practice to keep the loops in a function or an if statement. Doing this avoids slowing down your sketch, by running through the loop every single time your main loop starts over.

Uses

One of my favorite uses, is using for loops to cycle through each value in an array. One example of a for loop that I used recently, was in a weather station. I designed a weather station that had 16 underground sensors. Instead of running a function 16 times, I ran a for loop that counted up to 16, and ran the function to read from each one individually. This took much less code than it would have otherwise.

Usage

This is the syntax from Arduino.cc:

for (initialization; condition; increment) {
 //statement(s);
}

Let’s pick that apart.

You of course state that you’re using the for loop by starting with “for”. The arguments to the for loop can be tricky at first, but they are fairly easy to understand. You start with initialization. I prefer to start with “int i”. You initialize an int here generally, because you’re going to use that int to count with. This is the variable that you’ll use to determine when the loop is done.

The condition that you’re setting here is what needs to be true for this loop to be done. You could put i<=10 and it will run until it is equal to 10.

The last portion here is increment. Normally I’d simply put i++ here. This is where you’ll add to the int that was defined.

An important note: If you use this to count things in an array, arrays start with an index of 0. So if you programmatically have your sketch determining the length of an array, you need to make sure you’re calling array[i-1]. This would start your index at 0, and make sure you don’t go over the amount of values that’s in the array.

Example

This example will count to 10, waiting 1 second in between each number. You can speed this up by changing the delay at the end of the loop:

void setup() {
Serial.begin(9600); //Initialize serial output
}

void loop() {
Serial.println("For loop starting");

for (int i=0; i <= 10; i++) { //initialize the for loop, it will count i up to 10, then be done
 Serial.print("Counting:");//Write the text counting
 Serial.println(i);//Print out the value of i and start a new line after
 delay(1000);//Wait 1 second
}

Serial.println("For loop complete");

}

Let’s see what the output gives us on the serial monitor.

There you have it! Now we can count to 10, without having to use our brain!

 

passive income idea

Make Money With Reseller Hosting

A survey by NetCraft, in 2012, showed that there were 51 million websites launched during that year. That’s 140,000 websites per day! You can learn how to take advantage of this massive growth, and earn passive income by setting up a simple reseller hosting website. I personally use Hosting24, and I’ve been nothing but satisfied ever since.

I personally host 4 domains through them, a VPS, and a reseller account. My reseller account has worked flawlessly for my personal and customer accounts. I’ve had nothing but luck with Hosting24, and I couldn’t be happier with the service I’ve received.

Why promote them and not myself?

Normally you would think I’d be more excited to share my own hosting with you, but I want to share and relate with you about how exciting it is to make money from things like this. Once you have your reseller account set up with Hosting24, it’s basically done! You can host unlimited websites with the available packages, and they offer unlimited bandwidth and disk space.

I’m new to web business, what about support?

I switched to Hosting24 about 4 years ago now, and I’m blown away by their 24/7 customer service. You receive replies within minutes! I’ve learned an amazing amount from their knowledgeable staff, and I’ve been 100% satisfied with every support request I’ve been a part of.

Another key feature is how slick the WHM and cPanel setup is. Your customers each get their own cPanel, and it is set up automatically when you make a sale. You can get a marketing system set up with workflows that automate the entire process for you. This means that 100% passive income.

What is passive income, and why is this so cool?

Most passive income strategies include things like starting a blog, constantly repining affiliate links, completing tasks for people online, writing books and ebooks, dropshipping, etc. What is passive about those? Nothing! A lot of times, when you hear the words “passive income”, you find that there is nothing passive about them. That drives me crazy, don’t say passive, if it’s not passive!

A reseller hosting plan with Hosting24 can be completely passive. You literally set up your site, pick prices for your plans, put them on the market, and make $$$. I cant believe how simple of a business this is to get started with.

passive income idea

My dream

My dream as a young adult was always web hosting, db hosting, etc. I’ve always had something in the back of my head telling me, this is what I need to do. I always thought about renting a warehouse, getting fiberoptic internet, and buying a whole bunch of servers to host people’s websites. Do you know how much work that is? I do, I’ve done plenty of research.

Hosting24 handles all of these things for you. You don’t have to set up complicated web servers and services, no paying rent on a building, no huge internet bills, no server costs, and no maintenance! They do everything for you, how easy is that?

Sign me up then!

You can sign up today to make endless amounts of 100% passive income for as low as $25.99/month. Compare that to the internet alone that you’d have to pay for to do this yourself. It would be a much higher cost than that.

The deployment is simple when you sign up right now, the price is only $25.99, that’s $10 off of the original price of $35.99. I’ve been working with Hosting24 to get a deal like this for a long time now, don’t miss out!

Arduino Statements

Arduino – Switch Statements

The switch statement, in Arduino programming, is very similar to the if statement that I wrote about previously. The main similarity is that you’re testing a statement, if it equals true, certain code is ran.

The difference is versatility and ease of use. When looking at a switch statement, it appears to be much more complicated than an if statement. It’s not! Actually it is much more simple to use, and produces a lot less code.

You will thank yourself later for making the switch, the switch statement switch.

What is it?

Without showing you any code right away, I’ll explain what it is in human language.

A switch statement takes a variable as an argument, and evaluates it. The actual switch part comes after. Now that it knows what it’s dealing with, it looks at the possible matches that you’ve preselected. When it finds a match, it runs the code until it comes across the word “break”. The break makes it skip the whole rest of the switch statement.

If there is no match, it runs the default code. The default code can be blank, and doesn’t have to contain anything juicy. A lot of the time, my default code just outputs to serial saying there was no match.

Uses

An example, where you would use a switch statement in the real world, could be in actual smart home light switches. Maybe you have a function that toggles them off and on when your cell phone app communicates with the arduino.

You could have the communication set a variable based on what button was pushed. Then the variable gets passed in to the switch statement. The switch statement sees the variables value of “living room” and associates it with what it knows, which may be switch number 3. Then inside your statement it can set variables and call functions based on switch 3.

And then you have light on, light off. This statement can reduce a lot of code, where you may otherwise have to write a function or if statement multiple times.

Or you may decide to put the switch statement inside the function, which would produce the same result as the function being inside the switch.

Usage

This begins with a variable that you’re going to test. This variable can be anything, an int, a string, char, etc. You start the switch statement with “switch (var)”. The “var” is where you’d place the name of your variable.

Much like other things you come across while programming for Arduino, you place the code to be run in curly braces. { and }

The difference here, from the other statements, is that the conditions that you are testing reside inside the curly braces. Normally your arguments would go inside the parenthesis, not today!

The way you compare what your variables value is, looks like this: “case condition1:”. You’d replace condition1 with the value you’re testing against your variable. For example, if you’re trying to see if a string says “taco”, you’d replace condition1 with the word taco. It would say “case taco:”.

Ending each code block after you define your case is very important. You end it with break;. If you do not break out of your code, either the compile will fail, or you’ll end up with unexpected results. This is the only time you do want to break your code, instead of fixing it.

 

The last part of this is the default portion. You define the default code to be run by calling “default:”. Any code between the “default:” and the “break;” is run if none of the other things you defined turned out to be true. Sometimes there are errors that cause the variable not to be set right, sometimes you planned on using default for multiple cases. Whatever the case is that you’re using default, you will know. I’ll say again, that you do not have to use the default section. It does need to be there, but it can be simply “default: break;” if you want your switch ignored when nothing is true.

Examples

This example will be assuming the ability to read based on the age of the person. We’ll start by defining the age variable:

int age = 0;

Then we’ll initialize Serial 0, so we can output to the pc:

void setup() {
 
Serial.begin(9600);//Initialize pc communication

}

In the loop we tell switch what variable we’re testing, and what each condition will do. In this case we’re just outputting different words to the serial monitor. After the switch statement is over we do age++, this simply increments the int by 1. You can increment any int this way, and if you have it being done in loop, it’ll just keep on adding. After we have a 5 second delay, before the loop starts over again.

void loop() {

switch(age) {
 case 1:
 Serial.println("You're only 1, how can you read this!");
 break;
 case 2:
 Serial.println("You're only 2, you can talk, but surely still can't read.");
 break;
 case 3:
 Serial.println("You are 3, I'd be pretty impressed if you could read.");
 break;
 case 4:
 Serial.println("You're 4, you may be able to read, but I still don't believe you.");
 break;
 case 5:
 Serial.println("You're 5, I guess it's time to start reading.");
 break;
 default:
 Serial.println("You are either older than 5, or younger than 1. Do whatever you want, I guess...");
 break;
}
age++;
delay(5000);//wait 5 seconds
}

I’ll paste the entire code now, if you’re copying and pasting it, you can use this one:

int age = 0;

void setup() {
 
Serial.begin(9600);//Initialize pc communication

}

void loop() {

switch(age) {
 case 1:
 Serial.println("You're only 1, how can you read this!");
 break;
 case 2:
 Serial.println("You're only 2, you can talk, but surely still can't read.");
 break;
 case 3:
 Serial.println("You are 3, I'd be pretty impressed if you could read.");
 break;
 case 4:
 Serial.println("You're 4, you may be able to read, but I still don't believe you.");
 break;
 case 5:
 Serial.println("You're 5, I guess it's time to start reading.");
 break;
 default:
 Serial.println("You are either older than 5, or younger than 1. Do whatever you want, I guess...");
 break;
}
age++;
delay(5000);//wait 5 seconds
}

Let’s see what happens when we run the code!

switch output

Exactly what we were hoping for! It counts from zero to a bazillion, and correctly chooses what switch condition to use.

Let me know if you have any questions below! More than happy to answer them.

Arduino Statements

Arduino – If Statements

Welcome to If Statements! This is where you will learn to argue with your Arduino about whether or not a statement is true. If statements are very commonly used, get to know them well. Disclaimer: You will not win an argument with your Arduino, it always knows if a statement is true or not. This is similar to marriage, you may find yourself upset if you choose to argue with your Arduino.

What you need:

What is it?

An If Statement is a block of code that gets run depending on an statement you supply to it. If the statement is true, the block of code is run. If the statement is not true, it skips the block of code.

If you supply an else if statement, it will test that statement to verify if it’s true.

If that else if statement is false as well, and you supply an else statement, it will skip to the else statement and run that code block.

You don’t have to supply an else if statement, if your first if statement is false, it will go directly to else. You also don’t have to supply an else statement, if the first if statement is false, it would just not run the code at all without an else statement.

Uses

A good example of an if statement would be in a weather monitoring application. You could have a device monitoring the temperature, and if the temperature is below 32 degrees Fahrenheit, it will tell you ice is possible on the roads. An else statement for this could be an output stating that the roads should not have ice on them.

Another example would be when using an RFID tag. You could have a list of acceptable RFID tags, and if the one that is scanned belongs to the list, the door opens. An else statement for this would be a red blinking LED, that indicates a RFID tag that is not allowed access.

Usage

Lets look at the anatomy of an if statement.

The syntax from Arduino.cc looks like this:

if (condition)
{
  //statement(s)
}

Statement to be tested

Declaring the statement starts with “if ()”, you must have parenthesis here. In between the parenthesis, you put the condition that you’re testing. An example would be “if (x > 10)”. If x is 11, it would run the code. If x is 1, it would not run the code.

Code to be run

The second part of this is the curly braces { and }. This is where you put the code to be run. Anything outside these braces will be run, regardless of whether the condition is true. Inside the braces is code to be run if the statement is true.

If the If gets run, the others are ignored

The main thing to remember here, is that if any if statement equals true, the code gets run and any other part of the if statement is ignored. This means that it skips any else if, or else statements.

Examples

Simple predefined variable:

This example uses a predefined variable, which is an integer named age. This variable is declared right at the top of the following code. Here we’re trying to figure out if the person is old enough to drink. In this case the person is 14, so it should determine that the persons age is under 21, and run the else statement.

int age = 14;

void setup() {
 
Serial.begin(9600);//Initialize pc communication

}

void loop() {

if (age < 21) {
 Serial.println("You are not old enough to drink!");
}
else {
 Serial.println("You are 21 or older, you may have a drink.");
}
delay(5000);//wait 5 seconds
}

Let’s run the code, and see what we come up with. If you’re unsure how to upload code, check out my hello world post. Here is the output you should expect from the above code:

Success! It determined that we are unable to drink.

Let’s change the age variable to 24 now, and see what we get.

int age = 24;

The Arduino wins again! If statements are very useful when writing Arduino sketches. You’ll find yourself using them quite often. Let me know in the comments if you have any questions, and please subscribe for additional Arduino tips.

If you’re looking for something a little more versatile, you may try Arduino Switch Statements.

microcontroller vs microprocessor

Microcontroller vs Microprocessor

microcontroller vs microprocessor

What do you need, a microcontroller or a microprocessor?

The main question revolving around the microcontroller vs microprocessor is – what are you doing?

Let’s start by looking at the main differences between the two. You’ll find that they are used for very different things, and in general you’ll probably find that your project isn’t going to need the horsepower that a microprocessor provides.

What is a microcontroller?

The most popular microcontroller seems to be the Arduino Uno. There’s no questioning why it’s so popular, it’s a very powerful, cheap, easy to use, expandable unit. If you’re looking for more output you may try the Arduino Mega 2560, this is another very powerful unit that provides much more capability for outputting to many devices. You can learn a little more about Arduino products in the Intro to Arduino post.

arduino uno rev3

I use the Arduino Mega 2560 for a weather station that I developed for work, the weather station reads 16 underground sensors, an anemometer, a pyranometer,and a bme280, additionally it sends all of this data via the esp8266 wifi module to a database. I’ll write a post on this soon.

ARDUINO MEGA 2560

So what is a microcontroller? It is considered a SoC(system on chip), it contains the processing unit, ram, rom, and any other peripherals that the manufacturer chose to add to it. A microcontroller requires no extra circuitry for the devices that control the processing portion of it, because it’s all built in.

This means that if you develop something on your Arduino Uno, you can pull the chip, and build a much smaller project based off of what you developed. This is one feature the Arduino Mega 2560 does not have, but if you’re planning on using that many outputs it’s not going to be a small project anyways.

What is a microprocessor?

A microprocessor is an individual processor, similar to the CPU you have in your desktop computer. Microprocessors are considerably smaller than the average processor you’d see in a computer. A commonly known architecture that uses a microprocessor is the RaspberryPi. These are configurable beyond belief, but that is not thanks to the microprocessor in the system, it’s thanks to the circuitry around it. It would not, however,  be able to handle the loads that it does without the microprocessor.

raspberry pi 3

A microprocessor can handle running full operating systems, with multiple threads, multiple cores, and can take quite a beating. That’s where the microprocessor stands out compared to the microcontroller. The full operating system is what makes your RaspberryPi or mBed powered system, like the FRDM-K64F different.

There are all different kinds of microprocessors as well, and different aims for different boards. The FRDM-K64F focuses on an operating system that does not provide a user interface, but provides extreme capability with addons. The RaspberryPi, on the other hand, provides a user interface but is more limited to the addons that are developed specifically for it.

Which one should I use?

That depends on what you’re trying to do. Are you trying to build a smart mirror? Use a microprocessor. Are you just diving into the world of electronics? Use a microcontroller. You should evaluate the project that you’re considering and base the needs off of the needs of your project. Don’t just by a microprocessor because it sounds cooler, they are much more complicated to set up. Most of the time you can get by with a simple microcontroller.

 

Key features for Arduino IDE

How to use the arduino ide interface

In this post, I’m providing you with an introduction to the arduino ide interface. I was writing sketches for months, before I realized I was missing out on some of the key features of this awesome tool.

I will outline some of the most important features you’ll use when writing sketches.

 

All of these features are critical to making the most of arduino development, and are often overlooked by beginner developers.

Board selection

Here you select the board you’re using, this is important for when you’re compiling your sketch. If you pick the wrong board, your sketch may not run properly. The sketch is built by the compiler, specifically for the board you select, and some boards do not contain the same features or configuration as others.

Com port selection

This is where you select the port assigned to your arduino, by the computer. Without setting this, the ide doesn’t know where to upload your compiled sketch. If you own multiple boards, they will each be assigned to a different com port.

Tabs

It was a long time before I found out that tabs were a thing in the arduino ide. The significance of tabs is that it not only allows editing multiple files at once, but these files will build together as one sketch when compiled.

This feature allows you to separate the pre-set up, setup, loop, and any other functions or variable definitions in other tabs. It’s extremely useful for readability of long sketches. You can separate things, so when you’re looking for a certain function, it’s easy to find where the function starts and ends.

Libraries

Libraries are an amazingly helpful tool. Third party libraries provide additional functionality, that is not otherwise included in the arduinos built in library set. A good example of this is the esp8266. This is a wifi device, that would take weeks to set up without an available library to help drive it.

The arduino ide makes it very easy to manage libraries with searching and quick links to the source of these libraries.

Examples

Example code is provided with most libraries, and can be accessed very easily within the arduino ide. You don’t have to navigate to wherever it is that your libraries are stored, and find the example code. They are built right in to the interface, for easy access. This is another one I didn’t notice until months after I started writing arduino code.

Auto format

Auto format is another must for code readability. This feature automatically makes sure your curly braces are lined up, to help find beginning and ends of enclosed code. It also automatically indents code within the curly braces further than the curly braces are already invented. This extra indent ensures that the curly braces don’t get lost within the other code.

Verify code vs upload

A common question you may ask yourself when writing a sketch is, am I doing this right? Or, my code makes sense to me, will it make sense to the device? Or you may be writing a sketch, and your arduino isn’t near by to upload to.

That’s where the verify option comes in handy. It will compile your code and error check it for you, without the need for an arduino device to be plugged in. Then you can debug your code before trying to actually run it.

Output console

It may not be long after you start coding, that you find yourself constantly staring at the output console. This area will display tons of important information, about what ever action you’re currently performing.

If you’re opening serial monitor, it may let you know it couldn’t bind to the port. Then you might have to ask yourself, is my arduino plugged in?

If you’re opening serial monitor, it may let you know it couldn’t bind to the port. Then you might have to ask yourself, is my arduino plugged in?

Imagine coding without this. Would you sit and read through a thousand lines of code, to realize you forgot a semicolon on line 753? I sure hope not. I would give up pretty quickly.

Serial monitor

This is where you can have your arduino talk back to you. This is infinitely useful when debugging, or verifying that your functions are returning the right results. They made it very easy to output anything you’d like right to your computer.

You could, of course, use a different serial monitor program. It’s much easier to use the arduino ide as an all in one tool.

Baud rate

Your baud rate is what calibrates communication speed between the device and your computer. Without these 2 devices matching, you would get nothing at all, or a jumbled mess of nothing that makes sense.

Baud rate is a very important, and easily forgotten setting that is extremely critical. A lot of peripheral devices for your arduino will also require a specific baud rate. Each device you connect can run at a different rate, so you’re not limited to just one.

Serial send

Serial send is an amazing feature. This allows you to send data to your device, typed right in to the serial monitor. You can code your arduino device to interpret this data, and relay the message to other devices. This allows you to test functionality of devices, and determine how you’re going to communicate with them.

Questions?

Feel free to ask questions, or provide tips for others in the comments. I’m very open to questions and suggestions, that’s what I’m here for.

Maybe now you’re ready for the Serial Hello World Tutorial. This is a good place to start coding.

Hello World Tutorial

Arduino Serial Hello World

Hey guys, this time I’m going to introduce a hello world example outputting to the serial monitor on your Arduino IDE. You’ll learn how to write a very simple code that sets your first variable, and output that variable on to your computer screen.

What you need:

Pre-Setup:

This is where we create our variable. Our variable is a character array, more commonly known as a string. Arduino sends data via serial byte by byte, meaning 8 bits at a time. Later, you’ll find that you need to define the length of what you’re sending, when communicating with other devices some times.

The following code is our simple initialization of our character array. The key here is starting with “char”, this tells the IDE what type of variable we’re trying to set up. Next we name the variable “myString”, this is the second part of setting your variable. Then we set the char type to array by adding “[]” to the end of the name. Finally we set the value of our variable to “Hello World!”, and close the line out with a semi-colon. You’ll notice that every line has to be closed out with a semi-colon, this tells the compiler that you’re done with the current statement.

Setup:

Next we’re going to start the setup function. Setup is only run once, right when the program is started on your device. This is a good place to start initializing things that need to be started prior to reading or writing. Here we will initialize the serial port, the first serial port is always linked to USB. It can be used for other things than USB, but I prefer to leave it that way.

You’ll see our complete code below, this is a very simple one-liner setup. We start by calling “void setup()”, this tells the compiler that we’re defining the setup function. You can define custom functions as well, but we’ll get to that later. To create a box for your setup to reside in, you need to enclose your code in curly braces, “{” and “}”. These curly braces tell the compiler what code belongs to the function you’ve created, which in this case is setup.

Now that Our setup function is defined we can call “Serial.begin”, capital letters do matter here – Serial needs to be capitalized. If we take this call apart, “Serial” is the object that we’re telling the compiler that we’re working with, and “begin” is the function we’re calling. Some functions have arguments that need to be passed to them, that is done inside the parentheses – “(9600)”. What we’re doing here is setting the baud rate to 9600. The baud rate is important, because it synchronizes the speed of data flow between your computer and the Arduino device.

Loop:

Here’s the juicy part, finally the loop! The loop is where your main code is run, this code is run over and over and over until the end of time, or until the device is unplugged/reset. This is a very simple loop that prints the text we defined earlier in a variable, waits 1 second, then repeats.

We have 2 functions being called here. The first is “Serial.println(myString);”, this tells the Serial object to print a new line using the variable named “myString”. There is another function to Serial that is simply “Serial.print”, which would print everything on the same line. That is useful if you have a lot of code determining what to print, and you don’t want to do it on a new line yet. The second function is delay, note that it’s lower case. Most built in Arduino functions are lower case, and the objects are upper case. The argument we’re passing to the “delay(1000)” function is amount of time in milliseconds. So 1000 = 1 second.

The final product:

//Hello world example code
 //This will be an introduction to Arduino, via serial communication
 char myString[] = "Hello World!";

void setup()                    // This only runs once, right when you start the sketch
 {
 Serial.begin(9600);           // Instantiate serial with a buad rate of 9600 bps

}

void loop()                       // Runs forever
 {
 Serial.println(myString);  // Prints Hello World! via the string that we created pre-setup
 delay(1000); // Pauses for 1 second
 }

 

Now let’s run the code! Go on to the next post to put your code to work!

Arduino USB Serial

Uploading Code to Arduino and Using Serial Monitor

This post is assuming you’ve read my previous post, Arduino Serial Hello World. There you learned how to write the “Hello World” code, in preparation for output to the Serial Monitor. By now you should also understand the Basics of Arduino. These 2 are the prerequisites of this project. You could, of course, just copy my code and upload it.

Let’s get started!

I’ll be using the Arduino Uno, this is the most commonly known device that is used for Arduino programming. If you’ve read the Arduino Serial Hello World post, you’ll already have the code in your IDE.  If not, you can copy the following code and paste it in the Arduino IDE.

//Hello world example code
//This will be an introduction to Arduino, via serial communication
char myString[] = "Hello World!";

void setup() // This only runs once, right when you start the sketch
{
 Serial.begin(9600); // Instantiate serial with a buad rate of 9600 bps
 
}

void loop() // Runs forever
{
 Serial.println(myString); // Prints Hello World! via the string that we created pre-setup
 delay(1000); // Pauses for 1 second
}

Let’s get connected

If you haven’t already, you may want to configure your IDE for connection with the board. Following the configuration process allows the IDE to properly communicate with the board. It’s a very simple process, so it shouldn’t take too long.

After you do your one time configuration process, you’re ready to start uploading! Let’s plug USB in to the Arduino, and the other side into your computer.

You may notice I’m not using the default USB that came with the Arduino Uno. I choose to use a longer USB, because it helps keep my work area clutter free. Once you start more complex projects, you may run out of room in your work area.

Let’s upload

Now that you’re connected, you may see that the LED’s came on when plugging in your device. It’s alive! If you have it configured properly, you’re ready to upload. Click the arrow that points to the right, at the top left of your screen.

Now that you’re uploading, you can watch the progress in the black area at the bottom of your screen. There’s also a small progress bar that shows how it’s going.

This process should be very fast, on such a small sketch. When you start diving in to libraries, and using more complex code, it’ll take a little longer.

Where’s my output?

Now you’ll open the serial monitor. This is where the Arduino can send messages back to your computer. This is a very good area to try to use for debugging things. Click the icon that looks like a magnifying glass, at the top right of your screen.

If all is going well,  you’ll see the Arduino communicating with the Serial Monitor. It should say “Hello World!”. You’d think it would have more to say than that, but it really only talks about the things we tell it to. If you’re not talkative, neither is your Arduino, unfortunately. Don’t expect your Arduino to be the conversation starter, you’re the one in control here.

Thanks!

I hope you found this easy to follow, let me know in the comments if you have any questions. I’m here to help you, and I’d be more than happy to do so. Feel free to Subscribe below, you’ll find that I’m going to slowly work you in to more complex development. I feel you’d benefit from it a lot, if you follow along.

arduino uno rev3

Intro to Arduino

The intent of this intro to arduino, is to explain the basics of Arduino. This is a great place to start, if you’re just starting out with Arduino programming.

What is an Arduino?

Arduino is actually the language running on your device. What people know as an Arduino is a microcontroller, which runs whatever code you’ve uploaded to the device. Arduino was created to simplify electronics prototyping, but it can be used in more permanent situations. The following is the most popular board, the Arduino UNO. You can get one here.

What is an IDE?

IDE stands for Integrated Development Environment. There are a lot of programming languages that provide an IDE for you to work in, compile your program for execution, and deploy the finished product to your Arduino. Download the IDE here.

What is compiling?

When your program is compiled, it is being built. What this does, is converts your more “people readable” code to a language that your Arduino can understand.

How does Arduino work?

There are 4 parts of Arduino code that you need to understand. They are very simple, and easy to follow. Arduino is a dumbed-down version of the C programming language. C has been around for a long time, and will be for years to come. The following are the 4 parts to Arduino code, the images are going to be from the code in my “Hello World:Serial” tutorial:

Pre-Setup

  • You define any libraries you’d like to include in your project, libraries will be explained in another post. You don’t need to know about these yet
  • You define any global variables you’d like to set before running the main part of your code.

Setup

  • This is a run-once part of your code that prepares things, some sensors need to be fired up, or initialized, before you can begin reading from them.
  • Some times you need to do things like connect to wifi, that may only get done once.

Loop

  • This is your main code, this loop will run forever until the world ends, or until you disconnect power from the device.

Post-Loop

  • I’m just calling this post loop, it’s actually an area where you can define functions for your code.
  • You’ll learn that functions are very handy when your code gets long. Some of my Loop statements are only 4 lines long, because they are looping through functions.
  • Anything you put here, is to simplify repetitive tasks, and/or clean up your code.

Is there code that isn’t run?

Yes, you can comment out lines of code. Commenting allows you to “hide” code from the compiler, if you’d like to test your code without a statement or have a reference inside your code for later. Commenting is also a great way to add a “bookmark” to your code, so you can identify sections of code easier, and remember what they are for. Adding a comment is as simple as putting // at the beginning of the line you’re commenting out.

You can keep reading, to find out how to communicate with your Arduino via a Hello World example.

Thanks!

I hope you enjoyed my introduction to Arduino. Let me know if you have any questions, I’d be happy to answer them! Please subscribe below to get the latest posts by Anarchy Development.