mirror of
https://github.com/processing/processing4.git
synced 2026-06-16 04:26:26 +02:00
IO: Unify ADC examples
This commit is contained in:
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user