diff --git a/examples/javascript/adc121c021.js b/examples/javascript/adc121c021.js index c7f8ac4d..04925636 100644 --- a/examples/javascript/adc121c021.js +++ b/examples/javascript/adc121c021.js @@ -3,7 +3,7 @@ /*global */ /* * Author: Zion Orent -* Copyright (c) 2014 Intel Corporation. +* Copyright (c) 2015 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -28,37 +28,18 @@ // Load ADC121C021 module var I2C_ADC = require('jsupm_adc121c021'); // Instantiate an ADC121C021 on I2C bus 0 -var myI2C_ADC = new I2C_ADC.ADC121C021(I2C_ADC.ADC121C021_I2C_BUS, - I2C_ADC.ADC121C021_DEFAULT_I2C_ADDR); -var g_sum, g_iteration, g_intervalObj; -var g_total_iteration_count = 30; -// An analog sensor, such as a Grove light sensor, -// must be attached to the adc -main(); +var busID = I2C_ADC.ADC121C021_I2C_BUS; +var I2CAddr = I2C_ADC.ADC121C021_DEFAULT_I2C_ADDR; +var myI2C_ADC = new I2C_ADC.ADC121C021(busID, I2CAddr); -// this function will be called by itself -function main() +// get the data every 50 milliseconds +setInterval(function() { - g_sum = 0; - g_iteration = 0; - // Sum the value every 50 milliseconds for g_total_iteration_count times - // Then print the average value and corresponding voltage - g_intervalObj = setInterval(function(I2CADC_val) - { - g_sum += I2CADC_val; - if (g_iteration >= g_total_iteration_count) - { - g_iteration = 0; - var val = (g_sum / g_total_iteration_count); - console.log("ADC value: " + val + " Volts = " + - myI2C_ADC.valueToVolts(val)); - clearInterval(g_intervalObj); - // Now that we're done with this iteration, go to the next iteration - main(); - } - g_iteration++; - }, 50, myI2C_ADC.value()); -} + var val = myI2C_ADC.value(); + var voltsVal = myI2C_ADC.valueToVolts(val); + console.log("ADC value: " + val + " Volts = " + voltsVal); +}, 50); + // Print message when exiting process.on('SIGINT', function()