| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,27 @@ | |||
| 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 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,15 @@ | |||
| 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 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments