Arduino is a free popular platform for embedded programming based on a simple I/O board easily programmable. Interfacing it with Java allow us to create sophisticated interfaces and take advantages from the several API available in the Java ecosystem.
This is not a completely mandatory step but it will easy a lot our work. Our program will borrow some Arduino IDE libraries and configurations like which serial port it is using and at which boud rate. At the moment I wrote this tutorial the version of Arduino IDE was 0013.
Step 2) Prepare your Arduino
Connect your Arduino to the serial port in your computer. Here I’m connecting my Arduino with my laptop throught a USB.
Make sure your Arduino IDE is configured and communicating well if your Arduino. Let put on it a little program that sends to us a mensage:
void setup(){
Serial.begin(9600);
}
void loop(){
Serial.println("Is there anybody out there?");
delay(1000);
}
Step 3) Install RXTX Library
We will use some libraries to acess the serial port, some of them relies on binary implementations on our system. Our first step is to install the RXTX library (Java CommAPI) in your system. In a Debian like Linux you can do that by:
Again, this is not a mandatory step but will easy a lot our work. NetBeans is a free and open source Java IDE that will help us to develop our little application. Create a new project at File → New Project and choose at Java at Categories and Java Application at Projects.
Chose a name for your project. I called mine SerialTalker.
At the moment I wrote this tutorial I was using Netbeans version 6.5 and Java 6 update 10 but should work as well on newer and some older versions
Step 5) Adding Libraries and a Working Directory
On NetBeans the Projects tab, right-click your project and choose Properties.
On the Project Properties window select the Libraries on the Categories panel.
Click the Add JAR/Folder button.
Find where you placed your Arduino IDE installation. Inside this directory there’s a lib directory will some JAR files. Select all them and click Ok.
As we want to borrow the Arduino IDE configuration the program needs to know where is they configuration files. There’s a simple way to do that.
Still in the Project Properties window select Run at Categories panel. At Working Directory click in the Browse button and select the directory of your Arduino IDE. Mine is at /home/silveira/arduino-0013.
You can close now the Project Properties window. At this moment in autocomplete for these libraries are enable in your code.
Step 6) Codding and running
Here is the code you can replace at Main.java in your project:
You write in your computer, sends a message thought USB and Arduino translates it into a Morse code.
Just a Arduino board with a buzzer connected at the digital output 12 (one wire in the ground and the other in the 12).
I tried to make the code as general as possible so you can easily adapt it for anthers ways of transmitting a Morse code. To do that you just need to rewrite a few functions.
Reads a character from Serial. Main function loop().
Translate a ascii char into a Morse code using a reference table. A letter ‘K’ becomes a string word “-.-“. Function say_char().
Interpret the Morse word as light and sound. Mostly at function say_morse_word(). The Interpretation needs 5 functions to say all Morse words, dot(), dash(), shortgap(), mediumgap() and intragap().
int led = 13; // LED connected to digital pin 13
int buzzer = 12; // buzzer connected to digital pin 12
int unit = 50; // duration of a pulse
char * morsecode[] = {
"-----", // 0
".----", // 1
"..---", // 2
"...--", // 3
"....-", // 4
".....", // 5
"-....", // 6
"--...", // 7
"---..", // 8
"----.", // 9
"---...", // :
"-.-.-.", // ;
"", // < (there's no morse for this simbol)
"-...-", // =
"", // > (there's no morse for this simbol)
"..--..", // ?
".--._.", // @
".-", // A
"-...", // B
"-.-.", // C
"-..", // D
".", // E
"..-.", // F
"--.", // G
"....", // H
"..", // I
".---", // J
"-.-", // K
".-..", // L
"--", // M
"-.", // N
"---", // O
".--.", // P
"--.-", // Q
".-.", // R
"...", // S
"-", // T
"..-", // U
"...-", // V
".--", // W
"-..-", // X
"-.--", // Y
"--.." // Z
};
void setup() {
pinMode(led, OUTPUT);
pinMode(buzzer, OUTPUT);
Serial.begin(9600);
}
void say_morse_word(char * msg){
int index = 0;
while(msg[index]!='\0'){
// say a dash
if(msg[index]=='-'){
dash();
}
// say a dot
if(msg[index]=='.'){
dot();
}
// gap beetween simbols
intragap();
index++;
}
}
// beep
void beep(int time){
int i;
int t = 100; // period of the wav. bigger means lower pitch.
int beepduration = (int)((float)time/t*1800);
digitalWrite(led, HIGH);
for(i=0;i='0')&&(letter<='Z')&&(letter!='<')&&(letter!='>')){
Serial.print(morsecode[letter-'0']);
Serial.print(' ');
say_morse_word(morsecode[letter-'0']);
shortgap();
} else {
if(letter==' '){
Serial.print(" \\ ");
mediumgap();
}else{
Serial.print("X");
}
}
}
void loop(){
if(Serial.available()){
say_char((char)Serial.read());
}
}
Additionally you can put another function to say entire strings, like say_string(“HELLO WORLD”)
Para assistir o replay da apresentação, clique aqui.
Nos exemplos de código eu procurei seguir a abordagem do Robert Eckstein no videocast Learn JavaFX Script in 15 minutes. O resultado foi muito bom para apresentar a sintáxe da linguagem.
Exemplo 1
def raio = 4;
def Pi = 3.1415;
var area = Pi * (raio * raio);
println("Ãrea do cÃrculo: {area}");
SaÃda do Exemplo 1:
Ãrea do cÃrculo: 50.264
Exemplo 2
var isActive = true;
var isVisible:Boolean = false;
println("Active: {isActive}");
println("Visible: {isVisible}");
isVisible = true;
println("Visible: {isVisible}");
SaÃda do Exemplo 2:
Active: true
Visible: false
Visible: true
Exemplo 3
var integer1 = 3;
var number1 = 3.0;
var integer2:Integer = 3;
var number2:Number = 3.0;
var number3:Number = integer1;
println("Integer 1: {integer1}");
println("Integer 2: {integer2}");
println("Number 1: {number1}");
println("Number 2: {number2}");
println("Number 3 (copiado do integer 3): {number3}");
println("Number 1 como Integer: {number1 as Integer}");
SaÃda do Exemplo 3:
Integer 1: 3
Integer 2: 3
Number 1: 3.0
Number 2: 3.0
Number 3 (copiado do integer 3): 3.0
Number 1 como Integer: 3
class Monstro {
var nome: String;
var nÃvel: Integer;
var vida: Number;
}
var ogr = Monstro {
nome: "Ogro"
nÃvel: 16;
vida: 100.0;
}
println("nome: {ogr.nome}");
println("nÃvel: {ogr.nÃvel}");
println("vida: {ogr.vida}");
SaÃda do Exemplo 9:
nome: Ogro
nÃvel: 16
vida: 100.0
Exemplo 10
class Monstro {
var nome: String;
var nÃvel: Integer;
var vida: Number;
var força: Number;
function porrada(outro:Monstro){
outro.vida = outro.vida - força;
}
}
Exemplo 11
abstract class MonstroVoador {
var nome: String = "monstro voador";
abstract function voa():Void;
}
class Griffon extends MonstroVoador{
override function voa(){
println("estou voando =D");
}
}
Exemplo 12
var software: String[] = ["NetBeans", "Java", "JavaFX"];
var hardware: String[] = ["UltraSparc", "Niagra", "SunSpot"];
var oferta = [software,hardware];
println(software);
println(hardware);
println(oferta);
var umadez = [1..10];
var copia = umadez;
println(umadez);
println(copia);
var outracopia = umadez[valor|true];
println(outracopia);
var sopares = umadez[valor|(valor mod 2)==0];
println(sopares);
O homem está na cidade
como uma coisa está em outra
e a cidade está no homem
que está em outra cidade
mas variados são os modos
como uma coisa
está em outra coisa:
o homem, por exemplo, não está na cidade
como uma árvore está
em qualquer outra
nem como uma árvore
está em qualquer uma de suas folhas
(mesmo rolando longe dela)
O homem não está na cidade
como uma árvore está num livro
quando um vento ali a folheia
a cidade está no homem
mas não da mesma maneira
que um pássaro está numa árvore
não da mesma maneira que um pássaro
(a imagem dele)
está/va na água
e nem da mesma maneira
que o susto do pássaro
está no pássaro que eu escrevo
a cidade está no homem
quase como a árvore voa
no pássaro que a deixa
cada coisa está em outra
de sua própria maneira
e de maneira distinta
de como está em si mesma
a cidade não está no homem
do mesmo modo que em sua
quitandas praças e ruas
No caso eu estou usando a taxa de amostragem em 48kHz e dois canais de áudio (stereo) para ficar compatÃvel com outros formatos que eu estou usando no Cinelerra.
Pronto, voltamos a ser felizes juntos, eu, a câmera e o Cinelerra.