From 14237409185d645cb048aaf71f7ac294b81fe6e9 Mon Sep 17 00:00:00 2001 From: Malte Schmitz <malte@schmitz-sh.de> Date: Sat, 28 May 2022 22:10:52 +0200 Subject: [PATCH] Add I/O and scheduler --- neopixel_test.txt | 37 +++++++++++++++++++++++ platformio.ini | 1 + src/main.cpp | 77 +++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 106 insertions(+), 9 deletions(-) create mode 100644 neopixel_test.txt diff --git a/neopixel_test.txt b/neopixel_test.txt new file mode 100644 index 0000000..7f28ef7 --- /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 af7eb17..7941300 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 9066008..48c5700 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; + } + } } -- GitLab