Arduino element
Together with the Firmata 2 firmware (an Arduino sketch uploaded to the
Arduino board), this element allows you to control an Arduino board from
your EJS simulation. You will be able to:
- read from and write to any of the digital pins (from 2 to 13, pins 0 and 1 are used for communication)
- read the input from the analog pins (from 0 to 5)
- control servo motors attached to any of the digital pins
The Arduino board an be connected either by:
- ethernet connection, which requires the StandarFirmataEthernet sketch
- serial (USB) connection, with requires the StandardFirmata sketch
Usage
To add the element, simply drag and drop the element from the palette, to the list of elements for your simulation.
If the board is connected to the ethernet, edit the "Server IP:" and "Port number:" fields accordingly.
(The board must be loaded with the StandardFirmataEthernet sketch of the Firmata 2 firmware with the corresponding IP address and port.)
Connection methods
- boolean connectSerial(String usbPort); Connects to the board on the given serial (USB) port at the default baudrate of 57600. Returns true if successful.
- boolean connectEthernet(String ipAddress, int portnumber); Connects to the board on the given ethernet IP address at the indicated port number. Returns true if successful.
- boolean connect(); Connects to the board at the IP and port number presribed in the element's dialog. Returns true if successful.
- boolean close();: Closes the connection to the board. Returns true if successful.
Information methods
- String readFirmwareInformation(); Returns the firmware version information. Returns null if failed to communicate.
- String readMappingInformation(); Returns information about which pins correspond to analog channels. Returns null if failed to communicate.
Configuration methods
- boolean setDigitalMode(int digitalPin, int mode); Sets the mode of a digital pin. The mode must be one of:
- 0 = INPUT
- 1 = OUPUT
- 2 = ANALOG
- 3 = PWM
- 4 = SERVO
Returns true if successful.
- int getDigitalMode(int digitalPin); Returns the mode of a digital pin.
- boolean boolean setDigitalServoMode(int digitalPin, int minPulse, int maxPulse); Configures a digital pin to control a servo.
Parameter minPulse, resp. maxPulse, is the pulse width, in microseconds, corresponding to the minimum (0 degree), resp. maximum (180 degrees), angle on the servo.
- boolean boolean setDigitalServoMode(int digitalPin);Same as setDigitalServoMode(int digitalPin,544,2400);
Output methods
- boolean writeDigital(int digitalPin, boolean on); Sets a digital pin On (5 volts) or Off (0 volts). Returns true if successful.
- boolean writeDigital(int digitalPin, int value); Sets the (PWM-wave) value of a digital pin, from 0 (always off) to 255 (always on). Returns true if successful.
- boolean writeDigitalServo(int digitalPin, int angle); Sets the position of the servo (in degrees), from 0 to 180. Returns true if successful.
Input methods
- boolean readDigital(int digitalPin); Returns whether a digital pin is on
- int readDigitalValue(int digitalPin); Returns the value of a digital pin
- int readAnalog(int analogPin); Returns the value read from an analog pin (from 0 to 1023)
- double readAnalogVoltage(int pin, double voltageReference); Returns the voltage read from an analog pin in the interval [0,voltageReference]
- double readAnalogVoltage(int pin); Returns the voltage read from an analog pin in the interval [0,5]
Example of use
if (!arduino.connect("/dev/tty.usbmodem1421",57600)) { // "/dev/tty.usbmodem1421" a USB port on a MAc computer. Try "COM3" on a Windows machine
_view.alert("","Error","Arduino board not found:\n "+arduino);
_pause();
}
else {
_println("Arduino connected: "+arduino);
arduino.writeDigital(8, true); // Sets the digital pin number 8 to ON
}