Skip to content

Commit b07707e

Browse files
committed
IO: Unify ADC examples
1 parent d95a2ba commit b07707e

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

java/libraries/io/examples/AnalogDigital_SPI_MCP3001/AnalogDigital_SPI_MCP3001.pde

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,12 @@ void setup() {
99
}
1010

1111
void draw() {
12-
background(adc.getAnalog() * 255);
12+
// this will return a number between 0 and 1
13+
float measured = adc.analogRead();
14+
15+
// multiply with the supply voltage to get an absolute value
16+
float volts = 3.3 * measured;
17+
println("Analog Input is " + volts + "V");
18+
19+
background(measured * 255);
1320
}

java/libraries/io/examples/AnalogDigital_SPI_MCP3001/MCP3001.pde

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ class MCP3001 extends SPI {
1010
settings(500000, SPI.MSBFIRST, SPI.MODE0);
1111
}
1212

13-
float getAnalog() {
13+
// returns a number between 0.0 and 1.0
14+
float analogRead() {
1415
// dummy write, actual values don't matter
1516
byte[] out = { 0, 0 };
1617
byte[] in = transfer(out);

java/libraries/io/examples/AnalogDigital_SPI_MCP3008/AnalogDigital_SPI_MCP3008.pde

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@ void setup() {
99
}
1010

1111
void draw() {
12-
background(adc.getAnalog(0) * 255);
13-
fill(adc.getAnalog(1) * 255);
12+
// this will return a number between 0 and 1
13+
float measured = adc.analogRead(0);
14+
15+
// multiply with the supply voltage to get an absolute value
16+
float volts = 3.3 * measured;
17+
println("Analog Input 0 is " + volts + "V");
18+
19+
background(255);
20+
fill(measured * 255);
1421
ellipse(width/2, height/2, width * 0.75, width * 0.75);
1522
}

java/libraries/io/examples/AnalogDigital_SPI_MCP3008/MCP3008.pde

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ class MCP3008 extends SPI {
1111
settings(500000, SPI.MSBFIRST, SPI.MODE0);
1212
}
1313

14-
float getAnalog(int channel) {
14+
// returns a number between 0.0 and 1.0
15+
float analogRead(int channel) {
1516
if (channel < 0 || 7 < channel) {
1617
System.err.println("The channel needs to be from 0 to 7");
1718
throw new IllegalArgumentException("Unexpected channel");

0 commit comments

Comments
 (0)