Guide to Arduino Programming

Download Report

Transcript Guide to Arduino Programming

Intro to Arduino
Programming and Lab Basics
Mike Robinson
Part 1 - Words to the wise
Keep your code organized!
• A quick demonstration
A quick word about reading and writing code
Keep your circuits organized!
• A quick demonstration
330 Ohm
From Arduino
330 Ohm
From Arduino
330 Ohm
From Arduino
Keep separate pieces of code once you get
each part of your lab or project working
Balancing
Arduino Robot
Read
Accelerometer/
Gyroscope
Measure motor
rotation
Estimate angle
of robot
Set motor
voltage
Part 2 – Arduino Basics
First things first
Make sure you can upload the code blink from examples/basic.
If it worked, there should be a light slowly blinking on your Arduino
Void setup() and void loop()
Your code must have
only one function
called setup and one
called loop but They
can be empty
If you only want a piece
of code to run once,
where should you put
it?
If statements can be use to make basic
decisions in your code
Syntax
if(conditional statement)
{
Your code goes here
}
Example
if(testVal == number)
if(testVal < number)
if(testVal > number1 && testVal < number2 )
Else if and else
Syntax
if(conditional statement)
{
}
else if(conditional statement)
{
}
else
{
}
Later on you may want to be able to turn a section
of code on and off for debugging. Here is a trick to
make that easier
Syntax
if(1)
{
Code you want to turn on and off
}
To deactivate this code, change the one to a zero
if(0)
{
Now any code in between the brackets won’t do anything
}
The serial monitor is going to be your best
friend when it comes to debugging your code
The serial monitor lets you print information from your code
If your code is doing something strange, try printing out different
values and see if they make sense
Click
here
You will (hopefully not) make this mistake!
The serial lines are connected to digital pins 0 and 1 on the Arduino.
You usually don’t want to connect anything else to these lines.
Anatomy of a for loop in Arduino
Example:
for(int i = 0; i<10 ; i++)
{
The variable i here is our counter
You need to declare the variable with “int”
This section says to run
the loop while the
counter i is less than 10
This section says to add 1 to
our counter each time
through the for loop
Serial.println(i);
}
This code will now run 10 times
Serial monitor output
Functions
Also sometimes called subroutines
Functions are a great way to organize your code. They allow you to
break your code in small pieces and they help you troubleshoot.
The two main types of functions we will use are void and int, but you
can also use other data types if needed.
Functions
example:
int add(int a, int b)
{
int c = a + b;
return c;
}
To call “add” in your code:
sum = add(num1,num2);
Functions help make your code much more
readable
Part 3 – Project Advice
Consider using the “V Model” of mechatronic
design when working on your project
System
Design
System
Test
Subsystem
Design
Subsystem
Test
Single Part
Design
Single Part
Test
Code
Vasić, Vasilije S., and Mihailo P. Lazarević. "Standard industrial
guideline for mechatronic product design." FME Transactions
36.3 (2008): 103-108.