Arduino


Using Arduino Libraries in Atmel Studio

Pada artikel ini, akan ditunjukkan bagaimana cara menggunakan Arduino libraries untuk pemograman dengan Atmel Studio. Pemrograman yang dimaksud disini bukan pemrograman dengan ino file, melainkan dengan cpp.

Tahap awal yang harus dilakukan adalah install Atmel Studio dan Arduino IDE. Arduino IDE diperlukan untuk diambil librarynya saja. Pada artikel ini, yang digunakan adalah Arduino 1.6.4 dan Atmel Studio 6.2.

Buat project baru

Buka Atmel Studio, kemudian buat project baru.
File > New > Project.

Buat structure file

Buka Solution Explorer, kemudian lakukan langkah-langkah berikut:

Hapus Arduino.cpp

Buat folder dengan struktur sebagai berikut:

src
- arduino
- - avr
- - -  cores
- - - - arduino
- - - libraries
- - - variants
- - - - standard
- ProjectFiles

Buka folder instalasi Arduino 1.6, kemudian copy semua file yang ada di folder berikut:

“arduino-1.6.0\hardware\arduino\avr\cores\arduino” ke “src\arduino\avr\cores\arduino”
“arduino-1.6.0\hardware\arduino\avr\libraries” ke “src\arduino\avr\libraries” (opsional).
“arduino-1.6.0\hardware\arduino\avr\variants\standard” ke “src\arduino\avr\variants\standard”.

Tambahkan file-file yang sudah dicopy, ke dalam project, sehingga menjadi seperti pada gambar berikut ini:

Buat project file

Buat file “ProjectMain.cpp” dan “ProjectMain.h” di “ProjectFiles”. Berikut adalah contoh program sederhana yang dibuat.

ProjectMain.h

/*
 * ProjectMain.h
 *
 * Created: 16/06/2015 20:58:47
 *  Author: Akhal
 */ 


#ifndef PROJECTMAIN_H_
#define PROJECTMAIN_H_

#include 




#endif /* PROJECTMAIN_H_ */

ProjectMain.cpp

/*
 * ProjectMain.cpp
 *
 * Created: 16/06/2015 20:59:16
 *  Author: Akhal
 */ 

#include "ProjectMain.h"

void setup()
{
	pinMode(13, OUTPUT);
	
}

void loop()
{
	
	  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
	  delay(5000);              // wait for a second
	  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
	  delay(5000);              // wait for a second
}

Seting properties

Buka project properties dengan cara: Project > Arduino Properties (Alt+F7).

Lihat pada bagian AVR/GNU C Compiler > Symbols, kemudian tambahkan F_CPU=16000000UL.

Lihat pada bagian AVR/GNU C Compiler -> Directories, kemudian tambahkan path file yang telah dicopy sebelumnya.

../src
../src/arduino
../src/arduino/avr
../src/arduino/avr/cores
../src/arduino/avr/cores/arduino
../src/arduino/avr/libraries
../src/arduino/avr/variants
../src/arduino/avr/variants/standard
../src/ProjectFiles

Lihat pada bagian AVR/GNU C++ Compiler -> Symbols, kemudian tambahkan F_CPU=16000000UL.

Lihat pada bagian AVR/GNU C++ Compiler -> Directories, kemudian tambahkan path file yang telah dicopy sebelumnya.

../src
../src/arduino
../src/arduino/avr
../src/arduino/avr/cores
../src/arduino/avr/cores/arduino
../src/arduino/avr/libraries
../src/arduino/avr/variants
../src/arduino/avr/variants/standard
../src/ProjectFiles

Build program

Build -> Build Solution (F7), dan lihat outputnya.

........
Done executing task "RunCompilerTask".
Task "RunOutputFileVerifyTask"
Program Memory Usage 	:	3010 bytes   9,2 % Full
Data Memory Usage 		:	217 bytes   10,6 % Full
Done executing task "RunOutputFileVerifyTask".
Done building target "CoreBuild" in project "Arduino.cppproj".
Target "PostBuildEvent" skipped, due to false condition; ('$(PostBuildEvent)' != '') was evaluated as ('' != '').
Target "Build" in file "C:\Program Files (x86)\Atmel\Atmel Studio 6.2\Vs\Avr.common.targets" from project "Q:\KYU\Atmel\Arduino\Arduino\Arduino.cppproj" (entry point):
Done building target "Build" in project "Arduino.cppproj".
Done building project "Arduino.cppproj".

Build succeeded.
========== Build: 1 succeeded or up-to-date, 0 failed, 0 skipped ==========

Arduino command line syntax

SYNOPSIS

arduino [FILE.ino…]

arduino [–verify|–upload] [–board package:arch:board[:parameters]] [–port portname] [–pref name=value] [-v|–verbose] [FILE.ino]

 

DESCRIPTION

The arduino integrated development environment allows editing, compiling and uploading sketches (programs) for Arduino (and compatible) microcontroller boards.

Normally, running the arduino command starts the IDE, optionally loading any .ino files specified on the commandline.

Alternatively, if any of the following command line options is given, no graphical interface will be shown and instead a one-off verify (compile) or upload will be done. A single .ino file should be given. If the sketch contains multiple .ino files, any one can be specified on the commandline, but the entire sketch will be compiled.

When running in a one-off mode, it might be useful to set the build.path preference to allow keeping intermediate build results between multiple runs and only recompile the files that changed.

Note that on MacOS X, the main executable is Arduino.app/Contents/MacOS/JavaApplicationStub instead of arduino.

 

OPTIONS

