Skip to content
KingAndJoker edited this page Jun 17, 2022 · 10 revisions

A 2-minute tutorial to get started with JArduino

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.

Step0: Upload the JArduino firmware on your Arduino board

  1. Copy the JArduino folder (located in the distribution in org.sintef.jarduino.samples/arduino) into /libraries/
  2. Launch your Arduino environment
  3. File -> Examples -> JArduino -> JArduino firmware. It should open an Arduino program that you should upload to your board using the normal Arduino procedure.
  4. Your Arduino board is now ready for Jarduino. You can exit the Arduino environment forever and launch Eclipse. Just run the Java/JArduino program.

Step1: Extend the JArduino abstract class

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);
  }
}

Step2: Implement the setup method

	@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.

Step3: Implement the logic

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.

Step4: Write a main and enjoy!

	public static void main(String[] args) {
		JArduino arduino = new Blink("COM21");
		arduino.runArduinoProcess();
	}

Hopefully, the LED should now blink!

The JArduino team.