Browse Source

Added Arduino sketch

Thomas Buck 9 years ago
parent
commit
e512251ccc
1 changed files with 119 additions and 0 deletions
  1. 119
    0
      CaseLights.ino

+ 119
- 0
CaseLights.ino View File

@@ -0,0 +1,119 @@
1
+/*
2
+ * CaseLights
3
+ * 
4
+ * Arduino RGB LED Controller with Serial interface.
5
+ * 
6
+ * Two commands are supported, ending with a new-line:
7
+ * 
8
+ *     RGB r g b
9
+ *     
10
+ *     UV s
11
+ * 
12
+ * The RGB command sets the PWM output for the LEDs.
13
+ * The UV command turns the UV lights on or off (s can be 0 or 1).
14
+ */
15
+
16
+//#define DEBUG
17
+
18
+static int redPin = 10;
19
+static int greenPin = 9;
20
+static int bluePin = 11;
21
+static int uvPin = 13;
22
+
23
+void setup() {
24
+  Serial.begin(115200);
25
+  
26
+  pinMode(redPin, OUTPUT);
27
+  pinMode(greenPin, OUTPUT);
28
+  pinMode(bluePin, OUTPUT);
29
+  pinMode(uvPin, OUTPUT);
30
+  
31
+  analogWrite(redPin, 0);
32
+  analogWrite(greenPin, 0);
33
+  analogWrite(bluePin, 0);
34
+  digitalWrite(uvPin, LOW);
35
+
36
+#ifdef DEBUG
37
+  Serial.println("CaseLights initialized");
38
+#endif
39
+}
40
+
41
+void loop() {
42
+  if (Serial.available() > 0) {
43
+    int c = Serial.read();
44
+    if (c == 'R') {
45
+      c = Serial.read();
46
+      if (c == 'G') {
47
+        c = Serial.read();
48
+        if (c == 'B') {
49
+          int r = Serial.parseInt();
50
+          int g = Serial.parseInt();
51
+          int b = Serial.parseInt();
52
+          analogWrite(redPin, r);
53
+          analogWrite(greenPin, g);
54
+          analogWrite(bluePin, b);
55
+#ifdef DEBUG
56
+          Serial.print("RGB set ");
57
+          Serial.print(r);
58
+          Serial.print(' ');
59
+          Serial.print(g);
60
+          Serial.print(' ');
61
+          Serial.print(b);
62
+          Serial.println();
63
+#endif
64
+        } else {
65
+#ifdef DEBUG
66
+          Serial.print("Invalid character after G: ");
67
+          Serial.print(c);
68
+          Serial.println();
69
+#endif
70
+        }
71
+      } else {
72
+#ifdef DEBUG
73
+        Serial.print("Invalid character after R: ");
74
+        Serial.print(c);
75
+        Serial.println();
76
+#endif
77
+      }
78
+    } else if (c == 'U') {
79
+      c = Serial.read();
80
+      if (c == 'V') {
81
+        c = Serial.parseInt();
82
+        if (c == 0) {
83
+          digitalWrite(uvPin, LOW);
84
+#ifdef DEBUG
85
+          Serial.println("UV off");
86
+#endif
87
+        } else if (c == 1) {
88
+          digitalWrite(uvPin, HIGH);
89
+#ifdef DEBUG
90
+          Serial.println("UV on");
91
+#endif
92
+        } else {
93
+#ifdef DEBUG
94
+          Serial.print("Invalid character for UV: ");
95
+          Serial.print(c);
96
+          Serial.println();
97
+#endif
98
+        }
99
+      } else {
100
+#ifdef DEBUG
101
+        Serial.print("Invalid character after U: ");
102
+        Serial.print(c);
103
+        Serial.println();
104
+#endif
105
+      }
106
+    } else if ((c == '\n') || (c == '\r')) {
107
+#ifdef DEBUG
108
+      Serial.println("Skipping new-line or carriage-return...");
109
+#endif
110
+    } else {
111
+#ifdef DEBUG
112
+      Serial.print("Invalid character: ");
113
+      Serial.print(c);
114
+      Serial.println();
115
+#endif
116
+    }
117
+  }
118
+}
119
+

Loading…
Cancel
Save