-
Notifications
You must be signed in to change notification settings - Fork 11
/
adcspeedtest.js
56 lines (36 loc) · 1.36 KB
/
adcspeedtest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* ================================================
* AB Electronics UK Expander Pi - ADC Speed Test Demo
* Version 1.0 Created 19/06/2017
*
* Requires rpio to be installed, install with: npm install rpio
* Requires i2c-bus to be installed, install with: npm install i2c-bus
* run with: sudo node adcspeedtest.js
* ================================================
*/
console.reset = function () {
return process.stdout.write('\033c');
}
// link to the expanderpi library
var expanderpi = require('../../lib/expanderpi/expanderpi');
// create an instance of the adcdac class
var adc = new ExpanderPiADC();
// set the reference voltage
adc.setADCRefVoltage(4.096);
// read the raw value and voltage from channels 1 and 2 and print them on the console.
var numberOfSamples = 100000;
var sampleArray = [];
var a = 0;
var startTime = new Date();
for (var i = 0; i <= numberOfSamples; i++) {
sampleArray[i] = adc.readADCVoltage(1, 0);
}
var endTime = new Date();
var sum = 0;
for (var x = 0; x <= numberOfSamples; x++) {
sum += sampleArray[x];
}
var elapsedTime = (endTime.getTime() - startTime.getTime());
var sampleRate = (numberOfSamples / elapsedTime) * 1000;
var average = sum / numberOfSamples;
console.log("%d samples in %d ms.\nThe sample rate was %d samples per second\nThe average voltage was %dV", numberOfSamples, elapsedTime, sampleRate, average);