Java Magazine, Sept/Oct 2016
ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2016 19 internet of things public int getAnalogPin return analogPin When I create an instance of the VoltageInput class it is necessary to specify in the analogPin int argument the analog pin number to which the voltage source is connected The constructor saves the received analog pin number in the analogPin field which is read only accessible through the getAnalogPin method Then the constructor creates a new mraa Aio instance with the received analogPin as its pin argument saves its reference in the aio field and calls its setBit method to configure the analog to digital converter ADC resolution to 12 bits This way the ADC will provide 4096 possible values 212 4096 to represent from 0V to 5V A 0 value read from the ADC represents 0V and 4095 means 5V It is necessary to apply a linear function to convert the raw values read from the analog pin and map them to the corresponding input voltage values Thus the code multiplies the raw value read from the analog pin by 5 and divides it by 4095 to obtain the input voltage value from the raw value As I am using 12 bits of resolution the detected values will have a step of 5V 4095 0001220012V that is approximately 122 millivolts The VoltageInput class declares a getVoltage method that calls the this aio read method to retrieve the raw value from the analog pin and saves it in the rawValue long variable The code saves the value in this variable to make it easy to debug and understand how the code works Then the method calculates the result of dividing rawValue by 4095 and multiplying it by 5 and saves it to the float voltageValue variable Finally the method returns the value of the voltageValue variable This way the method returns the voltage value converted from the raw value retrieved by the this aio read method I have a class that allows me to retrieve a voltage value from a voltage source Now I will create a new Simple LightSensor class that represents the photoresistor which is included in the voltage divider and wired to an analog pin of the board The new class uses the previously coded VoltageInput class that reads and transforms an analog input The new class allows me to transform a voltage value into a light measurement and description The following lines show the code for this new class class SimpleLightSensor Light level descriptions public static final String LL_ EXTREMELY_ DARK Extremely dark public static final String LL_ VERY_ DARK Very dark public static final String LL_ JUST_ DARK Just dark public static final String LL_ SUNNY_ DAY Like a sunny day Maximum voltages that determine the light level private static final float EXTREMELY_ DARK 21f private static final float VERY_ DARK 31f private static final float JUST_ DARK 405f private final VoltageInput voltageInput private float measuredVoltage 0f private String lightLevel SimpleLightSensor LL_ SUNNY_ DAY public SimpleLightSensor int analogPin this voltageInput new VoltageInput analogPin
You must have JavaScript enabled to view digital editions.