Java Magazine, September/October 2016
ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2016 16 internet of things Three 270Ω resistors with 5 percent tolerance The color bands for the resistor are red violet brown and gold Figure 1 shows the previously mentioned electronic components connected to a breadboard the necessary wirings and the wirings from the Intel Galileo Gen 2 board to the breadboard I created the original diagram with the popular Fritzing multiplatform application The Fritzing file is included in the code bundle related to this article which is available in the Java Magazine download area There are three PWM capable GPIO general purpose input output pins 6 5 and 3 Each of them is connected to a 270Ω resistor and wired to an anode pin for each LED color The common cathode is connected to ground GND The analog input pin labeled A0 is connected to the voltage divider built from the photoresistor and a 10 kΩ resistor with 5 percent tolerance The photoresistor is wired to the IOREF pin I am using the boards default configuration and therefore the IOREF voltage is 5V The 10 kΩ resistor is wired to GND Coding a Class to Dim an LED by Using PWM Once you finish all the necessary wiring you need to write Java code to determine whether you are in a dark environment and then control the brightness of the three colors of the RGB LED based on the ambient light value The code reads the result of converting a resistance value into a voltage and then transforms this analog value into its digital representation The code maps the digital value to a voltage value and then it maps this voltage value to a darkness or ambient light measurement value In order to keep the code easier to read and understand the next Java classes I explain wont perform checks on the results of each operation performed However in a final version of the example you should check the result of each call to a method of an instance of the diferent mraa classes and make sure that the returned value is equal to mraa Result SUCCESS I am going to create the following three classes VariableBrightnessLed This class represents an LED connected to the board and it will allow me to control its brightness level VoltageInput This class represents a voltage source connected to an analog input pin in the board and it will allow me to map the raw values read from the analog input into voltage values SimpleLightSensor This class represents a light sensor and it will allow me to transform a voltage value measured with a VoltageInput instance into a light measurement and description Then I will create a BoardManager class that creates instances Figure 1 The electronic components connected to a breadboard and wired to the Intel Galileo Gen 2 board
You must have JavaScript enabled to view digital editions.