IO: Unify ADC examples

This commit is contained in:
gohai
2018-07-26 00:11:54 -07:00
parent a21e5a280e
commit a7e2d6f122
4 changed files with 21 additions and 5 deletions
@@ -9,5 +9,12 @@ void setup() {
}
void draw() {
background(adc.getAnalog() * 255);
// this will return a number between 0 and 1
float measured = adc.analogRead();
// multiply with the supply voltage to get an absolute value
float volts = 3.3 * measured;
println("Analog Input is " + volts + "V");
background(measured * 255);
}
@@ -10,7 +10,8 @@ class MCP3001 extends SPI {
settings(500000, SPI.MSBFIRST, SPI.MODE0);
}
float getAnalog() {
// returns a number between 0.0 and 1.0
float analogRead() {
// dummy write, actual values don't matter
byte[] out = { 0, 0 };
byte[] in = transfer(out);
@@ -9,7 +9,14 @@ void setup() {
}
void draw() {
background(adc.getAnalog(0) * 255);
fill(adc.getAnalog(1) * 255);
// this will return a number between 0 and 1
float measured = adc.analogRead(0);
// multiply with the supply voltage to get an absolute value
float volts = 3.3 * measured;
println("Analog Input 0 is " + volts + "V");
background(255);
fill(measured * 255);
ellipse(width/2, height/2, width * 0.75, width * 0.75);
}
@@ -11,7 +11,8 @@ class MCP3008 extends SPI {
settings(500000, SPI.MSBFIRST, SPI.MODE0);
}
float getAnalog(int channel) {
// returns a number between 0.0 and 1.0
float analogRead(int channel) {
if (channel < 0 || 7 < channel) {
System.err.println("The channel needs to be from 0 to 7");
throw new IllegalArgumentException("Unexpected channel");