Hardware Reference
In-Depth Information
ofCircle(50,50,50); // draw circle at (x,y,radius) in pixels for button
if(pin13 == ARD_HIGH)
ofSetColor(0,255,0); // when LED is on, draw full green
else
ofSetColor(0,127,0);// green is dimmed when LED is off
ofCircle(150,50, 50); // draw circle at (x,y,radius) in pixels for LED
ofSetColor(255,0,0); // set color for analog potentiometer
// draw rectangle with corners at (x1,y1,x2,y2)
ofRect(0, 45 ,(analogPin0*200) , 10); // rectangle is dynamic on the x-axis
// analogPin0 is a percentage multiplied by window width
} // end if (isArduinoSet)
}// end void testApp::draw()
void testApp::mousePressed(int x, int y, int button) {
if(ledcommand == true) // if LED is ON
ledcommand = false ; // flag the LED to turn OFF
else // the LED is OFF
ledcommand = true; // flag the LED to turn ON
}// end testApp::mousePressed
void testApp::arduinoSetup(const int & version) {
ofRemoveListener(arduino.EInitialized, this, &testApp::arduinoSetup);
// there is no need to continue to listen for the Arduino, so clear memory
arduino.sendAnalogPinReporting(0, ARD_ANALOG);// turn on analog pin0
arduino.sendDigitalPinMode(8, ARD_INPUT);// set digital pin8 as input
arduino.sendDigitalPinMode(13, ARD_OUTPUT);// set digital pin13 as output
isArduinoSet = true;// inform the rest of the program that Arduino is ready
}//end void testApp::arduinoSetup(
void testApp::arduinoLoop() {
// do not run this code until Arduino is operating
if (isArduinoSet){
pin8 = arduino.getDigital(8);// digital read pin8
pin13 = arduino.getDigital(13);// digital read pin13 verifying state
analogPin0 = arduino.getAnalog(0)/1023.0; // analog read A0
arduino.sendDigital(13, ledcommand);// digital write new state
}// end if (isArduinoSet)
arduino.update();// get any changes that the Arduino might have
}// end void testApp::arduinoLoop()
Verifying the Code
When done looking over and compiling the code, plug in the Arduino with the components set up in as Figure 3-3 and
the standard Firmata sketch uploaded. When running, the program will open a window with the same size as the prior
example. The program will also have the same two circles representing the button and LED, respectively performing
the same functions. A red bar is added to the program that will go from side to side, representing the full sweep of the
potentiometer.
the arduino may be required to reset, via the reset button, before the listener initializes and recognizes the
arduino. the listener is built into openFrameworks to listen for an arduino on the connection.
Note
 
 
Search WWH ::




Custom Search