An event based analog input reader with sampling for less jittery values. Currently only tested using a Raspberry Pi 2 along with an MCP3008. Might work with other SPI setups as well. Heavily based on Ladyada's python code.
npm i --save analog-reader
For instructions on wiring the MCP3008 to a Raspberry Pi, see here.
After that, configure the pins to be used and initialize the reader
const AnalogReader = ;const CLOCKPIN = 18;const MOSIPIN = 24;const MISOPIN = 23;const CSPIN = 25;var reader = CLOCKPINMOSIPINMISOPINCSPIN;
After initializing you can change some properties, such as the
// Sets how often (milliseconds) should we poll for data, defaults to 2 ms.// I'm not sure how to get how low can you go, as it will depend on the setup.readerreadDelay = 2;// Setting a higher sample size will take longer. It's a tradeoff between accuracy and speed. Defaults to 50.readersampleSize = 50;
Watching / unwatching
// Watch input 0, 1 and 2reader;reader;reader;// Stop watching input 1reader;
Set up a listener
reader// Starts reading!readerstart
Finally, to stop reading, try
// Stops reading! Can be resumed later!reader
To manually destroy the reader, use the following. This will
unexport and free up the used pins. It should be automatically called when stopping your program (
SIGINT) as well.
Once done you can't start listening again with the same reader.
npm install and then run
npm start to compile from coffeescript.
TODO proper testing.
npm run test currently runs a
webpack-dev-server with a mocked up
read method to test the sampling behaviour.