123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- /*
- * CaseLights
- *
- * Arduino RGB LED Controller with Serial interface.
- *
- * Two commands are supported, ending with a new-line:
- *
- * RGB r g b
- *
- * UV s
- *
- * The RGB command sets the PWM output for the LEDs.
- * The UV command turns the UV lights on or off (s can be 0 or 1).
- */
-
- //#define DEBUG
-
- static int redPin = 10;
- static int greenPin = 9;
- static int bluePin = 11;
- static int uvPin = 13;
-
- void setup() {
- Serial.begin(115200);
-
- pinMode(redPin, OUTPUT);
- pinMode(greenPin, OUTPUT);
- pinMode(bluePin, OUTPUT);
- pinMode(uvPin, OUTPUT);
-
- analogWrite(redPin, 0);
- analogWrite(greenPin, 0);
- analogWrite(bluePin, 0);
- digitalWrite(uvPin, LOW);
-
- #ifdef DEBUG
- Serial.println("CaseLights initialized");
- #endif
- }
-
- void loop() {
- if (Serial.available() > 0) {
- int c = Serial.read();
- if (c == 'R') {
- c = Serial.read();
- if (c == 'G') {
- c = Serial.read();
- if (c == 'B') {
- int r = Serial.parseInt();
- int g = Serial.parseInt();
- int b = Serial.parseInt();
- analogWrite(redPin, r);
- analogWrite(greenPin, g);
- analogWrite(bluePin, b);
- #ifdef DEBUG
- Serial.print("RGB set ");
- Serial.print(r);
- Serial.print(' ');
- Serial.print(g);
- Serial.print(' ');
- Serial.print(b);
- Serial.println();
- #endif
- } else {
- #ifdef DEBUG
- Serial.print("Invalid character after G: ");
- Serial.print(c);
- Serial.println();
- #endif
- }
- } else {
- #ifdef DEBUG
- Serial.print("Invalid character after R: ");
- Serial.print(c);
- Serial.println();
- #endif
- }
- } else if (c == 'U') {
- c = Serial.read();
- if (c == 'V') {
- c = Serial.parseInt();
- if (c == 0) {
- digitalWrite(uvPin, LOW);
- #ifdef DEBUG
- Serial.println("UV off");
- #endif
- } else if (c == 1) {
- digitalWrite(uvPin, HIGH);
- #ifdef DEBUG
- Serial.println("UV on");
- #endif
- } else {
- #ifdef DEBUG
- Serial.print("Invalid character for UV: ");
- Serial.print(c);
- Serial.println();
- #endif
- }
- } else {
- #ifdef DEBUG
- Serial.print("Invalid character after U: ");
- Serial.print(c);
- Serial.println();
- #endif
- }
- } else if ((c == '\n') || (c == '\r')) {
- #ifdef DEBUG
- Serial.println("Skipping new-line or carriage-return...");
- #endif
- } else {
- #ifdef DEBUG
- Serial.print("Invalid character: ");
- Serial.print(c);
- Serial.println();
- #endif
- }
- }
- }
|