ofArduino
This is a way to control an Arduino that has had the firmata library loaded onto it, from OF. To load firmata onto your Arduino, run the Arduino IDE, open the Examples > Firmata > StandardFirmata sketch, and upload it to the Arduino board. Once the ofArduino instance returns true from isArduinoReady() you can set the mode of the different digital pins using sendDigitalPinMode()
~~~~{.cpp}
sendDigitalPinMode(9, ARD_INPUT)
~~~~
This sets pin 9 to input so that it can read a button press, while:
~~~~{.cpp}
sendDigitalPinMode(9, ARD_PWM)
~~~~
sets pin 9 to be a PWM out pin. Note that this only works on pins that are PWM enabled.
- connect(...)
- connect(...)
- disconnect()
- isArduinoReady()
- update()
- setUseDelay(...)
- isInitialized()
- sendDigitalPinMode(...)
- sendAnalogPinReporting(...)
- sendDigital(...)
- sendPwm(...)
- sendString(...)
- sendReset()
- sendByte(...)
- getPwm(...)
- getDigital(...)
- getAnalog(...)
- getString()
- getDigitalPinMode(...)
- getAnalogPinReporting(...)
ofArduino methods
connect(...)
bool ofArduino::connect(string device, int baud)
opens a serial port connection to the arduino
disconnect()
void ofArduino::disconnect()
closes the serial port connection. Does not turn the Arduino off.
update()
void ofArduino::update()
polls data from the serial port, this has to be called periodically
isInitialized()
bool ofArduino::isInitialized()
returns true if a succesfull connection has been established and the Arduino has reported a firmware
sendDigitalPinMode(...)
void ofArduino::sendDigitalPinMode(int pin, int mode)
On the Arduino Uno pin: 2-13 mode: ARD_INPUT, ARD_OUTPUT, ARD_PWM setting a pins mode to ARD_INPUT turns on reporting for the port the pin is on Note: analog pins 0-5 can be used as digitial pins 16-21 but if the mode of one of these pins is set to ARD_INPUT then all analog pin reporting will be turned off
sendPwm(...)
void ofArduino::sendPwm(int pin, int value, bool force)
On the Uno this will work on pins: 3, 5, 6, 9, 10 and 11 value: 0 (always off) to 255 (always on). the pins mode has to be set to ARD_PWM TODO check if the PWM bug still is there causing frequent digital port reporting...
sendString(...)
void ofArduino::sendString(string str)
firmata can not handle strings longer than 12 characters.
sendReset()
void ofArduino::sendReset()
This will cause your Arduino to reset and boot into the program again.
sendByte(...)
void ofArduino::sendByte(unsigned char byte)
sends a byte without wrapping it in a firmata message, data has to be in the 0-127 range, values > 127 will be interpreted as commands.
getPwm(...)
int ofArduino::getPwm(int pin)
On the Arduino Uno pin: 3, 5, 6, 9, 10 and 11 returns the last set PWM value (0-255) for the given pin the pins mode has to be ARD_PWM Note: pin 16-21 can also be used if analog inputs 0-5 are used as digital pins
getDigital(...)
int ofArduino::getDigital(int pin)
On the Arduino Uno pin: 2-13 returns the last received value (if the pin mode is ARD_INPUT) or the last set value (if the pin mode is ARD_OUTPUT) for the given pin Note: pin 16-21 can also be used if analog inputs 0-5 are used as digital pins Returns whether the pin is reading high or low, 1 or 0. You can test against this with an if() statement which is handy:
if(arduino.getDigital(pin)){
// do something on high
}else{
// do something on low
}
getAnalog(...)
int ofArduino::getAnalog(int pin)
Returns the analog in value that the pin is currently reading. because the Arduino has a 10 bit ADC you get between 0 and 1023 for possible values.
getDigitalPinMode(...)
int ofArduino::getDigitalPinMode(int pin)
returns ARD_INPUT, ARD_OUTPUT, ARD_PWM, ARD_SERVO, ARD_ANALOG
