From 2453c8b9ce933ec29f3576531736fcaeaf3bbe00 Mon Sep 17 00:00:00 2001 From: Matt Date: Mon, 11 Apr 2016 00:11:38 -0400 Subject: [PATCH] Light code --- ino/mcopy_light.ino | 83 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 ino/mcopy_light.ino diff --git a/ino/mcopy_light.ino b/ino/mcopy_light.ino new file mode 100644 index 0000000..a1a13df --- /dev/null +++ b/ino/mcopy_light.ino @@ -0,0 +1,83 @@ +#include "SoftwareSerial.h" +#include "Adafruit_Pixie.h" + +#define NUMPIXELS 1 // Number of Pixies in the strip +#define PIXIEPIN 6 // Pin number for SoftwareSerial output + +SoftwareSerial pixieSerial(-1, PIXIEPIN); +Adafruit_Pixie light = Adafruit_Pixie(NUMPIXELS, &pixieSerial); + +String color = "000,000,000,000"; + +volatile int commaR = 0; +volatile int commaG = 0; +volatile int commaB = 0; + +String strR = "000"; +String strG = "000"; +String strB = "000"; +String strA = "000"; // To the end of the string + +volatile int r = 0; +volatile int g = 0; +volatile int b = 0; +volatile int a = 0; + +volatile char cmd_char = 'z'; + +void setup () { + Serial.begin(57600); + Serial.flush(); + pixieSerial.begin(115200); // Pixie REQUIRES this baud rate + black(); +} + +void loop () { + if (Serial.available()) { + /* read the most recent byte */ + cmd_char = (char)Serial.read(); + } + if (cmd_char != 'z') { + cmd(cmd_char); + cmd_char = 'z'; + } +} + +// +//b - off +//c - color - followed by String +// +void cmd (char val) { + if (val == 'b') { + black(); + } else if (val == 'c') { + colorString(); + } +} + +void black () { + light.setPixelColor(0, 0, 0, 0); +} + +void colorString () { + while (Serial.available() == 0) { + //Wait for color string + } + color = Serial.readString(); + + commaR = color.indexOf(','); //comma trailing R + commaG = color.indexOf(',', commaR + 1); + commaB = color.indexOf(',', commaG + 1); + + strR = color.substring(0, commaR); + strG = color.substring(commaR + 1, commaG); + strB = color.substring(commaG + 1, commaB); + strA = color.substring(commaB + 1); // To the end of the string + + r = strR.toInt(); + g = strG.toInt(); + b = strB.toInt(); + a = strA.toInt(); + + light.setPixelColor(r, g, b, a); +} \ No newline at end of file