From 0306d4f74a31dbe1e270ef6a934a572861a32bc6 Mon Sep 17 00:00:00 2001 From: Jake Hillion Date: Sun, 26 Apr 2020 18:59:29 +0100 Subject: [PATCH] Arduino first attempt --- arduino/arduino.ino | 116 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 109 insertions(+), 7 deletions(-) diff --git a/arduino/arduino.ino b/arduino/arduino.ino index 08f1177..6fea865 100644 --- a/arduino/arduino.ino +++ b/arduino/arduino.ino @@ -3,6 +3,9 @@ #define NUM_STRIPS 2 #define NUM_LEDS_PER_STRIP 180 +#define NUM_LEDS_PER_SQUARE 12 +#define NUM_SQUARES_PER_STRIP 15 + #define PIN_0 12 #define PIN_1 13 @@ -11,16 +14,115 @@ CRGB leds[NUM_STRIPS][NUM_LEDS_PER_STRIP]; void setup() { FastLED.addLeds(leds[0], NUM_LEDS_PER_STRIP); FastLED.addLeds(leds[1], NUM_LEDS_PER_STRIP); + + Serial.begin(9600); } +int hex; +int rgb; +int led; + +int stored[3]; + void loop() { - for(int x = 0; x < NUM_STRIPS; x++) { - // This inner loop will go over each led in the current strip, one at a time - for(int i = 0; i < NUM_LEDS_PER_STRIP; i++) { - leds[x][i] = CRGB::Red; - FastLED.show(); - leds[x][i] = CRGB::Black; - delay(100); + if (Serial.available() > 0) { + int data; + switch (data = Serial.read()) { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + case 'a': + case 'b': + case 'c': + case 'd': + case 'e': + case 'f': + hex = hex << 2 + charMap(data); + break; + case 'n': + nextColour(); + break; + case 'l': // end of LED + nextLed(); + break; + case 'q': + complete(); + break; + case 'r': + resetState(); + break; } } } + +void nextColour() { + stored[rgb++] = hex; + hex = 0; +} + +void nextLed() { + CRGB color = CRGB(stored[0], stored[1], stored[2]); + + int strip = led / NUM_SQUARES_PER_STRIP; + int start = (led * NUM_LEDS_PER_SQUARE) - (strip * NUM_LEDS_PER_STRIP); + + for (int i = start; i < start + NUM_LEDS_PER_SQUARE; i++) { + leds[strip][i] = color; + } + + rgb = hex = 0; +} + +void complete() { + FastLED.show(); + led = rgb = hex = 0; +} + +void resetState() { + led = rgb = hex = 0; +} + +int charMap(char that) { + switch (that) { + case '0': + return 0; + case '1': + return 1; + case '2': + return 2; + case '3': + return 3; + case '4': + return 4; + case '5': + return 5; + case '6': + return 6; + case '7': + return 7; + case '8': + return 8; + case '9': + return 9; + case 'a': + return 10; + case 'b': + return 11; + case 'c': + return 12; + case 'd': + return 13; + case 'e': + return 14; + case 'f': + return 15; + } + return -1; +}