Last active
October 7, 2016 07:34
-
-
Save LarsBergqvist/d7d27744b9426408bf62a1691d351277 to your computer and use it in GitHub Desktop.
Connecting an Arduino to a Processing sketch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int maxBarHeight = 600; | |
int barWidth = 400; | |
//IArduinoIntegration arduino = new ArduinoFakeIntegration(); | |
IArduinoIntegration arduino = new ArduinoIntegration(this); | |
// A list with bars to draw | |
// Specify the matching Arduino analog pin number and a color for the bar that should represent the input voltage | |
BarSpec[] barSpecs = { new BarSpec(0, color(150,150,20)), | |
new BarSpec(1, color(20,100,20)), | |
new BarSpec(2, color(20,20,100)) | |
}; | |
void setup() { | |
size(1200,600); | |
for (BarSpec barSpec : barSpecs) { | |
arduino.setupAnalogPinAsInput(barSpec.pinNumber); | |
} | |
} | |
void draw() { | |
background(0); | |
int numPinsDrawn = 0; | |
for (BarSpec barSpec : barSpecs) { | |
float v = arduino.getVoltageFromPin(barSpec.pinNumber); | |
// Draw the bar | |
int barXPos = barWidth*numPinsDrawn; | |
float barHeight = map(v,0,5.0,0,maxBarHeight); | |
fill(barSpec.barColor); | |
rect(barXPos,height-barHeight,barWidth,barHeight); | |
// Draw the text within the bar | |
fill(200,200,200); | |
textSize(40); | |
text(round(v*100)/100.0 + "[V]",barXPos,height-80); | |
text("Pin A" + barSpec.pinNumber,barXPos,height-40); | |
numPinsDrawn++; | |
} | |
delay(200); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment