Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example 2 that can be added #14

Open
mebarton opened this issue Nov 20, 2017 · 0 comments
Open

Example 2 that can be added #14

mebarton opened this issue Nov 20, 2017 · 0 comments

Comments

@mebarton
Copy link

// To run this do the following
//
// install modules
// npm install node-ads1x15 --save
//
//
// nodejs i2c.js
//
// Code test on a Pi3 and a AUTOMATION PHAT unit
// https://shop.pimoroni.com/products/automation-phat

var ads1x15 = require('node-ads1x15');
var chip = 0; //0 for ads1015, 1 for ads1115

//Simple usage (default ADS address on pi 2b or 3):
var adc = new ads1x15(chip);

// Optionally i2c address as (chip, address) or (chip, address, i2c_dev)
// So to use /dev/i2c-0 use the line below instead...:

//var adc = new ads1x15(chip, 0x48, '/dev/i2c-1');

var channel = 0; //channel 0, 1, 2, or 3...
var samplesPerSecond = '250'; // see index.js for allowed values for your chip
var progGainAmp = '4096'; // see index.js for allowed values for your chip

var ChData =[]; //somewhere to store our reading
var dev = 127; // used to change Ch data to Voltage

// Reading the ch data
var ReadCh = function(ch, callback){
adc.readADCSingleEnded(ch, progGainAmp, samplesPerSecond, function(err, data) {
if(err){
//logging / troubleshooting code goes here...
throw err;
}
// if you made it here, then the data object contains your reading!
ChData[ch] = data // Putting data into

//console.log ('channel  ' + ch + ': ' + data);
// Calling the next ch or done
callback();

});
}

// A way to make the reads run one after the other with callbacks
var Readchall = function(){ReadCh(0, Readch1)};
var Readch1 = function(){ReadCh(1, Readch2)};
var Readch2 = function(){ReadCh(2, Readch3)};
var Readch3 = function(){ReadCh(3, ChDone)};
var ChDone = function(){
// This is run after the 4 ch have been read
console.log ('CH1: ' + (ChData[0]/dev).toFixed(2) +
'\tCH2: ' + (ChData[1]/dev).toFixed(2) +
'\tCH3: ' + (ChData[2]/dev).toFixed(2) +
'\tCH4: ' + (ChData[3]/dev).toFixed(2) );
};

if(!adc.busy)
{

// Readchall(); // - one Time Read read all 4 ch

// OUTPUT
// CH1: 0.03 CH2: 0.03 CH3: 0.03 CH4: 4.60

iv = setInterval(Readchall, 500); // Repeat every 500msec
// 100 (every 100msecs or 10 times a sec) is the max with this program
// will do with 4 ch being run each time and the Sample rate (On a pi3).

// OUTPUT
// CH1: 5.15 CH2: 0.03 CH3: 0.03 CH4: 4.60
// CH1: 5.15 CH2: 0.03 CH3: 0.03 CH4: 4.60
// CH1: 0.03 CH2: 0.03 CH3: 0.03 CH4: 4.60
// CH1: 0.03 CH2: 0.03 CH3: 0.03 CH4: 4.60

}

@mebarton mebarton changed the title Example 2 that cab be added Example 2 that can be added Nov 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant