GeezerVison is showing github.com — IO: Add an 8-channel ADC example using the MCP3008 · dimr/processing@736897b
File tree
Expand file treeCollapse file tree
Open diff view settings
java/libraries/io/examples/SPIAnalogDigitalOOP8
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboard +27Lines changed: 27 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +import processing.io.SPI; |
| 2 | + |
| 3 | +// MCP3008 is a Analog-to-Digital converter using SPI |
| 4 | +// other than the MCP3001, this has 8 input channels |
| 5 | +// datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/21295d.pdf |
| 6 | + |
| 7 | +class MCP3008 extends SPI { |
| 8 | + |
| 9 | + MCP3008(String dev) { |
| 10 | + super(dev); |
| 11 | + super.settings(500000, SPI.MSBFIRST, SPI.MODE0); |
| 12 | + } |
| 13 | + |
| 14 | + float getAnalog(int channel) { |
| 15 | + if (channel < 0 || 7 < channel) { |
| 16 | + System.err.println("The channel needs to be from 0 to 7"); |
| 17 | + throw new IllegalArgumentException("Unexpected channel"); |
| 18 | + } |
| 19 | + byte[] out = { 0, 0, 0 }; |
| 20 | + // encode the channel number in the first byte |
| 21 | + out[0] = (byte)(0x18 | channel); |
| 22 | + byte[] in = super.transfer(out); |
| 23 | + int val = ((in[1] & 0x03) << 8) | (in[2] & 0xff); |
| 24 | + // val is between 0 and 1023 |
| 25 | + return val/1023.0; |
| 26 | + } |
| 27 | +} |
Collapse file
Copy file name to clipboard +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +import processing.io.*; |
| 2 | +MCP3008 adc; |
| 3 | + |
| 4 | +// see setup.png in the sketch folder for wiring details |
| 5 | + |
| 6 | +void setup() { |
| 7 | + //printArray(SPI.list()); |
| 8 | + adc = new MCP3008(SPI.list()[0]); |
| 9 | +} |
| 10 | + |
| 11 | +void draw() { |
| 12 | + background(adc.getAnalog(0) * 255); |
| 13 | + fill(adc.getAnalog(1) * 255); |
| 14 | + ellipse(width/2, height/2, width * 0.75, width * 0.75); |
| 15 | +} |
Collapse file
Copy file name to clipboard 68.4 KB
- Display the source diff
- Display the rich diff