-
Notifications
You must be signed in to change notification settings - Fork 63
2 Minute Tutorial
We have ported some of the examples provided by Arduino. This tutorial describes how to implement the Blink example (see http://www.arduino.cc/en/Tutorial/Blink for the Arduino version) in JArduino.
- Copy the JArduino folder (located in the distribution in org.sintef.jarduino.samples/arduino) into /libraries/
- Launch your Arduino environment
- File -> Examples -> JArduino -> JArduino firmware. It should open an Arduino program that you should upload to your board using the normal Arduino procedure.
- Your Arduino board is now ready for Jarduino. You can exit the Arduino environment forever and launch Eclipse. Just run the Java/JArduino program.
import org.sintef.jarduino.JArduino;
public class Blink extends JArduino {
//Constructor taking a String describing the serial port where the Arduino Board is connected (eg, "COM7")
public Blink(String port) {
super(port);
}
}
@Override
protected void setup() {
// initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
pinMode(DigitalPin.PIN_13, PinMode.OUTPUT);
}
This code simply sets the pin 13 (connected to a LED) as an output. Note that JArduino relies on enumeration to describe pins, modes, etc. In Arduino you would have to rely on integers... which would allow you to set non-existing pins with non-existing modes... In other words, JArduino is safer and provides better guidance to developpers.
One easy way to implement the logic of your component is to overide the loop method:
@Override
protected void loop() {
// set the LED on
digitalWrite(DigitalPin.PIN_13, DigitalState.HIGH);
delay(1000); // wait for a second
// set the LED off
digitalWrite(DigitalPin.PIN_13, DigitalState.LOW);
delay(1000); // wait for a second
}
But you can off course define your own methods, specify complex flows that would not fit into the loop, etc.
public static void main(String[] args) {
JArduino arduino = new Blink("COM21");
arduino.runArduinoProcess();
}
Hopefully, the LED should now blink!
The JArduino team.