diff --git a/neopixel_test.txt b/neopixel_test.txt new file mode 100644 index 0000000000000000000000000000000000000000..7f28ef75c5d4858397d6c3574782deea4f7fd2b9 --- /dev/null +++ b/neopixel_test.txt @@ -0,0 +1,37 @@ +// Simple NeoPixel test. Lights just a few pixels at a time so a +// 1m strip can safely be powered from Arduino 5V pin. Arduino +// may nonetheless hiccup when LEDs are first connected and not +// accept code. So upload code first, unplug USB, connect pixels +// to GND FIRST, then +5V and digital pin 6, then re-plug USB. +// A working strip will show a few pixels moving down the line, +// cycling between red, green and blue. If you get no response, +// might be connected to wrong end of strip (the end wires, if +// any, are no indication -- look instead for the data direction +// arrows printed on the strip). + +#include <Arduino.h> +#include <Adafruit_NeoPixel.h> + +#define PIN 6 +#define N_LEDS 144 + +Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_LEDS, PIN, NEO_GRB + NEO_KHZ800); + +void setup() { + strip.begin(); +} + +static void chase(uint32_t c) { + for(uint16_t i=0; i<strip.numPixels()+4; i++) { + strip.setPixelColor(i , c); // Draw new pixel + strip.setPixelColor(i-4, 0); // Erase pixel a few steps back + strip.show(); + delay(25); + } +} + +void loop() { + chase(strip.Color(255, 0, 0)); // Red + chase(strip.Color(0, 255, 0)); // Green + chase(strip.Color(0, 0, 255)); // Blue +} \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index af7eb17e9f8f0480999984a41c8c50a2753bdbd4..7941300e95323e040749bbfb0ad1465daf600f69 100644 --- a/platformio.ini +++ b/platformio.ini @@ -15,5 +15,6 @@ framework = arduino lib_deps = Adafruit NeoPixel + TaskScheduler upload_port = /dev/cu.wchusbserial1420 diff --git a/src/main.cpp b/src/main.cpp index 9066008f56f6104002a02cb6da181015aa5de222..48c570099a81ddea63e59bcaf9465120cbcac9a6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,28 +1,87 @@ #include <Arduino.h> #include <Adafruit_NeoPixel.h> +#include <TaskScheduler.h> #define NEOPIXEL_PIN 6 #define NEOPIXEL_LEN 105 +// Pin A and Pin B are connected if the button is pressed. +// Pin A and Pin No are connected if no external button is connected. +#define EXTERNAL_BUTTON_PIN_A 7 +#define EXTERNAL_BUTTON_PIN_B 9 +#define EXTERNAL_BUTTON_PIN_NO 8 + +#define INTERNAL_BUTTON_PIN_A 10 +#define INTERNAL_BUTTON_PIN_B 11 + Adafruit_NeoPixel pixels(NEOPIXEL_LEN, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800); +Scheduler scheduler; +void handleButton(); +Task taskButton(TASK_IMMEDIATE, TASK_FOREVER, &handleButton); +void handleStep(); +Task taskStep(25, TASK_FOREVER, &handleStep); + void setup() { pinMode(LED_BUILTIN, OUTPUT); + + pinMode(EXTERNAL_BUTTON_PIN_A, OUTPUT); + digitalWrite(EXTERNAL_BUTTON_PIN_A, LOW); + pinMode(EXTERNAL_BUTTON_PIN_B, INPUT_PULLUP); + pinMode(EXTERNAL_BUTTON_PIN_NO, INPUT_PULLUP); + pinMode(INTERNAL_BUTTON_PIN_A, OUTPUT); + digitalWrite(INTERNAL_BUTTON_PIN_A, LOW); + pinMode(INTERNAL_BUTTON_PIN_B, INPUT_PULLUP); + pixels.begin(); + scheduler.init(); + scheduler.addTask(taskButton); + taskButton.enable(); + scheduler.addTask(taskStep); + taskStep.enable(); } -int pos = 0; - void loop() { + scheduler.execute(); +} + +uint32_t color = 0xFFFFFF; +int position = 0; + +void show() { for (int i = 0; i < NEOPIXEL_LEN; i++) { - pixels.setPixelColor(i, 0xFFFFFF); + pixels.setPixelColor(i, 0); + } + for (int i = position; i < position + 10; i++) { + pixels.setPixelColor(i, color); } - pixels.setPixelColor(pos, 0xFF0000); pixels.show(); - pos = (pos + 1) % NEOPIXEL_LEN; +} - digitalWrite(LED_BUILTIN, HIGH); - delay(500); - digitalWrite(LED_BUILTIN, LOW); - delay(500); +void handleButton() { + if (digitalRead(INTERNAL_BUTTON_PIN_B) == LOW) { + color = 0x0000FF; + } else if (digitalRead(EXTERNAL_BUTTON_PIN_NO) == LOW) { + color = 0xFF0000; + } else if (digitalRead(EXTERNAL_BUTTON_PIN_B) == LOW) { + color = 0x00FF00; + } + show(); +} + +boolean forward = true; +void handleStep() { + if (forward) { + position = position + 1; + if (position > NEOPIXEL_LEN - 11) { + position = position - 2; + forward = false; + } + } else { + position = position - 1; + if (position < 0) { + position = 1; + forward = true; + } + } }