Skip to content

Instantly share code, notes, and snippets.

@LarsBergqvist
Last active October 6, 2016 10:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LarsBergqvist/08465b5326caad7ee63ef4667cd62d36 to your computer and use it in GitHub Desktop.
Save LarsBergqvist/08465b5326caad7ee63ef4667cd62d36 to your computer and use it in GitHub Desktop.
Wrapper class for using the Arduino Firmata library in Processing
import processing.core.PApplet;
import processing.serial.*;
import cc.arduino.*;
public class ArduinoIntegration implements IArduinoIntegration {
// Change this so that it matches the usb device connected to the Arduino
String arduinoUSBDeviceName = "/dev/tty.usbmodem1421";
Arduino arduino;
ArduinoIntegration(PApplet applet) {
try {
arduino = new Arduino(applet, arduinoUSBDeviceName , 57600);
}
catch(Exception e) {
println(e.getMessage());
}
}
public float getVoltageFromPin(int pinNumber) {
if (arduino == null)
return 0;
float pinValue = arduino.analogRead(pinNumber);
float pinValueInVolts = map(pinValue,0,1024,0,5);
return pinValueInVolts;
}
public void setupAnalogPinAsInput(int pinNumber) {
if (arduino != null) {
arduino.pinMode(pinNumber, Arduino.INPUT);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment