Hardware Reference
In-Depth Information
This change takes advantage of the analog percent value to evenly change the color in proportion
to the bar.
Add the following code to the arduinoSetup() function below the code line
arduino.sendDigitalPinMode(13, ARD_OUTPUT); defining the new componets. Note that the text following the
comment delimiters ( // ) is optional.
arduino.sendDigitalPinMode(3, ARD_PWM); // set pin 3 for PWM
arduino.sendDigitalPinMode(10, ARD_SERVO);// set pin 10 to accept a servo
arduino.sendServoAttach(10);// define servo information as default
isArduinoSet = true;
Listing 3-7 shows the next portion of code to add, which is the last for this example. The code handles the button
pause, the servo, and the PWM LED, and gets inserted into the arduinoLoop() function before the ending bracket of
the if (isArduinoSet) statement and after analogPin0 = arduino.getAnalog(0)/1023.0; .
Listing 3-7. The End of “Expanding on the Idea” Example
if (pin8 == ARD_HIGH){ // check if button is being pressed
pin13 = true; // flag the draw function to change
arduino.sendDigital(13, ARD_HIGH);// turn on LED
} // end if pin8 == ARD_HIGH)
else {
arduino.sendDigital(13, ARD_LOW);
arduino.sendPwm(3,analogPin0*255);
arduino.sendServo(10, analogPin0*180);
} // end else
Verifying the Code
With all the code changed and compiled, start the program with the Arduino plugged in. The program should look
like Figure 3-6 . When the screen is fully drawn, the pause button will have to be pressed to get the servo and the LED
to activate.
Figure 3-6. The look of the final example
The changes that were made make the Arduino act differently without your having to upload a new sketch.
Note that the changes now allow the potentiometer to control the sweep of the servo and the brightness of the LED
when the potentiometer is swept from maximum to minimum. Also take note of the behavior when the pause is held
while the potentiometer is being moved.
 
Search WWH ::




Custom Search