–board package:arch:board[:parameters]
Select the board to compile for.

  • package is the identifier of the vendor (the first level folders inside the hardware directory). Default arduino boards use arduino.
  • architecture is the architecture of the board (second level folders inside the hardware directory). Default arduino boards use either arduino:avr for all AVR-based boards (like Uno, Mega or Leonardo) or arduino:sam for 32bit SAM-based boards (like Arduino Due).
  • board is the actual board to use, as defined in boards.txt contained in the architecture folder selected. For example, arduino:avr:uno for the Arduino Uno, arduino:avr:diecimila for the Arduino Duemilanove or Diecimila, or arduino:avr:mega for the Arduino Mega.
  • parameters is a comma-separated list of boards specific parameters that are normally shown under submenus of the “Tools” menu. For example arduino:avr:nano:cpu=atmega168 to Select the mega168 variant of the Arduino Nano board.
If this option is not passed, the value from the current preferences is used (e.g., the last board selected in the IDE).
–port portname
Select the serial port to perform upload of the sketch. On linux and MacOS X, this should be the path to a device file (e.g., /dev/ttyACM0). On Windows, this should be the name of the serial port (e.g., COM3).
If this option is not passed, the value from the current preferences is used (e.g., the last port selected in the IDE).
–verbose-build
Enable verbose mode during build. If this option is not given, verbose mode during build is disabled regardless of the current preferences.
This option is only valid together with –verify or –upload.
–verbose-upload
Enable verbose mode during upload. If this option is not given, verbose mode during upload is disabled regardless of the current preferences.
This option is only valid together with –verify or –upload.
-v, –verbose
Enable verbose mode during build and upload. This option has the same effect of using both –verbose-build and –verbose-upload.
This option is only valid together with –verify or –upload.
–preferences-file filename
Read and store preferences from the specified filename instead of the default one.
–pref name=value
Sets the preference name to the given value.
Note that the preferences you set with this option are not validated: Invalid names will be set but never used, invalid values might lead to an error later on.
–save-prefs
Save any (changed) preferences to preferences.txt. In particular –board, –port, –pref, –verbose, –verbose-build and –verbose-upload may alter the current preferences.
–upload
Build and upload the sketch.
–verify
Build the sketch.
–get-pref preference
Prints the value of the given preference to the standard output stream. When the value does not exist, nothing is printed and the exit status is set (see EXIT STATUS below).

PREFERENCES

Arduino keeps a list of preferences, as simple name and value pairs. Below, a few of them are documented but a lot more are available.

sketchbook.path
The path where sketches are (usually) stored. This path can also contain some special subdirectories (see FILES below).
update.check
When set to true, the IDE checks for a new version on startup.
editor.external
When set to true, use an external editor (the IDE does not allow editing and reloads each file before verifying).
build.path
The path to use for building. This is where things like the preprocessed .cpp file, compiled .o files and the final .hex file go.
If set, this directory should already exist before running the arduino command.
If this preference is not set (which is normally the case), a new temporary build folder is created on every run and deleted again when the application is closed.

EXIT STATUS

0
Success
1
Build failed or upload failed
2
Sketch not found
3
Invalid (argument for) commandline option
4
Preference passed to –get-pref does not exist

FILES

~/.arduino15/preferences.txt
This file stores the preferences used for the IDE, building and uploading sketches.
My Documents/Arduino/ (Windows)
~/Documents/Arduino/ (Mac OS X)
~/Arduino/ (Linux)
This directory is referred to as the “Sketchbook” and contains the user’s sketches. The path can be changed through the sketchbook.path preference.
Apart from sketches, three special directories can be inside the sketchbook:

libraries
Libraries can be put inside this directory, one library per subdirectory.
hardware
Support for third-party hardware can be added through this directory.
tools
External code-processing tools (that can be run through the Tools menu of the IDE) can be added here.

EXAMPLES

Start the Arduino IDE, with two files open:

arduino /path/to/sketch/sketch.ino /path/to/sketch/extra.ino

Compile and upload a sketch using the last selected board and serial port

arduino --upload /path/to/sketch/sketch.ino

Compile and upload a sketch to an Arduino Nano, with an Atmega168 CPU, connected on port /dev/ttyACM0:

arduino --board arduino:avr:nano:cpu=atmega168 --port /dev/ttyACM0 --upload /path/to/sketch/sketch.ino

Compile a sketch, put the build results in the build directory an re-use any previous build results in that directory.

arduino --pref build.path=/path/to/sketch/build --verify /path/to/sketch/sketch.ino

Change the selected board and build path and do nothing else.

arduino --pref build.path=/path/to/sketch/build --board arduino:avr:uno --save-prefs

 

HISTORY

1.5.2
Added initial commandline support. This introduced –verify, –upload, –board, –port, –verbose and -v.
1.5.5
Added support for board-specific parameters to –board.
Sketch filenames are now interpreted relative to the current directory instead of the location of the arduino command itself.
1.5.6
Introduced –pref, –preferences-file, –verbose-build and –verbose-upload.
Preferences set through –pref are remembered, preferences set through –board, –port or the –verbose options are not.
When running with –verify or –upload, the full GUI is no longer shown. Error messages still use a graphical popup and on Windows, the splash screen is still shown.
1.5.8
Introduced –save-prefs.
–pref options are now not saved to the preferences file, just like –board and –port, unless –save-prefs is specified.
A path passed to –preferences-file, or set in the build.path, preferences.path or settings.path is now interpreted relative to the current directory instead of the location of the arduino command itself. 

Source:
github.com/arduino/Arduino

Download:
arduino.cc

Mirror:
Arduino 1.0.6
Arduino 1.5.8
Arduino 1.6.0