This repository was archived by the owner on Aug 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArduinoPinTester.pde
104 lines (97 loc) · 2.84 KB
/
ArduinoPinTester.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
boolean errorOccured = false;
int selectedPin = 0;
boolean[] digitalOut = new boolean[14];
int[] analogOut = new int[14];
boolean[] pwmSupported = {false, false, false, true, false, true, true, false, false, true, true, true, false, false};
boolean[] pwmOut = new boolean[14];
/* --- Properties start --- */
String serialPort = ""; //Arduino serial port.
String fontFile = "CourierNewPSMT-48.vlw"; //Font file name.
int analogSensitivity = 4; //Determines how fast analog bar moves.
/* --- Properties end --- */
void setup() {
if (serialPort == "") {
println("Error: \"serialPort\" is not specified.");
errorOccured = true;
return;
}
try {
arduino = new Arduino(this, serialPort, 57600);
}
catch(Exception exception) {
println("Error: An error occurred while connecting to Arduino.");
errorOccured = true;
return;
}
for(int i = 0; i < 14; i++) {
digitalOut[i] = false;
analogOut[i] = 0;
pwmOut[i] = false;
}
size(945, 715);
textFont(loadFont(fontFile));
}
void draw() {
if(errorOccured) exit(); //Exits if any errors occured in setup();
background(120);
fill(0, 255, 0);
stroke(0, 255, 0);
square(20, selectedPin * 50 + 20, 20);
for(int i = 0; i < 14; i++) {
fill(255);
textSize(30);
text("D" + i, 60, i * 50 + 40);
if(pwmSupported[i]) {
if(pwmOut[i]) fill(255, 255, 0);
else fill(255);
textSize(20);
text("PWM", 130, i * 50 + 40);
}
stroke(255);
if(pwmOut[i]) {
noFill();
rect(200, i * 50 + 20, 120, 20);
fill(255);
rect(320 - 120 * ((float)analogOut[i] / 255), i * 50 + 20, 120 * ((float)analogOut[i] / 255), 20);
arduino.analogWrite(i, analogOut[i]);
}
else {
if(digitalOut[i]) fill(255);
else noFill();
square(300, i * 50 + 20, 20);
arduino.pinMode(i, Arduino.OUTPUT);
if(digitalOut[i]) arduino.digitalWrite(i, Arduino.HIGH);
else arduino.digitalWrite(i, Arduino.LOW);
}
}
text("Controls", 380, 580);
textSize(20);
text("Up Down: Select pins", 400, 610);
text("Left Right: Increase/Decrease analog output", 400, 630);
text("Z: Toggle digital output", 400, 650);
text("X: Toggle analog/digital output", 400, 670);
text("Esc: Exit", 400, 690);
}
void keyPressed() {
if(keyCode == 40) {
selectedPin++;
if(selectedPin > 13) selectedPin = 0;
}
else if(keyCode == 38) {
selectedPin--;
if(selectedPin < 0) selectedPin = 13;
}
else if(keyCode == 37 && pwmOut[selectedPin]) {
analogOut[selectedPin] += 4;
if(analogOut[selectedPin] > 255) analogOut[selectedPin] = 255;
}
else if(keyCode == 39 && pwmOut[selectedPin]) {
analogOut[selectedPin] -= 4;
if(analogOut[selectedPin] < 0) analogOut[selectedPin] = 0;
}
else if(keyCode == 90 && !pwmOut[selectedPin]) digitalOut[selectedPin] = !digitalOut[selectedPin];
else if(keyCode == 88 && pwmSupported[selectedPin]) pwmOut[selectedPin] = !pwmOut[selectedPin];
}